84 lines
1.9 KiB
Go
84 lines
1.9 KiB
Go
package httpentity
|
|
|
|
// import (
|
|
// "encoding/json"
|
|
// "log"
|
|
// "net/http"
|
|
// "strings"
|
|
|
|
// "github.com/gin-gonic/gin"
|
|
// )
|
|
|
|
// func NewWebServer(addr string) *http.Server {
|
|
// srv := &http.Server{
|
|
// Addr: addr,
|
|
// Handler: createRouter(),
|
|
// }
|
|
|
|
// return srv
|
|
// }
|
|
|
|
// func createRouter() *gin.Engine {
|
|
// // Create a new gin router for api
|
|
// // What is difference between gin.Default() and gin.New()?
|
|
// // https://stackoverflow.com/questions/44318441/what-is-difference-between-gin-default-and-gin-new
|
|
|
|
// apiEngine := gin.New()
|
|
// apiGroup := apiEngine.Group("/api")
|
|
// {
|
|
// apiGroup.GET("/randomNumber", GET_RandomNumber)
|
|
// apiGroup.GET("/randomPassword", GET_RandomPassword)
|
|
// apiGroup.GET("/randomDate", GET_RandomDate)
|
|
// }
|
|
|
|
// // create a new gin router for static files
|
|
// staticEngine := gin.New()
|
|
// staticEngine.Static("/", "./web")
|
|
|
|
// // Create a new gin router
|
|
// r := gin.Default()
|
|
// // r can accept all messages from apiEngine and staticEngine
|
|
// r.Any("/*any", func(c *gin.Context) {
|
|
// defer handleError(c)
|
|
// w := c.Writer
|
|
// w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
// w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
|
|
|
|
// path := c.Param("any")
|
|
|
|
// if strings.HasPrefix(path, "/api") {
|
|
// apiEngine.ServeHTTP(c.Writer, c.Request)
|
|
// } else {
|
|
// staticEngine.HandleContext(c)
|
|
// }
|
|
|
|
// })
|
|
|
|
// // Return the router
|
|
// return r
|
|
// }
|
|
|
|
// func GET_RandomNumber(c *gin.Context) {
|
|
|
|
// // make a json decoder
|
|
// dec := json.NewDecoder(c.Request.Body)
|
|
// obj := map[string]interface{}{}
|
|
// dec.Decode(&obj)
|
|
|
|
// seed := obj["seed"]
|
|
// place := obj["place"]
|
|
|
|
// response := map[string]interface{}{
|
|
// "value": 10,
|
|
// }
|
|
|
|
// c.JSON(http.StatusOK)
|
|
// }
|
|
|
|
// func handleError(c *gin.Context) {
|
|
// if r := recover(); r != nil {
|
|
// log.Println(r)
|
|
// c.String(http.StatusBadRequest, r.(error).Error())
|
|
// }
|
|
// }
|