Skip to content

Commit

Permalink
xml handler
Browse files Browse the repository at this point in the history
  • Loading branch information
kdudkov committed Nov 10, 2023
1 parent 98b68ca commit 5c0b222
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions cmd/goatak_server/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ package main
import (
"embed"
"encoding/json"
"encoding/xml"
"errors"
"github.com/kdudkov/goatak/pkg/cot"
"net/http"
"path/filepath"
"runtime/pprof"
"sort"
"time"

"github.com/aofei/air"
"github.com/prometheus/client_golang/prometheus/promhttp"

"github.com/kdudkov/goatak/internal/client"
"github.com/kdudkov/goatak/pkg/cot"
"github.com/kdudkov/goatak/pkg/model"
"github.com/kdudkov/goatak/staticfiles"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

//go:embed templates
Expand Down Expand Up @@ -79,6 +80,7 @@ func getAdminApi(app *App, addr string, renderer *staticfiles.Renderer, webtakRo

adminApi.GET("/takproto/1", getWsHandler(app))
adminApi.POST("/cot", getCotPostHandler(app))
adminApi.POST("/cot_xml", getCotXmlPostHandler(app))

if webtakRoot != "" {
adminApi.FILE("/webtak/", filepath.Join(webtakRoot, "index.html"))
Expand Down Expand Up @@ -254,3 +256,29 @@ func getCotPostHandler(app *App) func(req *air.Request, res *air.Response) error
return nil
}
}

func getCotXmlPostHandler(app *App) func(req *air.Request, res *air.Response) error {
return func(req *air.Request, res *air.Response) error {
scope := getStringParam(req, "scope")
if scope == "" {
scope = "test"
}
ev := new(cot.Event)

dec := xml.NewDecoder(req.Body)

if err := dec.Decode(ev); err != nil {
app.Logger.Errorf("cot decode error %s", err)
return err
}

c, err := cot.EventToProto(ev)
if err != nil {
app.Logger.Errorf("cot convert error %s", err)
return err
}
c.Scope = scope
app.NewCotMessage(c)
return nil
}
}

0 comments on commit 5c0b222

Please sign in to comment.