shelfy/internal/route/main.go

140 lines
4.7 KiB
Go
Raw Normal View History

2025-06-06 07:42:55 +00:00
package route
import (
"app/shelfly/internal/download"
"app/shelfly/internal/library"
"app/shelfly/internal/login"
"app/shelfly/internal/users"
"app/shelfly/renders"
"fmt"
"log"
"net/http"
"time"
2025-06-18 14:18:10 +00:00
"os"
"path/filepath"
2025-06-06 07:42:55 +00:00
"github.com/gorilla/mux"
"gorm.io/gorm"
)
2025-06-18 14:15:29 +00:00
2025-06-06 07:42:55 +00:00
type spaHandler struct {
staticPath string
indexPath string
}
// Routes non protégées
func RoutesPublic(r *mux.Router, bd *gorm.DB) {
2025-06-18 14:15:29 +00:00
// Fichiers statiques (CSS, JS, etc.)
staticDir := "./templates/assets/"
r.PathPrefix("/templates/assets/").Handler(
http.StripPrefix("/templates/assets/", http.FileServer(http.Dir(staticDir))),
)
// Page de login
r.HandleFunc("/login", renders.Login)
// Endpoint d'API pour se logger
r.HandleFunc("/api/login", login.LoginHandler(bd)).Methods("POST")
r.HandleFunc("/api/scan/{id}", library.ScanFolder(bd)).Methods("GET")
r.HandleFunc("/api/download/stream", renders.HandleJobsStream(bd))
r.HandleFunc("/playlist.m3u", func(w http.ResponseWriter, r *http.Request) {
uploadDir := "/app/upload"
w.Header().Set("Content-Type", "audio/x-mpegurl")
fmt.Fprintln(w, "#EXTM3U")
err := filepath.Walk(uploadDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
// On construit l'URL HTTP complète
relPath, _ := filepath.Rel(uploadDir, path)
// Important : remplacer \ par / pour Windows
relPath = filepath.ToSlash(relPath)
2025-06-18 14:19:51 +00:00
fileURL := fmt.Sprintf("http://%s:4000/upload/%s", relPath)
2025-06-18 14:15:29 +00:00
fmt.Fprintln(w, fileURL)
return nil
})
if err != nil {
http.Error(w, "Erreur lors de la génération de la playlist", http.StatusInternalServerError)
}
})
2025-06-06 07:42:55 +00:00
}
// Routes protégées
func RoutesProtected(r *mux.Router, bd *gorm.DB) {
2025-06-18 14:15:29 +00:00
// Ici on place les vues et API qui doivent être protégées
2025-06-06 07:42:55 +00:00
r.HandleFunc("/stream", StreamHandler)
2025-06-18 14:15:29 +00:00
r.HandleFunc("/dashboard", renders.Dashboard(bd))
2025-06-06 07:42:55 +00:00
r.HandleFunc("/settings", renders.Settings)
r.HandleFunc("/library", renders.Library)
2025-06-18 14:15:29 +00:00
r.HandleFunc("/menuLibary", renders.Library)
r.HandleFunc("/godownloader/downloads", renders.GoDownload)
r.HandleFunc("/godownloader/linkcollectors", renders.GoDownloadLinkCollectors)
r.HandleFunc("/godownloader/settings", renders.GoDownloadSetting(bd))
r.HandleFunc("/godownloader/poll-status", renders.PollStatusHandler(bd))
r.HandleFunc("/godownloader/table-refresh", renders.GoDownloadPartialTable(bd))
r.HandleFunc("/godownloader/settings/delete", renders.GoDownloadSettingDelete(bd))
2025-06-12 08:57:10 +00:00
r.HandleFunc("/api/download/add", renders.HandleAddJob(bd)).Methods("POST")
r.HandleFunc("/api/download/all", renders.HandleListJobsPartial(bd)).Methods("GET")
2025-06-18 14:15:29 +00:00
r.HandleFunc("/downloads", renders.GoDownload2(bd))
r.HandleFunc("/stream/{id}", download.HandleStreamPage()).Methods("GET")
r.HandleFunc("/api/download/start/{id}", renders.HandleStartJob(bd)).Methods("POST")
r.HandleFunc("/api/download/pause/{id}", renders.HandlePauseJob).Methods("POST")
r.HandleFunc("/api/download/resume/{id}", renders.HandleResumeJob(bd)).Methods("POST")
r.HandleFunc("/api/download/delete/{id}", renders.HandleDeleteJob(bd)).Methods("DELETE")
r.HandleFunc("/api/download/delete-multiple", renders.HandleDeleteMultipleJobs(bd)).Methods("POST")
// API user
r.HandleFunc("/api/user/create", users.CreateUser(bd)).Methods("POST")
r.HandleFunc("/api/user/update/{id}", users.UpdateUser(bd)).Methods("PUT")
r.HandleFunc("/api/user/delete/{id}", users.DeleteUser(bd)).Methods("DELETE")
r.HandleFunc("/api/user/all/", users.ReadAllUser(bd)).Methods("GET")
r.HandleFunc("/api/user/{id}", users.FindUserById(bd)).Methods("GET")
// API download
r.HandleFunc("/api/pathDownload/create", download.CreateSavePath(bd)).Methods("POST")
r.HandleFunc("/api/pathDownload/update/{id}", download.UpdateSavePath(bd)).Methods("PUT")
r.HandleFunc("/api/pathDownload/delete/{id}", download.DeleteSavePath(bd)).Methods("DELETE")
r.HandleFunc("/api/pathDownload/all/", download.ReadAllSavePath(bd)).Methods("GET")
//API Check path
r.HandleFunc("/validate-path", download.PathValidationHandler)
//API Scan folder
2025-06-06 07:42:55 +00:00
}
func StreamHandler(w http.ResponseWriter, r *http.Request) {
2025-06-18 14:15:29 +00:00
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "Le streaming nest pas supporté par ce serveur", http.StatusInternalServerError)
return
}
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
// Boucle infinie (ou jusqu'à annulation)
for {
select {
case <-ticker.C:
2025-06-06 07:42:55 +00:00
fmt.Fprintf(w, "data: <p>Message #%d</p>\n\n")
2025-06-18 14:15:29 +00:00
flusher.Flush()
case <-r.Context().Done():
// Le client a probablement fermé la connexion
log.Println("Client déconnecté")
return
}
}
}