Initial commit
This commit is contained in:
48
internal/server/routes.go
Normal file
48
internal/server/routes.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"billit/cmd/web"
|
||||
"github.com/a-h/templ"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
)
|
||||
|
||||
func (s *Server) RegisterRoutes() http.Handler {
|
||||
e := echo.New()
|
||||
e.Use(middleware.Logger())
|
||||
e.Use(middleware.Recover())
|
||||
|
||||
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
||||
AllowOrigins: []string{"https://*", "http://*"},
|
||||
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"},
|
||||
AllowHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
|
||||
AllowCredentials: true,
|
||||
MaxAge: 300,
|
||||
}))
|
||||
|
||||
fileServer := http.FileServer(http.FS(web.Files))
|
||||
e.GET("/assets/*", echo.WrapHandler(fileServer))
|
||||
|
||||
e.GET("/web", echo.WrapHandler(templ.Handler(web.HelloForm())))
|
||||
e.POST("/hello", echo.WrapHandler(http.HandlerFunc(web.HelloWebHandler)))
|
||||
|
||||
e.GET("/", s.HelloWorldHandler)
|
||||
|
||||
e.GET("/health", s.healthHandler)
|
||||
|
||||
return e
|
||||
}
|
||||
|
||||
func (s *Server) HelloWorldHandler(c echo.Context) error {
|
||||
resp := map[string]string{
|
||||
"message": "Hello World",
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
func (s *Server) healthHandler(c echo.Context) error {
|
||||
return c.JSON(http.StatusOK, s.db.Health())
|
||||
}
|
||||
Reference in New Issue
Block a user