actual changes :3

This commit is contained in:
2025-05-21 01:20:56 +02:00
parent 005ae783c5
commit 396ccc28f4
22 changed files with 3710 additions and 36 deletions

View File

@@ -31,32 +31,58 @@ type Server struct {
Env map[string]string
}
func (s *Server) registerRoutes() {
http.HandleFunc("/api/v1/ping", s.ping)
http.HandleFunc("/api/v1/login", s.login)
func (s *Server) registerRoutes() http.Handler {
mux := http.NewServeMux()
http.HandleFunc("/api/v1/song/{hash}/", s.song)
http.HandleFunc("/api/v1/songs/recents", s.recents)
http.HandleFunc("/api/v1/songs/favorites", s.favorites)
http.HandleFunc("/api/v1/songs/artist", s.aristsSongs)
mux.HandleFunc("/api/v1/ping", s.ping)
mux.HandleFunc("/api/v1/login", s.login)
http.HandleFunc("/api/v1/collection", s.collection)
http.HandleFunc("/api/v1/search/collections", s.collectionSearch)
mux.HandleFunc("/api/v1/song/{hash}/", s.song)
mux.HandleFunc("/api/v1/songs/recent", s.recents)
mux.HandleFunc("/api/v1/songs/favorites", s.favorites)
mux.HandleFunc("/api/v1/songs/artist", s.aristsSongs)
http.HandleFunc("/api/v1/search/active", s.activeSearch)
http.HandleFunc("/api/v1/search/artist", s.artistSearch)
mux.HandleFunc("/api/v1/collection", s.collection)
mux.HandleFunc("/api/v1/search/collections", s.collectionSearch)
http.HandleFunc("/api/v1/audio/{filepath}", s.songFile)
http.HandleFunc("/api/v1/image/{filepath}", s.imageFile)
mux.HandleFunc("/api/v1/search/active", s.activeSearch)
mux.HandleFunc("/api/v1/search/artist", s.artistSearch)
http.HandleFunc("/api/v1/callback", s.callback)
http.Handle("/swagger/", httpSwagger.WrapHandler)
mux.HandleFunc("/api/v1/audio/{filepath}", s.songFile)
mux.HandleFunc("/api/v1/image/{filepath}", s.imageFile)
mux.HandleFunc("/api/v1/callback", s.callback)
mux.Handle("/swagger/", httpSwagger.WrapHandler)
return corsMiddleware(logRequests(mux))
}
func run(s *Server) {
s.registerRoutes()
mux := s.registerRoutes()
fmt.Println("starting server on http://localhost" + s.Port)
log.Fatal(http.ListenAndServe(s.Port, nil))
log.Fatal(http.ListenAndServe(s.Port, mux))
}
func logRequests(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s", r.RemoteAddr, r.Method, r.URL.Path)
next.ServeHTTP(w, r)
})
}
func corsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusNoContent)
return
}
next.ServeHTTP(w, r)
})
}
// ping godoc