2025-12-16 16:37:53 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
2026-04-21 23:53:42 +00:00
|
|
|
"context"
|
2025-12-16 16:37:53 +00:00
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
|
2026-04-21 23:53:42 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/config"
|
2025-12-16 16:37:53 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db"
|
2026-04-21 23:53:42 +00:00
|
|
|
nhttp "github.com/Gleipnir-Technology/nidus-sync/http"
|
2026-01-02 08:58:57 -07:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/platform"
|
2026-04-14 19:59:32 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
|
2026-04-21 23:53:42 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/resource"
|
2026-04-01 16:57:33 +00:00
|
|
|
//"github.com/gorilla/mux"
|
2025-12-16 16:37:53 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
)
|
|
|
|
|
|
2026-04-01 16:57:33 +00:00
|
|
|
/*
|
|
|
|
|
type renderer struct {
|
|
|
|
|
}
|
|
|
|
|
func (ren *renderer) Render(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
// In the best case scenario, the excellent github.com/pkg/errors package
|
|
|
|
|
// helps reveal information on the error, setting it on Err, and in the Render()
|
|
|
|
|
// method, using it to set the application-specific error code in AppCode.
|
|
|
|
|
type ResponseErr struct {
|
|
|
|
|
Error error `json:"-"` // low-level runtime error
|
|
|
|
|
HTTPStatusCode int `json:"-"` // http response status code
|
|
|
|
|
|
|
|
|
|
StatusText string `json:"status"` // user-level status message
|
|
|
|
|
AppCode int64 `json:"code,omitempty"` // application-specific error code
|
|
|
|
|
ErrorText string `json:"error,omitempty"` // application-level error message, for debugging
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *ResponseErr) Render(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
|
http.Error(w, e.StatusText, e.HTTPStatusCode)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func errRender(err error) *ResponseErr {
|
|
|
|
|
log.Error().Err(err).Msg("Rendering error")
|
|
|
|
|
return &ResponseErr{
|
|
|
|
|
Error: err,
|
|
|
|
|
HTTPStatusCode: 500,
|
|
|
|
|
StatusText: "Error rendering response",
|
|
|
|
|
ErrorText: err.Error(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Renderable interface {
|
|
|
|
|
Render(http.ResponseWriter, *http.Request) error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func renderShim(w http.ResponseWriter, r *http.Request, renderer Renderable) error {
|
|
|
|
|
return renderer.Render(w, r)
|
|
|
|
|
}
|
|
|
|
|
func renderList(w http.ResponseWriter, r *http.Request, data []Renderable) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2026-03-12 23:49:16 +00:00
|
|
|
func handleClientIos(w http.ResponseWriter, r *http.Request, u platform.User) {
|
2026-01-02 08:58:57 -07:00
|
|
|
var sinceStr string
|
|
|
|
|
err := r.ParseForm()
|
2025-12-16 16:37:53 +00:00
|
|
|
if err != nil {
|
2026-04-01 16:57:33 +00:00
|
|
|
renderShim(w, r, errRender(fmt.Errorf("Failed to parse GET form: %w", err)))
|
2025-12-16 16:37:53 +00:00
|
|
|
return
|
2026-01-02 08:58:57 -07:00
|
|
|
} else {
|
|
|
|
|
sinceStr = r.FormValue("since")
|
2025-12-16 16:37:53 +00:00
|
|
|
}
|
2026-01-02 08:58:57 -07:00
|
|
|
|
|
|
|
|
var since *time.Time
|
|
|
|
|
if sinceStr == "" {
|
|
|
|
|
since = nil
|
|
|
|
|
} else {
|
|
|
|
|
since, err = parseTime(sinceStr)
|
|
|
|
|
if err != nil {
|
2026-04-01 16:57:33 +00:00
|
|
|
renderShim(w, r, errRender(fmt.Errorf("Failed to parse 'since' value: %w", err)))
|
2026-01-02 08:58:57 -07:00
|
|
|
return
|
|
|
|
|
}
|
2025-12-16 16:37:53 +00:00
|
|
|
}
|
2026-01-02 08:58:57 -07:00
|
|
|
|
|
|
|
|
csync, err := platform.ContentClientIos(r.Context(), u, since)
|
2025-12-16 16:37:53 +00:00
|
|
|
if err != nil {
|
2026-04-01 16:57:33 +00:00
|
|
|
renderShim(w, r, errRender(err))
|
2025-12-16 16:37:53 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-06 22:23:59 +00:00
|
|
|
var since_used time.Time
|
|
|
|
|
if since == nil {
|
|
|
|
|
since_used = time.Unix(0, 0)
|
|
|
|
|
} else {
|
|
|
|
|
since_used = *since
|
|
|
|
|
}
|
2026-01-02 08:58:57 -07:00
|
|
|
response := ResponseClientIos{
|
2026-01-06 03:06:38 +00:00
|
|
|
Fieldseeker: toResponseFieldseeker(csync.Fieldseeker),
|
2026-01-06 22:23:59 +00:00
|
|
|
Since: since_used,
|
2026-01-02 08:58:57 -07:00
|
|
|
}
|
2026-04-01 16:57:33 +00:00
|
|
|
if err := renderShim(w, r, response); err != nil {
|
|
|
|
|
renderShim(w, r, errRender(err))
|
2025-12-16 16:37:53 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-12 23:49:16 +00:00
|
|
|
func apiMosquitoSource(w http.ResponseWriter, r *http.Request, u platform.User) {
|
2025-12-16 16:37:53 +00:00
|
|
|
bounds, err := parseBounds(r)
|
|
|
|
|
if err != nil {
|
2026-04-01 16:57:33 +00:00
|
|
|
renderShim(w, r, errRender(err))
|
2025-12-16 16:37:53 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
query := db.NewGeoQuery()
|
|
|
|
|
query.Bounds = *bounds
|
|
|
|
|
query.Limit = 100
|
2026-01-05 02:06:34 +00:00
|
|
|
sources, err := platform.MosquitoSourceQuery()
|
2025-12-16 16:37:53 +00:00
|
|
|
if err != nil {
|
2026-04-01 16:57:33 +00:00
|
|
|
renderShim(w, r, errRender(err))
|
2025-12-16 16:37:53 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 16:57:33 +00:00
|
|
|
data := []Renderable{}
|
2026-01-06 22:23:59 +00:00
|
|
|
for _, s := range sources {
|
2025-12-16 16:37:53 +00:00
|
|
|
data = append(data, NewResponseMosquitoSource(s))
|
|
|
|
|
}
|
2026-04-01 16:57:33 +00:00
|
|
|
if err := renderList(w, r, data); err != nil {
|
|
|
|
|
renderShim(w, r, errRender(err))
|
2025-12-16 16:37:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-12 23:49:16 +00:00
|
|
|
func apiTrapData(w http.ResponseWriter, r *http.Request, u platform.User) {
|
2025-12-16 16:37:53 +00:00
|
|
|
bounds, err := parseBounds(r)
|
|
|
|
|
if err != nil {
|
2026-04-01 16:57:33 +00:00
|
|
|
renderShim(w, r, errRender(err))
|
2025-12-16 16:37:53 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
query := db.NewGeoQuery()
|
|
|
|
|
query.Bounds = *bounds
|
|
|
|
|
query.Limit = 100
|
2026-01-05 02:06:34 +00:00
|
|
|
trap_data, err := platform.TrapDataQuery()
|
2025-12-16 16:37:53 +00:00
|
|
|
if err != nil {
|
2026-04-01 16:57:33 +00:00
|
|
|
renderShim(w, r, errRender(err))
|
2025-12-16 16:37:53 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 16:57:33 +00:00
|
|
|
data := []Renderable{}
|
2026-01-06 22:23:59 +00:00
|
|
|
for _, td := range trap_data {
|
2025-12-16 16:37:53 +00:00
|
|
|
data = append(data, NewResponseTrapDatum(td))
|
|
|
|
|
}
|
2026-04-01 16:57:33 +00:00
|
|
|
if err := renderList(w, r, data); err != nil {
|
|
|
|
|
renderShim(w, r, errRender(err))
|
2025-12-16 16:37:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-12 23:49:16 +00:00
|
|
|
func apiServiceRequest(w http.ResponseWriter, r *http.Request, u platform.User) {
|
2025-12-16 16:37:53 +00:00
|
|
|
bounds, err := parseBounds(r)
|
|
|
|
|
if err != nil {
|
2026-04-01 16:57:33 +00:00
|
|
|
renderShim(w, r, errRender(err))
|
2025-12-16 16:37:53 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
query := db.NewGeoQuery()
|
|
|
|
|
query.Bounds = *bounds
|
|
|
|
|
query.Limit = 100
|
2026-01-05 02:06:34 +00:00
|
|
|
requests, err := platform.ServiceRequestQuery()
|
2025-12-16 16:37:53 +00:00
|
|
|
if err != nil {
|
2026-04-01 16:57:33 +00:00
|
|
|
renderShim(w, r, errRender(err))
|
2025-12-16 16:37:53 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 16:57:33 +00:00
|
|
|
data := []Renderable{}
|
2026-01-06 22:23:59 +00:00
|
|
|
for _, sr := range requests {
|
2026-04-14 19:59:32 +00:00
|
|
|
data = append(data, types.ServiceRequestFromModel(sr))
|
2025-12-16 16:37:53 +00:00
|
|
|
}
|
2026-04-01 16:57:33 +00:00
|
|
|
if err := renderList(w, r, data); err != nil {
|
|
|
|
|
renderShim(w, r, errRender(err))
|
2025-12-16 16:37:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parseBounds(r *http.Request) (*db.GeoBounds, error) {
|
|
|
|
|
err := r.ParseForm()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
east := r.FormValue("east")
|
|
|
|
|
north := r.FormValue("north")
|
|
|
|
|
south := r.FormValue("south")
|
|
|
|
|
west := r.FormValue("west")
|
|
|
|
|
|
|
|
|
|
bounds := db.GeoBounds{}
|
|
|
|
|
|
|
|
|
|
var temp float64
|
|
|
|
|
temp, err = strconv.ParseFloat(east, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
bounds.East = temp
|
|
|
|
|
temp, err = strconv.ParseFloat(north, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
bounds.North = temp
|
|
|
|
|
temp, err = strconv.ParseFloat(south, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
bounds.South = temp
|
|
|
|
|
temp, err = strconv.ParseFloat(west, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
bounds.West = temp
|
|
|
|
|
return &bounds, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func webhookFieldseeker(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
// Create or open the log file
|
|
|
|
|
file, err := os.OpenFile("webhook/request.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("Error opening log file: %v", err)
|
|
|
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
|
|
// Write timestamp
|
|
|
|
|
timestamp := time.Now().Format("2006-01-02 15:04:05")
|
|
|
|
|
fmt.Fprintf(file, "\n=== Request logged at %s ===\n", timestamp)
|
|
|
|
|
|
|
|
|
|
// Write request line
|
|
|
|
|
fmt.Fprintf(file, "%s %s %s\n", r.Method, r.RequestURI, r.Proto)
|
|
|
|
|
|
|
|
|
|
// Write all headers
|
|
|
|
|
fmt.Fprintf(file, "\nHeaders:\n")
|
|
|
|
|
for name, values := range r.Header {
|
|
|
|
|
for _, value := range values {
|
|
|
|
|
fmt.Fprintf(file, "%s: %s\n", name, value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write body
|
|
|
|
|
fmt.Fprintf(file, "\nBody:\n")
|
|
|
|
|
body, err := io.ReadAll(r.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("Error reading request body: %v", err)
|
|
|
|
|
fmt.Fprintf(file, "Error reading body: %v\n", err)
|
|
|
|
|
} else {
|
|
|
|
|
file.Write(body)
|
|
|
|
|
if len(body) == 0 {
|
|
|
|
|
fmt.Fprintf(file, "(empty body)")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Fprintf(file, "\n=== End of request ===\n\n")
|
|
|
|
|
|
|
|
|
|
// Extract the crc_token value for the signature portion
|
|
|
|
|
|
|
|
|
|
// Respond with 204 No Content
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-02 08:58:57 -07:00
|
|
|
func parseTime(x string) (*time.Time, error) {
|
2025-12-16 16:37:53 +00:00
|
|
|
created_epoch, err := strconv.ParseInt(x, 10, 64)
|
|
|
|
|
if err != nil {
|
2026-01-02 08:58:57 -07:00
|
|
|
return &time.Time{}, fmt.Errorf("Failed to parse time '%s': %w", x, err)
|
2025-12-16 16:37:53 +00:00
|
|
|
}
|
|
|
|
|
created := time.UnixMilli(created_epoch)
|
2026-01-02 08:58:57 -07:00
|
|
|
return &created, nil
|
2025-12-16 16:37:53 +00:00
|
|
|
}
|
2026-04-21 23:53:42 +00:00
|
|
|
|
|
|
|
|
type about struct {
|
|
|
|
|
Environment string `json:"environment"`
|
|
|
|
|
SentryDSN string `json:"sentry_dsn"`
|
|
|
|
|
Version string `json:"version"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getRoot(ctx context.Context, r *http.Request, q resource.QueryParams) (*about, *nhttp.ErrorWithStatus) {
|
|
|
|
|
return &about{
|
|
|
|
|
Environment: config.Environment,
|
|
|
|
|
SentryDSN: config.SentryDSNFrontend,
|
|
|
|
|
Version: version,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|