- 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.
126 lines
3.5 KiB
Go
126 lines
3.5 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"regexp"
|
|
|
|
"github.com/andyleap/hnh-map/internal/app"
|
|
"github.com/andyleap/hnh-map/internal/app/services"
|
|
)
|
|
|
|
var clientPath = regexp.MustCompile(`client/([^/]+)/(.*)`)
|
|
|
|
// ClientRouter handles /client/* requests with token-based auth.
|
|
func (h *Handlers) ClientRouter(rw http.ResponseWriter, req *http.Request) {
|
|
matches := clientPath.FindStringSubmatch(req.URL.Path)
|
|
if matches == nil {
|
|
JSONError(rw, http.StatusBadRequest, "Client token not found", "BAD_REQUEST")
|
|
return
|
|
}
|
|
ctx := req.Context()
|
|
username, err := h.Auth.ValidateClientToken(ctx, matches[1])
|
|
if err != nil {
|
|
rw.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
_ = username
|
|
|
|
switch matches[2] {
|
|
case "locate":
|
|
h.clientLocate(rw, req)
|
|
case "gridUpdate":
|
|
h.clientGridUpdate(rw, req)
|
|
case "gridUpload":
|
|
h.clientGridUpload(rw, req)
|
|
case "positionUpdate":
|
|
h.clientPositionUpdate(rw, req)
|
|
case "markerUpdate":
|
|
h.clientMarkerUpdate(rw, req)
|
|
case "":
|
|
http.Redirect(rw, req, "/", http.StatusFound)
|
|
case "checkVersion":
|
|
if req.FormValue("version") == app.ClientVersion {
|
|
rw.WriteHeader(http.StatusOK)
|
|
} else {
|
|
rw.WriteHeader(http.StatusBadRequest)
|
|
}
|
|
default:
|
|
rw.WriteHeader(http.StatusNotFound)
|
|
}
|
|
}
|
|
|
|
func (h *Handlers) clientLocate(rw http.ResponseWriter, req *http.Request) {
|
|
gridID := req.FormValue("gridID")
|
|
result, err := h.Client.Locate(req.Context(), gridID)
|
|
if err != nil {
|
|
rw.WriteHeader(http.StatusNotFound)
|
|
return
|
|
}
|
|
rw.Write([]byte(result))
|
|
}
|
|
|
|
func (h *Handlers) clientGridUpdate(rw http.ResponseWriter, req *http.Request) {
|
|
defer req.Body.Close()
|
|
var grup services.GridUpdate
|
|
if err := json.NewDecoder(req.Body).Decode(&grup); err != nil {
|
|
slog.Error("error decoding grid request", "error", err)
|
|
JSONError(rw, http.StatusBadRequest, "error decoding request", "BAD_REQUEST")
|
|
return
|
|
}
|
|
result, err := h.Client.ProcessGridUpdate(req.Context(), grup)
|
|
if err != nil {
|
|
slog.Error("grid update failed", "error", err)
|
|
return
|
|
}
|
|
json.NewEncoder(rw).Encode(result.Response)
|
|
}
|
|
|
|
func (h *Handlers) clientGridUpload(rw http.ResponseWriter, req *http.Request) {
|
|
ct := req.Header.Get("Content-Type")
|
|
if fixed := services.FixMultipartContentType(ct); fixed != ct {
|
|
req.Header.Set("Content-Type", fixed)
|
|
}
|
|
if err := req.ParseMultipartForm(app.MultipartMaxMemory); err != nil {
|
|
slog.Error("multipart parse error", "error", err)
|
|
return
|
|
}
|
|
id := req.FormValue("id")
|
|
extraData := req.FormValue("extraData")
|
|
file, _, err := req.FormFile("file")
|
|
if err != nil {
|
|
slog.Error("form file error", "error", err)
|
|
return
|
|
}
|
|
defer file.Close()
|
|
if err := h.Client.ProcessGridUpload(req.Context(), id, extraData, file); err != nil {
|
|
slog.Error("grid upload failed", "error", err)
|
|
}
|
|
}
|
|
|
|
func (h *Handlers) clientPositionUpdate(rw http.ResponseWriter, req *http.Request) {
|
|
defer req.Body.Close()
|
|
buf, err := io.ReadAll(req.Body)
|
|
if err != nil {
|
|
slog.Error("error reading position update", "error", err)
|
|
return
|
|
}
|
|
if err := h.Client.UpdatePositions(req.Context(), buf); err != nil {
|
|
slog.Error("position update failed", "error", err)
|
|
}
|
|
}
|
|
|
|
func (h *Handlers) clientMarkerUpdate(rw http.ResponseWriter, req *http.Request) {
|
|
defer req.Body.Close()
|
|
buf, err := io.ReadAll(req.Body)
|
|
if err != nil {
|
|
slog.Error("error reading marker update", "error", err)
|
|
return
|
|
}
|
|
if err := h.Client.UploadMarkers(req.Context(), buf); err != nil {
|
|
slog.Error("marker update failed", "error", err)
|
|
}
|
|
}
|