Introduce chi router

chi
Gabriel Belinsky 2 years ago
parent a2753fa627
commit 60c790e068
  1. 6
      go.mod
  2. 18
      internal/apiServer/apiServer.go

@ -2,8 +2,12 @@ module dread.land/deepgram-demo
go 1.19
require (
github.com/go-audio/wav v1.1.0
github.com/go-chi/chi/v5 v5.0.10
)
require (
github.com/go-audio/audio v1.0.0 // indirect
github.com/go-audio/riff v1.0.0 // indirect
github.com/go-audio/wav v1.1.0
)

@ -11,6 +11,7 @@ import (
"dread.land/deepgram-demo/internal/audio"
"dread.land/deepgram-demo/internal/metadata"
"github.com/go-chi/chi/v5"
)
type ApiServer struct {
@ -49,7 +50,7 @@ func (apiServer *ApiServer) filesHandler(w http.ResponseWriter, r *http.Request)
}
func (apiServer *ApiServer) metadataHandler(w http.ResponseWriter, r *http.Request) {
filename := path.Base(r.URL.Path)
filename := path.Base(path.Dir(r.URL.Path))
switch r.Method {
case "GET":
// get metadata
@ -93,14 +94,11 @@ func (apiServer *ApiServer) listHandler(w http.ResponseWriter, r *http.Request)
func (apiServer *ApiServer) Serve() {
// POST and GET media
http.HandleFunc("/files/", apiServer.filesHandler)
// list media
http.HandleFunc("/files", apiServer.listHandler)
// list metadata (with filter)
http.HandleFunc("/metadata", apiServer.listHandler)
// GET metadata
http.HandleFunc("/metadata/", apiServer.metadataHandler)
r := chi.NewRouter()
r.Get("/files", apiServer.listHandler)
r.Get("/files/{filename}", apiServer.filesHandler)
r.Post("/files/{filename}", apiServer.filesHandler)
r.Get("/files/{filename}/metadata", apiServer.metadataHandler)
log.Fatal(http.ListenAndServe(":3030", nil))
log.Fatal(http.ListenAndServe(":3030", r))
}

Loading…
Cancel
Save