2023-06-23 20:52:52 +00:00
|
|
|
package gotelem
|
|
|
|
|
|
|
|
// this file defines the HTTP handlers and routes.
|
|
|
|
|
|
|
|
import (
|
2023-06-27 23:22:24 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-06-23 20:52:52 +00:00
|
|
|
"net/http"
|
2024-02-18 19:10:27 +00:00
|
|
|
"time"
|
2023-06-23 20:52:52 +00:00
|
|
|
|
2024-02-19 04:41:22 +00:00
|
|
|
"log/slog"
|
|
|
|
|
2023-06-23 20:52:52 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
2023-06-27 23:22:24 +00:00
|
|
|
"github.com/google/uuid"
|
2023-06-30 16:51:06 +00:00
|
|
|
"github.com/kschamplin/gotelem/internal/db"
|
2023-06-23 20:52:52 +00:00
|
|
|
"github.com/kschamplin/gotelem/skylab"
|
|
|
|
"nhooyr.io/websocket"
|
2023-07-06 02:16:12 +00:00
|
|
|
"nhooyr.io/websocket/wsjson"
|
2023-06-23 20:52:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type slogHttpLogger struct {
|
|
|
|
slog.Logger
|
|
|
|
}
|
|
|
|
|
2023-06-30 16:51:06 +00:00
|
|
|
func TelemRouter(log *slog.Logger, broker *Broker, db *db.TelemDb) http.Handler {
|
2023-06-23 20:52:52 +00:00
|
|
|
r := chi.NewRouter()
|
|
|
|
|
2023-06-29 00:23:08 +00:00
|
|
|
r.Use(middleware.RequestID)
|
|
|
|
r.Use(middleware.RealIP)
|
2023-06-30 12:40:50 +00:00
|
|
|
r.Use(middleware.Logger) // TODO: integrate with slog instead of go default logger.
|
2023-06-23 20:52:52 +00:00
|
|
|
r.Use(middleware.Recoverer)
|
|
|
|
|
2023-06-24 05:15:42 +00:00
|
|
|
// heartbeat request.
|
2023-06-23 20:52:52 +00:00
|
|
|
r.Get("/ping", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write([]byte("pong"))
|
|
|
|
})
|
2023-06-24 05:15:42 +00:00
|
|
|
|
2023-06-27 23:22:24 +00:00
|
|
|
r.Mount("/api/v1", apiV1(broker, db))
|
2023-06-23 20:52:52 +00:00
|
|
|
|
|
|
|
// To future residents - you can add new API calls/systems in /api/v2
|
|
|
|
// Don't break anything in api v1! keep legacy code working!
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// define API version 1 routes.
|
2023-06-30 16:51:06 +00:00
|
|
|
func apiV1(broker *Broker, db *db.TelemDb) chi.Router {
|
2023-06-23 20:52:52 +00:00
|
|
|
r := chi.NewRouter()
|
2023-06-30 12:40:50 +00:00
|
|
|
// this API only accepts JSON.
|
|
|
|
r.Use(middleware.AllowContentType("application/json"))
|
|
|
|
// no caching - always get the latest data.
|
2024-02-12 15:45:23 +00:00
|
|
|
// TODO: add a smart short expiry cache for queries that take a while.
|
2023-06-30 12:40:50 +00:00
|
|
|
r.Use(middleware.NoCache)
|
|
|
|
|
2023-06-23 20:52:52 +00:00
|
|
|
r.Get("/schema", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2023-06-30 12:40:50 +00:00
|
|
|
// return the Skylab JSON definitions
|
2023-06-23 20:52:52 +00:00
|
|
|
w.Write([]byte(skylab.SkylabDefinitions))
|
|
|
|
})
|
|
|
|
|
2023-06-27 23:22:24 +00:00
|
|
|
r.Route("/packets", func(r chi.Router) {
|
|
|
|
r.Get("/subscribe", apiV1PacketSubscribe(broker, db))
|
|
|
|
r.Post("/", func(w http.ResponseWriter, r *http.Request) {
|
2024-02-13 16:03:39 +00:00
|
|
|
var pkgs []skylab.BusEvent
|
2023-06-27 23:22:24 +00:00
|
|
|
decoder := json.NewDecoder(r.Body)
|
2023-06-29 00:23:08 +00:00
|
|
|
if err := decoder.Decode(&pkgs); err != nil {
|
2023-06-27 23:22:24 +00:00
|
|
|
w.WriteHeader(http.StatusTeapot)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// we have a list of packets now. let's commit them.
|
|
|
|
db.AddEvents(pkgs...)
|
|
|
|
})
|
2023-06-29 00:23:08 +00:00
|
|
|
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
2024-02-12 15:45:23 +00:00
|
|
|
// this should use http query params to return a list of packets.
|
2023-06-29 00:23:08 +00:00
|
|
|
|
|
|
|
})
|
|
|
|
|
2024-02-18 01:26:13 +00:00
|
|
|
// this is to get a single field
|
2024-02-25 04:57:17 +00:00
|
|
|
r.Get("/{name:[a-z_]+}/{field:[a-z_]+}", apiV1GetValues(db))
|
2023-06-29 00:23:08 +00:00
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
// records are driving segments/runs.
|
|
|
|
r.Route("/records", func(r chi.Router) {
|
2023-06-30 12:40:50 +00:00
|
|
|
r.Get("/", apiV1GetRecords(db)) // get all runs
|
|
|
|
r.Get("/active", apiV1GetActiveRecord(db)) // get current run (no end time)
|
|
|
|
r.Post("/", apiV1StartRecord(db)) // create a new run (with note). Ends active run if any, and creates new active run (no end time)
|
|
|
|
r.Get("/{id}", apiV1GetRecord(db)) // get details on a specific run
|
|
|
|
r.Put("/{id}", apiV1UpdateRecord(db)) // update a specific run. Can only be used to add notes/metadata, and not to change time/id.
|
2023-06-29 00:23:08 +00:00
|
|
|
|
2023-06-27 23:22:24 +00:00
|
|
|
})
|
|
|
|
|
2023-09-19 19:17:22 +00:00
|
|
|
r.Get("/stats", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
}) // v1 api stats (calls, clients, xbee connected, meta health ok)
|
2023-06-27 23:22:24 +00:00
|
|
|
|
2023-06-23 20:52:52 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2023-06-27 23:22:24 +00:00
|
|
|
// apiV1Subscriber is a websocket session for the v1 api.
|
|
|
|
type apiV1Subscriber struct {
|
2024-02-12 20:38:01 +00:00
|
|
|
nameFilter []string // names of packets we care about.
|
2023-06-27 23:22:24 +00:00
|
|
|
}
|
|
|
|
|
2024-02-12 15:45:23 +00:00
|
|
|
// this is a websocket stream.
|
2023-06-30 16:51:06 +00:00
|
|
|
func apiV1PacketSubscribe(broker *Broker, db *db.TelemDb) http.HandlerFunc {
|
2023-06-27 23:22:24 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
conn_id := r.RemoteAddr + uuid.New().String()
|
|
|
|
sub, err := broker.Subscribe(conn_id)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
fmt.Fprintf(w, "error subscribing: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer broker.Unsubscribe(conn_id)
|
|
|
|
// attempt to upgrade.
|
|
|
|
c, err := websocket.Accept(w, r, nil)
|
2023-07-01 03:08:06 +00:00
|
|
|
c.Ping(r.Context())
|
2023-06-27 23:22:24 +00:00
|
|
|
if err != nil {
|
2023-06-29 00:23:08 +00:00
|
|
|
// TODO: is this the correct option?
|
2023-06-27 23:22:24 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
fmt.Fprintf(w, "error ws handshake: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-29 00:23:08 +00:00
|
|
|
// TODO: use K/V with session token?
|
2023-06-27 23:22:24 +00:00
|
|
|
sess := &apiV1Subscriber{}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2023-06-29 00:23:08 +00:00
|
|
|
case <-r.Context().Done():
|
2023-06-27 23:22:24 +00:00
|
|
|
return
|
|
|
|
case msgIn := <-sub:
|
2024-02-12 20:38:01 +00:00
|
|
|
if len(sess.nameFilter) == 0 {
|
2023-06-27 23:22:24 +00:00
|
|
|
// send it.
|
2023-07-06 02:16:12 +00:00
|
|
|
wsjson.Write(r.Context(), c, msgIn)
|
2023-06-27 23:22:24 +00:00
|
|
|
}
|
2024-02-12 20:38:01 +00:00
|
|
|
for _, name := range sess.nameFilter {
|
|
|
|
if name == msgIn.Name {
|
2023-06-27 23:22:24 +00:00
|
|
|
// send it
|
2024-02-12 20:38:01 +00:00
|
|
|
wsjson.Write(r.Context(), c, msgIn)
|
2023-07-06 02:16:12 +00:00
|
|
|
break
|
2023-06-27 23:22:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2023-06-30 12:40:50 +00:00
|
|
|
|
2024-02-24 22:48:19 +00:00
|
|
|
// apiV1GetValues is a function that creates a handler for
|
|
|
|
// getting the specific value from a packet.
|
|
|
|
// this is useful for OpenMCT or other viewer APIs
|
|
|
|
func apiV1GetValues(db *db.TelemDb) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// we need a start and end time. If none is provided,
|
|
|
|
// we use unix epoch as start, and now + 1 day as end.
|
|
|
|
start := time.Unix(0, 0)
|
|
|
|
startString := r.URL.Query().Get("start")
|
|
|
|
if startString != "" {
|
|
|
|
start, err = time.Parse(time.RFC3339, startString)
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end := time.Now().Add(1 * time.Hour)
|
|
|
|
endParam := r.URL.Query().Get("start")
|
|
|
|
if endParam != "" {
|
|
|
|
end, err = time.Parse(time.RFC3339, endParam)
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
name := chi.URLParam(r, "name")
|
|
|
|
field := chi.URLParam(r, "field")
|
|
|
|
|
|
|
|
// TODO: add limit and pagination
|
|
|
|
|
|
|
|
res, err := db.GetValues(r.Context(), name, field, start, end)
|
|
|
|
if err != nil {
|
|
|
|
// 500 server error:
|
|
|
|
fmt.Print(err)
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(res)
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-06-30 12:40:50 +00:00
|
|
|
// TODO: rename. record is not a clear name. Runs? drives? segments?
|
2023-06-30 16:51:06 +00:00
|
|
|
func apiV1GetRecords(db *db.TelemDb) http.HandlerFunc {
|
2023-06-30 12:40:50 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-30 16:51:06 +00:00
|
|
|
func apiV1GetActiveRecord(db *db.TelemDb) http.HandlerFunc {
|
2023-06-30 12:40:50 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-30 16:51:06 +00:00
|
|
|
func apiV1StartRecord(db *db.TelemDb) http.HandlerFunc {
|
2023-06-30 12:40:50 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {}
|
|
|
|
}
|
|
|
|
|
2023-06-30 16:51:06 +00:00
|
|
|
func apiV1GetRecord(db *db.TelemDb) http.HandlerFunc {
|
2023-06-30 12:40:50 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {}
|
|
|
|
}
|
|
|
|
|
2023-06-30 16:51:06 +00:00
|
|
|
func apiV1UpdateRecord(db *db.TelemDb) http.HandlerFunc {
|
2023-06-30 12:40:50 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {}
|
|
|
|
}
|