86 lines
2.1 KiB
Go
86 lines
2.1 KiB
Go
package http
|
|
|
|
import (
|
|
"embed"
|
|
"fmt"
|
|
"io/fs"
|
|
"net/http"
|
|
|
|
"github.com/labstack/echo/v5"
|
|
"github.com/labstack/echo/v5/middleware"
|
|
"trankilou.fr/lassistanoque/backend/internal/config"
|
|
"trankilou.fr/lassistanoque/backend/internal/domain"
|
|
"trankilou.fr/lassistanoque/backend/internal/http/handlers"
|
|
"trankilou.fr/lassistanoque/backend/internal/service/auth"
|
|
"trankilou.fr/lassistanoque/backend/internal/service/user"
|
|
)
|
|
|
|
//go:embed web/_app/*
|
|
var appFiles embed.FS
|
|
|
|
//go:embed web/assets/*
|
|
var assetsFiles embed.FS
|
|
|
|
//go:embed web/index.html
|
|
var indexhtml []byte
|
|
|
|
//go:embed web/robots.txt
|
|
var robotstxt []byte
|
|
|
|
type Router struct {
|
|
settings *domain.Settings
|
|
echo *echo.Echo
|
|
}
|
|
|
|
type Dependencies struct {
|
|
Settings *domain.Settings
|
|
AuthService *auth.Service
|
|
UserService *user.Service
|
|
TokenManager auth.TokenManager
|
|
}
|
|
|
|
func NewRouter(deps Dependencies) *Router {
|
|
|
|
e := echo.New()
|
|
e.Use(middleware.RequestLogger())
|
|
e.Use(middleware.CORS("http://localhost:5173"))
|
|
|
|
// SPA
|
|
fsapp, err := fs.Sub(appFiles, "web/_app")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fsassets, err := fs.Sub(assetsFiles, "web/assets")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
appHandler := http.FileServer(http.FS(fsapp))
|
|
assetsHandler := http.FileServer(http.FS(fsassets))
|
|
e.GET("/_app/*", echo.WrapHandler(http.StripPrefix("/_app/", appHandler)))
|
|
e.GET("/assets/*", echo.WrapHandler(http.StripPrefix("/assets/", assetsHandler)))
|
|
e.GET("/robots.txt", func(c *echo.Context) error {
|
|
return c.Blob(http.StatusOK, "plain/text", robotstxt)
|
|
})
|
|
e.GET("/*", func(c *echo.Context) error {
|
|
fmt.Println("match /*")
|
|
fmt.Println(string(indexhtml))
|
|
return c.Blob(http.StatusOK, "text/html", indexhtml)
|
|
})
|
|
|
|
// API
|
|
api := e.Group("/api")
|
|
_ = handlers.NewAuthGroup("/auth", api, deps.AuthService)
|
|
_ = handlers.NewUserGroup("/user", api, deps.UserService, deps.TokenManager.TokenMiddleware)
|
|
//_ = handlers.NewUserGroup("/user", e, deps.UserService)
|
|
|
|
return &Router{
|
|
settings: deps.Settings,
|
|
echo: e,
|
|
}
|
|
}
|
|
|
|
func (r *Router) Start() error {
|
|
cfg := config.GetConfig()
|
|
return r.echo.Start(fmt.Sprintf(":%d", cfg.HttpPort))
|
|
}
|