refactor routing into a modular and versioned structure

Move inline route definitions from main.go into a dedicated internal/routes package. This organization introduces support for API versioning (v1) and separates endpoint logic into specialized modules for administration and bug reporting.
This commit is contained in:
Flavio Fois
2026-03-23 09:18:16 +01:00
parent 9aa188af8c
commit 84521d8d59
5 changed files with 136 additions and 76 deletions

View File

@@ -0,0 +1,41 @@
package v1
import (
apimw "emly-api-go/internal/middleware"
"time"
"emly-api-go/internal/handlers"
"github.com/go-chi/chi/v5"
"github.com/go-chi/httprate"
"github.com/jmoiron/sqlx"
)
func registerBugReports(r chi.Router, db *sqlx.DB) {
r.Route("/bug-reports", func(r chi.Router) {
// API key only: submit a report and check count
r.Group(func(r chi.Router) {
r.Use(apimw.APIKeyAuth(db))
r.Use(httprate.LimitByIP(30, time.Minute))
r.Get("/count", handlers.GetReportsCount(db))
r.Post("/", handlers.CreateBugReport(db))
})
// API key + admin key: full read/write access
r.Group(func(r chi.Router) {
r.Use(apimw.APIKeyAuth(db))
r.Use(apimw.AdminKeyAuth(db))
r.Use(httprate.LimitByIP(30, time.Minute))
r.Get("/", handlers.GetAllBugReports(db))
r.Get("/{id}", handlers.GetBugReportByID(db))
r.Get("/{id}/status", handlers.GetReportStatusByID(db))
r.Get("/{id}/files", handlers.GetReportFilesByReportID(db))
r.Get("/{id}/files/{file_id}", handlers.GetReportFileByFileID(db))
r.Get("/{id}/download", handlers.GetBugReportZipById(db))
r.Patch("/{id}/status", handlers.PatchBugReportStatus(db))
r.Delete("/{id}", handlers.DeleteBugReportByID(db))
})
})
}