call upload

This commit is contained in:
Daniel 2024-07-24 14:07:24 -04:00
parent b5c0781631
commit 2a02b88822
2 changed files with 58 additions and 8 deletions

View file

@ -2,17 +2,19 @@ package server
import ( import (
"fmt" "fmt"
"io"
"net/http" "net/http"
"reflect"
"time" "time"
"github.com/go-chi/render"
) )
//const timeFormat = "2006-01-02T15:04:05.999Z07:00"
type callUploadRequest struct { type callUploadRequest struct {
Audio []byte `form:"audio"` Audio []byte `form:"audio"`
AudioName string `form:"audioName"` AudioName string `form:"audioName"`
AudioType time.Time `form:"audioType"` AudioType string `form:"audioType"`
DateTime string `form:"dateTime"` DateTime time.Time `form:"dateTime"`
Frequencies []int `form:"frequencies"` Frequencies []int `form:"frequencies"`
Frequency int `form:"frequency"` Frequency int `form:"frequency"`
Key string `form:"key"` Key string `form:"key"`
@ -32,13 +34,50 @@ func (car *callUploadRequest) Bind(r *http.Request) error {
} }
func (s *Server) routeCallUpload(w http.ResponseWriter, r *http.Request) { func (s *Server) routeCallUpload(w http.ResponseWriter, r *http.Request) {
callUpload := new(callUploadRequest) call := new(callUploadRequest)
err := render.Bind(r, callUpload) err := fillFields(r, call)
if err != nil { if err != nil {
http.Error(w, "cannot bind upload", 500) http.Error(w, "cannot bind upload "+r.Header.Get("Content-Type")+err.Error(), 500)
return return
} }
fmt.Printf("%#v\n", callUpload) fmt.Printf("%#v\n", call)
w.Write([]byte("yay")) w.Write([]byte("yay"))
} }
func fillFields(r *http.Request, v *callUploadRequest) error {
rv := reflect.ValueOf(v).Elem()
rt := rv.Type()
for i := 0; i < rv.NumField(); i++ {
f := rv.Field(i)
ff := rt.Field(i).Tag.Get("form")
switch ff {
case "audio":
file, _, err := r.FormFile(ff)
if err != nil {
return err
}
audioBytes, err := io.ReadAll(file)
if err != nil {
return err
}
f.SetBytes(audioBytes)
case "dateTime":
t, err := time.Parse(time.RFC3339, r.Form.Get(ff))
if err != nil {
return err
}
f.Set(reflect.ValueOf(t))
case "frequencies":
case "frequency", "talkgroup":
case "patches", "sources":
default:
f.SetString(r.Form.Get(ff))
}
}
return nil
}

View file

@ -1,6 +1,8 @@
package server package server
import ( import (
"fmt"
"io"
"net/http" "net/http"
"time" "time"
@ -14,6 +16,7 @@ import (
func (s *Server) setupRoutes() { func (s *Server) setupRoutes() {
r := s.r r := s.r
r.Use(middleware.Recoverer)
r.Use(middleware.WithValue(database.DBCTXKeyValue, s.db)) r.Use(middleware.WithValue(database.DBCTXKeyValue, s.db))
r.Group(func(r chi.Router) { r.Group(func(r chi.Router) {
@ -25,6 +28,7 @@ func (s *Server) setupRoutes() {
r.Group(func(r chi.Router) { r.Group(func(r chi.Router) {
r.Use(rateLimiter()) r.Use(rateLimiter())
r.Use(render.SetContentType(render.ContentTypeJSON)) r.Use(render.SetContentType(render.ContentTypeJSON))
// r.Use(teeRequest())
// public routes // public routes
r.Post("/auth", s.routeAuth) r.Post("/auth", s.routeAuth)
r.Post("/api/call-upload", s.routeCallUpload) r.Post("/api/call-upload", s.routeCallUpload)
@ -40,6 +44,13 @@ func (s *Server) setupRoutes() {
}) })
} }
func teeRequest() func(http.Handler) http.Handler {
return middleware.New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
by, err := io.ReadAll(r.Body)
fmt.Println(string(by), err)
}))
}
func rateLimiter() func(http.Handler) http.Handler { func rateLimiter() func(http.Handler) http.Handler {
return httprate.LimitByRealIP(100, 1*time.Minute) return httprate.LimitByRealIP(100, 1*time.Minute)
} }