44 lines
1005 B
Go

package service
import (
"fmt"
"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, httpPort 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())
portStr := fmt.Sprintf(":%s", httpPort)
log.Info().Msgf("Starting server on %s", portStr)
return http.ListenAndServe(portStr, r)
}