Files
butterfliu/main.go
lzw-723 9aec254f23
All checks were successful
Go CI / test-and-build (push) Successful in 11s
添加接口
2026-04-07 16:38:59 +08:00

67 lines
1.9 KiB
Go

package main
import (
"butterfliu/config"
"butterfliu/internal/controller"
"butterfliu/internal/repository"
"butterfliu/internal/service"
"log"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
func main() {
config := config.LoadConfig()
err := InitDB(config.DatabasePath)
if err != nil {
log.Fatal(err)
}
r := chi.NewRouter()
r.Use(middleware.Logger)
libraryRepo := repository.NewLibraryRepository(GetDB())
songRepo := repository.NewSongRepository(GetDB())
mediaRepo := repository.NewMediaRepository(GetDB())
libraryService := service.NewLibraryService(libraryRepo)
songService := service.NewSongService(songRepo, mediaRepo)
libraryController := controller.NewLibraryController(libraryService)
songController := controller.NewSongController(songService)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World!"))
})
r.Route("/api/libraries", func(r chi.Router) {
r.Get("/", libraryController.GetAll)
r.Post("/", libraryController.Create)
r.Get("/{id}", libraryController.GetByID)
r.Delete("/{id}", libraryController.Delete)
r.Put("/{id}/name", libraryController.UpdateName)
r.Put("/{id}/path", libraryController.UpdatePath)
r.Post("/{id}/scan", libraryController.Scan)
r.Get("/{id}/songs", libraryController.GetSongs)
r.Get("/scan-status", libraryController.GetScanStatus)
})
r.Route("/api/songs", func(r chi.Router) {
r.Get("/", songController.GetAllWithDetails)
r.Get("/{id}", songController.GetByIDWithDetails)
r.Get("/{id}/stream", songController.Stream)
r.Get("/{id}/cover", songController.Cover)
})
r.Route("/api/artists", func(r chi.Router) {
r.Get("/", libraryController.GetArtists)
r.Get("/{id}", libraryController.GetArtist)
})
r.Route("/api/albums", func(r chi.Router) {
r.Get("/", libraryController.GetAlbums)
r.Get("/{id}", libraryController.GetAlbum)
})
http.ListenAndServe(":8102", r)
}