Add existing codebase
This commit is contained in:
commit
e12550a643
25 changed files with 1409 additions and 0 deletions
34
pkg/brainbuffer/server/middleware.go
Normal file
34
pkg/brainbuffer/server/middleware.go
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/rs/zerolog/log"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Logger() echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) (err error) {
|
||||
req := c.Request()
|
||||
res := c.Response()
|
||||
start := time.Now()
|
||||
if err = next(c); err != nil {
|
||||
c.Error(err)
|
||||
}
|
||||
stop := time.Now()
|
||||
|
||||
log.Info().
|
||||
Str("remote_ip", c.RealIP()).
|
||||
Str("host", req.Host).
|
||||
Str("uri", req.RequestURI).
|
||||
Str("method", req.Method).
|
||||
Str("path", req.URL.Path).
|
||||
Str("user_agent", req.UserAgent()).
|
||||
Int("status", res.Status).
|
||||
Str("latency", stop.Sub(start).String()).
|
||||
Send()
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
60
pkg/brainbuffer/server/server.go
Normal file
60
pkg/brainbuffer/server/server.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
apperrors "brainbuffer/pkg/brainbuffer/errors"
|
||||
"brainbuffer/pkg/brainbuffer/task"
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
"github.com/knadh/koanf"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"github.com/rs/zerolog/log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func New(conf *koanf.Koanf, pool *pgxpool.Pool) *echo.Echo {
|
||||
server := echo.New()
|
||||
|
||||
server.HTTPErrorHandler = errorHandler(server)
|
||||
|
||||
registerHandlers(server, pool)
|
||||
registerMiddleware(server, conf)
|
||||
|
||||
return server
|
||||
}
|
||||
|
||||
func registerHandlers(server *echo.Echo, pool *pgxpool.Pool) {
|
||||
task.AddHandlers(server)
|
||||
}
|
||||
|
||||
func registerMiddleware(server *echo.Echo, conf *koanf.Koanf) {
|
||||
server.Use(middleware.CORS())
|
||||
|
||||
server.Use(Logger())
|
||||
|
||||
server.Use(middleware.JWTWithConfig(middleware.JWTConfig{
|
||||
Skipper: func(ctx echo.Context) bool {
|
||||
return false
|
||||
},
|
||||
SigningMethod: middleware.AlgorithmHS256,
|
||||
SigningKey: conf.Bytes("jwt.key"),
|
||||
}))
|
||||
}
|
||||
|
||||
func errorHandler(s *echo.Echo) echo.HTTPErrorHandler {
|
||||
return func(err error, c echo.Context) {
|
||||
mappedErr := err
|
||||
|
||||
switch v := err.(type) {
|
||||
case apperrors.NotFoundError:
|
||||
mappedErr = echo.NewHTTPError(http.StatusNotFound, v.Error())
|
||||
case apperrors.UnknownError:
|
||||
log.Err(v.Err).Send()
|
||||
mappedErr = echo.NewHTTPError(http.StatusInternalServerError)
|
||||
case apperrors.AlreadyExistsError:
|
||||
// TODO: Change status code to hide user identity.
|
||||
mappedErr = echo.NewHTTPError(http.StatusBadRequest, v.Error())
|
||||
}
|
||||
|
||||
s.DefaultHTTPErrorHandler(mappedErr, c)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue