- Introduced .editorconfig for consistent coding styles across the project. - Added .golangci.yml for Go linting configuration. - Updated AGENTS.md to clarify project structure and components. - Enhanced CONTRIBUTING.md with Makefile usage for common tasks. - Updated Dockerfiles to use Go 1.24 and improved build instructions. - Refined README.md and deployment documentation for clarity. - Added testing documentation in testing.md for backend and frontend tests. - Introduced Makefile for streamlined development commands and tasks.
77 lines
2.0 KiB
Go
77 lines
2.0 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/andyleap/hnh-map/internal/app"
|
|
)
|
|
|
|
// APIConfig handles GET /map/api/config.
|
|
func (h *Handlers) APIConfig(rw http.ResponseWriter, req *http.Request) {
|
|
ctx := req.Context()
|
|
s := h.Auth.GetSession(ctx, req)
|
|
if s == nil {
|
|
JSONError(rw, http.StatusUnauthorized, "Unauthorized", "UNAUTHORIZED")
|
|
return
|
|
}
|
|
config, err := h.Map.GetConfig(ctx, s.Auths)
|
|
if err != nil {
|
|
HandleServiceError(rw, err)
|
|
return
|
|
}
|
|
JSON(rw, http.StatusOK, config)
|
|
}
|
|
|
|
// APIGetChars handles GET /map/api/v1/characters.
|
|
func (h *Handlers) APIGetChars(rw http.ResponseWriter, req *http.Request) {
|
|
ctx := req.Context()
|
|
s := h.Auth.GetSession(ctx, req)
|
|
if !h.canAccessMap(s) {
|
|
JSONError(rw, http.StatusUnauthorized, "Unauthorized", "UNAUTHORIZED")
|
|
return
|
|
}
|
|
if !s.Auths.Has(app.AUTH_MARKERS) && !s.Auths.Has(app.AUTH_ADMIN) {
|
|
JSON(rw, http.StatusOK, []interface{}{})
|
|
return
|
|
}
|
|
chars := h.Map.GetCharacters()
|
|
JSON(rw, http.StatusOK, chars)
|
|
}
|
|
|
|
// APIGetMarkers handles GET /map/api/v1/markers.
|
|
func (h *Handlers) APIGetMarkers(rw http.ResponseWriter, req *http.Request) {
|
|
ctx := req.Context()
|
|
s := h.Auth.GetSession(ctx, req)
|
|
if !h.canAccessMap(s) {
|
|
JSONError(rw, http.StatusUnauthorized, "Unauthorized", "UNAUTHORIZED")
|
|
return
|
|
}
|
|
if !s.Auths.Has(app.AUTH_MARKERS) && !s.Auths.Has(app.AUTH_ADMIN) {
|
|
JSON(rw, http.StatusOK, []interface{}{})
|
|
return
|
|
}
|
|
markers, err := h.Map.GetMarkers(ctx)
|
|
if err != nil {
|
|
HandleServiceError(rw, err)
|
|
return
|
|
}
|
|
JSON(rw, http.StatusOK, markers)
|
|
}
|
|
|
|
// APIGetMaps handles GET /map/api/maps.
|
|
func (h *Handlers) APIGetMaps(rw http.ResponseWriter, req *http.Request) {
|
|
ctx := req.Context()
|
|
s := h.Auth.GetSession(ctx, req)
|
|
if !h.canAccessMap(s) {
|
|
JSONError(rw, http.StatusUnauthorized, "Unauthorized", "UNAUTHORIZED")
|
|
return
|
|
}
|
|
showHidden := s.Auths.Has(app.AUTH_ADMIN)
|
|
maps, err := h.Map.GetMaps(ctx, showHidden)
|
|
if err != nil {
|
|
HandleServiceError(rw, err)
|
|
return
|
|
}
|
|
JSON(rw, http.StatusOK, maps)
|
|
}
|