gotelem/http.go

154 lines
3.8 KiB
Go
Raw Permalink Normal View History

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"
"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-23 20:52:52 +00:00
"github.com/kschamplin/gotelem/skylab"
"golang.org/x/exp/slog"
"nhooyr.io/websocket"
)
type slogHttpLogger struct {
slog.Logger
}
2023-06-27 23:22:24 +00:00
func TelemRouter(log *slog.Logger, broker *JBroker, 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-23 20:52:52 +00:00
r.Use(middleware.Logger) // TODO: integrate with slog
r.Use(middleware.Recoverer)
r.Get("/schema", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
// return the spicy json response.
w.WriteHeader(http.StatusOK)
w.Write([]byte(skylab.SkylabDefinitions))
})
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!
// serve up a local status page.
return r
}
// define API version 1 routes.
2023-06-27 23:22:24 +00:00
func apiV1(broker *JBroker, db *TelemDb) chi.Router {
2023-06-23 20:52:52 +00:00
r := chi.NewRouter()
r.Get("/schema", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
// return the spicy json response.
w.WriteHeader(http.StatusOK)
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) {
var pkgs []skylab.BusEvent
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
return
2023-06-27 23:22:24 +00:00
})
2023-06-29 00:23:08 +00:00
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
// this should use query params to return a list of packets.
})
// this is to get packets by a name.
r.Get("/{name:[a-z_]+}", func(w http.ResponseWriter, r *http.Request) {
})
})
// records are driving segments/runs.
r.Route("/records", func(r chi.Router) {
r.Get("/") // get all runs
r.Get("/active") // get current run (no end time)
r.Post("/") // create a new run (with note). Ends active run if any, and creates new active run (no end time)
r.Get("/{id}") // get details on a specific run
r.Put("/{id}") // update a specific run. Can only be used to add notes/metadata, and not to change time/id.
2023-06-27 23:22:24 +00:00
})
2023-06-29 00:23:08 +00:00
r.Get("/stats") // v1 api stats (calls, clients, xbee connected, meta health ok)
2023-06-27 23:22:24 +00:00
2023-06-29 00:23:08 +00:00
r.
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 {
idFilter []uint64 // list of Ids to subscribe to. If it's empty, subscribes to all.
}
func apiV1PacketSubscribe(broker *JBroker, db *TelemDb) http.HandlerFunc {
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)
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:
if len(sess.idFilter) == 0 {
// send it.
goto escapeFilter
}
for _, id := range sess.idFilter {
if id == msgIn.Id {
// send it
}
}
2023-06-29 00:23:08 +00:00
escapeFilter:
2023-06-27 23:22:24 +00:00
return
}
}
}
}