- build: somewhat polished dockerization setup - build: io/fs migrations with `golang-migrate` - feat: automatic init. admin account creation (.env creds) - feat(routers): combined user & token routers into single auth router - feat(routers): improved route layouts (`Routes`) - feat(middlewares): removed redundant `userCtx` middleware - fix(schema): note <-> note_versions relation (versioning) - feat(queries): removed redundant rollback functionality - feat(queries): combined duplicate version check & insertion/creation - tests: decreased redundancy by removing 'unnecessary' unit tests - refactor: hid internal packages behind `server/internal` - docs: notes & auth handler comments
43 lines
1022 B
Go
43 lines
1022 B
Go
package service
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"git.umbrella.haus/ae/notatest/internal/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, q *data.Queries, jwtSecret string) error {
|
|
r := chi.NewRouter()
|
|
|
|
authRouter := authResource{
|
|
JWTSecret: jwtSecret,
|
|
Users: q,
|
|
Tokens: q,
|
|
}
|
|
notesRouter := notesResource{
|
|
JWTSecret: jwtSecret,
|
|
Notes: q,
|
|
}
|
|
|
|
// 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 (we must prefix the API routes with `/api`
|
|
// as the domain will be the same for the front and back ends)
|
|
r.Route("/api", func(r chi.Router) {
|
|
r.Mount("/auth", authRouter.Routes())
|
|
r.Mount("/notes", notesRouter.Routes())
|
|
})
|
|
|
|
log.Info().Msg("Starting server on :8080")
|
|
return http.ListenAndServe(":8080", r)
|
|
}
|