notatest/server/pkg/service/service.go

42 lines
933 B
Go

package service
import (
"net/http"
"git.umbrella.haus/ae/notatest/pkg/data"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/jackc/pgx/v5"
"github.com/rs/zerolog/log"
)
func Run(conn *pgx.Conn, jwtSecret string) error {
q := data.New(conn)
r := chi.NewRouter()
tokensRouter := tokensResource{
JWTSecret: jwtSecret,
Tokens: q,
}
usersRouter := usersResource{
JWTSecret: jwtSecret,
Users: q,
}
notesRouter := notesResource{}
// Global middlewares
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(loggerMiddleware(&log.Logger))
r.Use(middleware.Recoverer)
r.Use(middleware.AllowContentType("application/json"))
// Routes grouped by functionality
r.Mount("/auth", tokensRouter.Routes())
r.Mount("/users", usersRouter.Routes())
r.Mount("/notes", notesRouter.Routes())
log.Info().Msg("Starting server on :8080")
return http.ListenAndServe(":8080", r)
}