2024-10-25 02:32:25 +00:00
|
|
|
package server
|
2024-10-24 12:11:06 +00:00
|
|
|
|
|
|
|
import (
|
2024-10-25 23:59:45 +00:00
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
|
2024-10-25 02:32:25 +00:00
|
|
|
"github.com/ekzyis/hermes/pages"
|
2024-10-24 12:11:06 +00:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/labstack/echo/v4/middleware"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
*echo.Echo
|
|
|
|
}
|
|
|
|
|
2024-10-25 02:32:25 +00:00
|
|
|
func New() *Server {
|
2024-10-24 12:11:06 +00:00
|
|
|
e := echo.New()
|
|
|
|
|
|
|
|
e.Static("/", "public")
|
|
|
|
|
|
|
|
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
|
2024-10-25 02:32:25 +00:00
|
|
|
Format: "${time_custom} ${remote_ip} ${method} ${uri} ${status}\n",
|
2024-10-24 12:11:06 +00:00
|
|
|
CustomTimeFormat: "2006-01-02 15:04:05.00000-0700",
|
|
|
|
}))
|
|
|
|
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
|
|
|
// TODO: add CORS origins
|
|
|
|
AllowOrigins: []string{},
|
|
|
|
AllowCredentials: true,
|
|
|
|
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept},
|
|
|
|
}))
|
|
|
|
|
|
|
|
// TODO: attach error handler
|
|
|
|
// e.HTTPErrorHandler = ...
|
|
|
|
|
2024-10-25 02:32:25 +00:00
|
|
|
e.GET("/", func(c echo.Context) error {
|
2024-10-25 23:59:45 +00:00
|
|
|
return pages.Index().Render(getContext(c), c.Response().Writer)
|
2024-10-25 02:32:25 +00:00
|
|
|
})
|
|
|
|
|
2024-10-24 12:11:06 +00:00
|
|
|
s := &Server{e}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
2024-10-25 23:59:45 +00:00
|
|
|
|
|
|
|
func getContext(c echo.Context) context.Context {
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
ctx = context.WithValue(ctx, "env", os.Getenv("ENVIRONMENT"))
|
|
|
|
ctx = context.WithValue(ctx, "session", c.Get("session"))
|
|
|
|
ctx = context.WithValue(ctx, "commit", os.Getenv("GIT_COMMIT"))
|
|
|
|
ctx = context.WithValue(ctx, "path", c.Request().URL.Path)
|
|
|
|
return ctx
|
|
|
|
}
|