Move ingestor to own package

This commit is contained in:
Daniel 2024-07-28 23:22:12 -04:00
parent f05a717e2f
commit 51bd6d8433
3 changed files with 18 additions and 3 deletions

View file

@ -1,4 +1,4 @@
package server package ingestors
import ( import (
"fmt" "fmt"
@ -11,10 +11,22 @@ import (
"dynatron.me/x/stillbox/internal/common" "dynatron.me/x/stillbox/internal/common"
"dynatron.me/x/stillbox/pkg/gordio/database" "dynatron.me/x/stillbox/pkg/gordio/database"
"github.com/go-chi/chi/v5"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
type HTTPIngestor struct {
}
func NewHTTPIngestor() *HTTPIngestor {
return new(HTTPIngestor)
}
func (h *HTTPIngestor) InstallRoutes(r chi.Router) {
r.Post("/api/call-upload", h.routeCallUpload)
}
type callUploadRequest struct { type callUploadRequest struct {
Audio []byte `form:"audio"` Audio []byte `form:"audio"`
AudioName string AudioName string
@ -53,7 +65,7 @@ func (car *callUploadRequest) ToAddCallParams(submitter int) database.AddCallPar
} }
} }
func (s *Server) routeCallUpload(w http.ResponseWriter, r *http.Request) { func (h *HTTPIngestor) routeCallUpload(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(1024 * 1024 * 2) // 2MB err := r.ParseMultipartForm(1024 * 1024 * 2) // 2MB
if err != nil { if err != nil {
http.Error(w, "cannot parse form "+err.Error(), http.StatusBadRequest) http.Error(w, "cannot parse form "+err.Error(), http.StatusBadRequest)

View file

@ -27,7 +27,7 @@ func (s *Server) setupRoutes() {
r.Use(render.SetContentType(render.ContentTypeJSON)) r.Use(render.SetContentType(render.ContentTypeJSON))
// public routes // public routes
r.Post("/auth", s.routeAuth) r.Post("/auth", s.routeAuth)
r.Post("/api/call-upload", s.routeCallUpload) s.hi.InstallRoutes(r)
}) })
r.Group(func(r chi.Router) { r.Group(func(r chi.Router) {

View file

@ -5,6 +5,7 @@ import (
"dynatron.me/x/stillbox/pkg/gordio/config" "dynatron.me/x/stillbox/pkg/gordio/config"
"dynatron.me/x/stillbox/pkg/gordio/database" "dynatron.me/x/stillbox/pkg/gordio/database"
"dynatron.me/x/stillbox/pkg/gordio/ingestors"
"github.com/go-chi/chi/middleware" "github.com/go-chi/chi/middleware"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/go-chi/jwtauth/v5" "github.com/go-chi/jwtauth/v5"
@ -15,6 +16,7 @@ type Server struct {
db *database.DB db *database.DB
r *chi.Mux r *chi.Mux
jwt *jwtauth.JWTAuth jwt *jwtauth.JWTAuth
hi *ingestors.HTTPIngestor
} }
func New(cfg *config.Config) (*Server, error) { func New(cfg *config.Config) (*Server, error) {
@ -29,6 +31,7 @@ func New(cfg *config.Config) (*Server, error) {
db: db, db: db,
r: r, r: r,
jwt: jwtauth.New("HS256", []byte(cfg.JWTSecret), nil), jwt: jwtauth.New("HS256", []byte(cfg.JWTSecret), nil),
hi: ingestors.NewHTTPIngestor(),
} }
r.Use(middleware.RequestID) r.Use(middleware.RequestID)
r.Use(middleware.RealIP) r.Use(middleware.RealIP)