link-go/server.go
2021-03-15 01:24:23 +06:00

43 lines
1 KiB
Go

package cgnolink
import (
"cgnolink/link"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/knadh/koanf"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"net/http"
)
func NewServer(conf *koanf.Koanf, pool *pgxpool.Pool) *echo.Echo {
server := echo.New()
addMiddleware(server, conf)
addHandlers(server, pool)
return server
}
func addMiddleware(s *echo.Echo, conf *koanf.Koanf) {
s.Use(middleware.CORS())
s.Use(middleware.JWTWithConfig(middleware.JWTConfig{
Skipper: func(ctx echo.Context) bool {
return ctx.Request().Method == "GET" && ctx.Request().URL.Path != "/links"
},
SigningMethod: conf.String("jwt.method"),
SigningKey: conf.Bytes("jwt.key"),
}))
}
func addHandlers(s *echo.Echo, pool *pgxpool.Pool) {
s.GET("", func(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, struct {
Message string `json:"message"`
}{
Message: "Hello. This is my (https://cognio.dev/) URL shortener for easy access to my socials, projects and other resources.",
})
})
link.AddHandlers(s, pool)
}