2026-01-13 20:26:15 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
|
|
import (
|
2026-02-16 20:16:57 +00:00
|
|
|
"context"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
2026-01-13 20:26:15 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/api"
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/auth"
|
2026-02-16 20:16:57 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
2026-01-30 18:21:27 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/html"
|
2026-01-13 20:26:15 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Router() chi.Router {
|
|
|
|
|
r := chi.NewRouter()
|
|
|
|
|
// Root is a special endpoint that is neither authenticated nor unauthenticated
|
|
|
|
|
r.Get("/", getRoot)
|
|
|
|
|
|
|
|
|
|
// Unauthenticated endpoints
|
|
|
|
|
r.Get("/arcgis/oauth/begin", getArcgisOauthBegin)
|
|
|
|
|
r.Get("/arcgis/oauth/callback", getArcgisOauthCallback)
|
|
|
|
|
r.Get("/district", getDistrict)
|
|
|
|
|
|
2026-02-10 16:24:37 +00:00
|
|
|
// Mock endpoints
|
2026-02-16 19:52:20 +00:00
|
|
|
r.Get("/mock", renderMockList)
|
|
|
|
|
addMock(r, "/mock/report", "sync/mock/report.html")
|
|
|
|
|
addMock(r, "/mock/report/{code}", "sync/mock/report-detail.html")
|
|
|
|
|
addMock(r, "/mock/report/{code}/confirm", "sync/mock/report-confirmation.html")
|
|
|
|
|
addMock(r, "/mock/report/{code}/contribute", "sync/mock/report-contribute.html")
|
|
|
|
|
addMock(r, "/mock/report/{code}/evidence", "sync/mock/report-evidence.html")
|
|
|
|
|
addMock(r, "/mock/report/{code}/schedule", "sync/mock/report-schedule.html")
|
|
|
|
|
addMock(r, "/mock/report/{code}/update", "sync/mock/report-update.html")
|
2026-01-13 20:26:15 +00:00
|
|
|
|
2026-02-10 16:24:37 +00:00
|
|
|
// Utility endpoints
|
2026-01-13 20:26:15 +00:00
|
|
|
r.Get("/oauth/refresh", getOAuthRefresh)
|
2026-01-22 18:37:00 +00:00
|
|
|
r.Get("/privacy", getPrivacy)
|
2026-01-13 20:26:15 +00:00
|
|
|
r.Get("/qr-code/report/{code}", getQRCodeReport)
|
|
|
|
|
r.Get("/signin", getSignin)
|
|
|
|
|
r.Post("/signin", postSignin)
|
|
|
|
|
r.Get("/signup", getSignup)
|
|
|
|
|
r.Post("/signup", postSignup)
|
2026-02-07 05:51:21 +00:00
|
|
|
r.Get("/template-test", getTemplateTest)
|
2026-01-13 20:26:15 +00:00
|
|
|
|
|
|
|
|
// Authenticated endpoints
|
|
|
|
|
r.Route("/api", api.AddRoutes)
|
2026-02-17 14:26:19 +00:00
|
|
|
r.Method("GET", "/admin", authenticatedHandler(getAdminDash))
|
2026-01-13 20:26:15 +00:00
|
|
|
r.Method("GET", "/cell/{cell}", auth.NewEnsureAuth(getCellDetails))
|
2026-02-17 17:29:15 +00:00
|
|
|
r.Method("GET", "/download", authenticatedHandler(getDownloadList))
|
2026-01-28 17:15:42 +00:00
|
|
|
r.Method("GET", "/layout-test", auth.NewEnsureAuth(getLayoutTest))
|
2026-02-17 07:05:31 +00:00
|
|
|
r.Method("GET", "/message", authenticatedHandler(getMessageList))
|
2026-02-10 16:24:37 +00:00
|
|
|
r.Method("GET", "/notification", auth.NewEnsureAuth(getNotificationList))
|
2026-02-07 18:26:47 +00:00
|
|
|
r.Method("GET", "/pool", auth.NewEnsureAuth(getPoolList))
|
2026-02-07 20:02:39 +00:00
|
|
|
r.Method("GET", "/pool/upload", auth.NewEnsureAuth(getPoolUpload))
|
2026-02-08 03:47:48 +00:00
|
|
|
r.Method("GET", "/pool/upload/{id}", auth.NewEnsureAuth(getPoolUploadByID))
|
2026-02-08 00:59:41 +00:00
|
|
|
r.Method("POST", "/pool/upload", auth.NewEnsureAuth(postPoolUpload))
|
2026-02-16 19:14:58 +00:00
|
|
|
r.Method("GET", "/radar", auth.NewEnsureAuth(getRadar))
|
2026-02-17 19:06:51 +00:00
|
|
|
r.Method("GET", "/service-request", authenticatedHandler(getServiceRequestList))
|
2026-02-17 19:51:50 +00:00
|
|
|
r.Method("GET", "/service-request/{id}", authenticatedHandler(getServiceRequestDetail))
|
2026-02-13 21:14:46 +00:00
|
|
|
r.Method("GET", "/setting", auth.NewEnsureAuth(getSetting))
|
2026-02-17 05:33:12 +00:00
|
|
|
r.Method("GET", "/setting/organization", auth.NewEnsureAuth(getSettingOrganization))
|
2026-02-13 21:14:46 +00:00
|
|
|
r.Method("GET", "/setting/integration", auth.NewEnsureAuth(getSettingIntegration))
|
2026-02-16 20:16:57 +00:00
|
|
|
r.Method("GET", "/setting/pesticide", authenticatedHandler(getSettingPesticide))
|
2026-02-16 20:19:56 +00:00
|
|
|
r.Method("GET", "/setting/pesticide/add", authenticatedHandler(getSettingPesticideAdd))
|
2026-02-16 20:25:57 +00:00
|
|
|
r.Method("GET", "/setting/user", authenticatedHandler(getSettingUserList))
|
|
|
|
|
r.Method("GET", "/setting/user/add", authenticatedHandler(getSettingUserAdd))
|
2026-01-28 14:57:50 +00:00
|
|
|
r.Method("GET", "/signout", auth.NewEnsureAuth(getSignout))
|
2026-01-13 20:26:15 +00:00
|
|
|
r.Method("GET", "/source/{globalid}", auth.NewEnsureAuth(getSource))
|
2026-02-12 21:06:35 +00:00
|
|
|
r.Method("GET", "/stadia", auth.NewEnsureAuth(getStadia))
|
2026-01-15 22:12:35 +00:00
|
|
|
r.Method("GET", "/trap/{globalid}", auth.NewEnsureAuth(getTrap))
|
2026-01-28 14:58:13 +00:00
|
|
|
r.Method("GET", "/text/{destination}", auth.NewEnsureAuth(getTextMessages))
|
2026-02-17 17:29:15 +00:00
|
|
|
r.Method("GET", "/upload", authenticatedHandler(getUploadList))
|
2026-01-13 20:26:15 +00:00
|
|
|
|
2026-01-30 18:21:27 +00:00
|
|
|
html.AddStaticRoute(r, "/static")
|
2026-01-13 20:26:15 +00:00
|
|
|
return r
|
|
|
|
|
}
|
2026-02-16 20:16:57 +00:00
|
|
|
|
|
|
|
|
type errorWithStatus struct {
|
|
|
|
|
Message string
|
|
|
|
|
Status int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *errorWithStatus) Error() string {
|
|
|
|
|
return e.Message
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-17 19:51:50 +00:00
|
|
|
type handlerFunction[T any] func(context.Context, *models.User) (string, T, *errorWithStatus)
|
2026-02-16 20:16:57 +00:00
|
|
|
type wrappedHandler func(http.ResponseWriter, *http.Request)
|
2026-02-17 19:51:50 +00:00
|
|
|
type contentAuthenticated[T any] struct {
|
|
|
|
|
C T
|
2026-02-16 20:16:57 +00:00
|
|
|
URL ContentURL
|
|
|
|
|
User User
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// w http.ResponseWriter, r *http.Request, u *models.User) {
|
2026-02-17 19:51:50 +00:00
|
|
|
func authenticatedHandler[T any](f handlerFunction[T]) http.Handler {
|
2026-02-16 20:16:57 +00:00
|
|
|
return auth.NewEnsureAuth(func(w http.ResponseWriter, r *http.Request, u *models.User) {
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
userContent, err := contentForUser(ctx, u)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
template, content, e := f(ctx, u)
|
|
|
|
|
if err != nil {
|
|
|
|
|
//log.Warn().Int("status", s).Err(e).Str("user message", m).Msg("Responding with an error from sync pages")
|
|
|
|
|
http.Error(w, err.Error(), e.Status)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-02-17 19:51:50 +00:00
|
|
|
html.RenderOrError(w, template, contentAuthenticated[T]{
|
2026-02-16 20:16:57 +00:00
|
|
|
C: content,
|
|
|
|
|
URL: newContentURL(),
|
|
|
|
|
User: userContent,
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|