FloorVisualizer/internal/handler/router.go
dindang cb7e595c27 Fix lint warnings and code cleanup
- Remove duplicate calc structs, use model.CalcXxx
- Replace log.Printf with logger in recommend handler
- Remove dead code (unused var, dummy imports)
- Clean up response helpers
- Add api/ package with route registration

Co-Authored-By: Claude <noreply@anthropic.com>
2026-07-16 10:29:11 +08:00

43 lines
986 B
Go

package handler
import (
"encoding/json"
"net/http"
"net/url"
"floorvisualizer/internal/model"
)
func NewProxyClient(proxyURLStr string) *http.Client {
u, _ := url.Parse(proxyURLStr)
if u == nil {
return http.DefaultClient
}
return &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(u)}}
}
func writeJSON(w http.ResponseWriter, status int, v any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
if status >= 400 {
msg := "Unknown error"
switch x := v.(type) {
case string:
msg = x
case map[string]string:
if e, ok := x["error"]; ok {
msg = e
}
}
json.NewEncoder(w).Encode(model.ApiError{Code: status, Error: msg})
return
}
json.NewEncoder(w).Encode(model.ApiResponse{Code: status, Data: v})
}
func writeErr(w http.ResponseWriter, status int, msg string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(model.ApiError{Code: status, Error: msg})
}