mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-27 07:16:19 +09:00
refactor: initial go service
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
|
||||
"github.com/memohai/memoh/internal/auth"
|
||||
"github.com/memohai/memoh/internal/handlers"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
echo *echo.Echo
|
||||
addr string
|
||||
}
|
||||
|
||||
func NewServer(addr string, jwtSecret string, pingHandler *handlers.PingHandler, authHandler *handlers.AuthHandler, memoryHandler *handlers.MemoryHandler, fsHandler *handlers.FSHandler, swaggerHandler *handlers.SwaggerHandler) *Server {
|
||||
if addr == "" {
|
||||
addr = ":8080"
|
||||
}
|
||||
|
||||
e := echo.New()
|
||||
e.HideBanner = true
|
||||
e.Use(middleware.Recover())
|
||||
e.Use(middleware.RequestLogger())
|
||||
e.Use(auth.JWTMiddleware(jwtSecret, func(c echo.Context) bool {
|
||||
path := c.Request().URL.Path
|
||||
if path == "/ping" || path == "/api/swagger.json" || path == "/auth/login" {
|
||||
return true
|
||||
}
|
||||
if strings.HasPrefix(path, "/api/docs") {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}))
|
||||
|
||||
if pingHandler != nil {
|
||||
pingHandler.Register(e)
|
||||
}
|
||||
if authHandler != nil {
|
||||
authHandler.Register(e)
|
||||
}
|
||||
if memoryHandler != nil {
|
||||
memoryHandler.Register(e)
|
||||
}
|
||||
if fsHandler != nil {
|
||||
fsHandler.Register(e)
|
||||
}
|
||||
if swaggerHandler != nil {
|
||||
swaggerHandler.Register(e)
|
||||
}
|
||||
|
||||
return &Server{
|
||||
echo: e,
|
||||
addr: addr,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) Start() error {
|
||||
return s.echo.Start(s.addr)
|
||||
}
|
||||
Reference in New Issue
Block a user