34 lines
634 B
Go
34 lines
634 B
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"jottyfan.de/learngin/modules"
|
|
)
|
|
|
|
type EchoRequest struct {
|
|
Message string `form:"message" json:"message" binding:"required"`
|
|
}
|
|
|
|
func main() {
|
|
r := gin.Default()
|
|
r.LoadHTMLGlob("templates/*")
|
|
|
|
r.GET("/ping", modules.Ping)
|
|
|
|
r.GET("/", func(c *gin.Context) {
|
|
c.HTML(http.StatusOK, "index.html", nil)
|
|
})
|
|
|
|
r.POST("/echo", func(c *gin.Context) {
|
|
var req EchoRequest
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
} else {
|
|
c.JSON(http.StatusOK, gin.H{"echo": req.Message})
|
|
}
|
|
})
|
|
r.Run(":8080")
|
|
}
|