Simple webserver.
This commit is contained in:
parent
7ad054f1ef
commit
b9016d8db5
3 changed files with 0 additions and 0 deletions
49
2024/10/golang_webserver_demo1/main.go
Normal file
49
2024/10/golang_webserver_demo1/main.go
Normal file
|
@ -0,0 +1,49 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
r := gin.Default()
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{
|
||||
"message": "pong",
|
||||
"datetime": time.Now(),
|
||||
})
|
||||
})
|
||||
|
||||
r.GET("/info", func(c *gin.Context) {
|
||||
var directories []map[string]string
|
||||
dirs, err := os.ReadDir(".")
|
||||
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{
|
||||
"message": "it failed",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
for i := 0; i < len(dirs); i++ {
|
||||
m := make(map[string]string)
|
||||
m["name"] = dirs[i].Name()
|
||||
if dirs[i].IsDir() {
|
||||
m["dir"] = "yes"
|
||||
} else {
|
||||
m["dir"] = "no"
|
||||
}
|
||||
directories = append(directories, m)
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{
|
||||
"dirs": directories,
|
||||
})
|
||||
})
|
||||
|
||||
err := r.Run("localhost:8080")
|
||||
if err != nil {
|
||||
panic("Couldn't run the server. It failed with the error: " + err.Error())
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue