94 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			Go
		
	
	
			
		
		
	
	
			94 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			Go
		
	
	
| package main
 | |
| 
 | |
| import (
 | |
| 	"log"
 | |
| 	"net/http"
 | |
| 	"os"
 | |
| 	"path/filepath"
 | |
| 	"strings"
 | |
| 	"time"
 | |
| )
 | |
| 
 | |
| var (
 | |
| 	port      = os.Getenv("PODCAST_PORT")        // Es: ":8080"
 | |
| 	baseURL   = os.Getenv("PODCAST_BASE_URL")    // Es: "http://mioserver.com"
 | |
| 	audioDir  = os.Getenv("PODCAST_AUDIO_DIR")   // Es: "/data/podcast/audio"
 | |
| 	coversDir = os.Getenv("PODCAST_COVERS_DIR")  // Es: "/data/podcast/covers"
 | |
| 	podTitle  = os.Getenv("PODCAST_TITLE")       // Es: "Il Mio Podcast"
 | |
| 	podAuthor = os.Getenv("PODCAST_AUTHOR")      // Es: "Il Mio Podcast"
 | |
| 	podRights = os.Getenv("PODCAST_COPYRIGHT")   // Es: "Il Mio Podcast"
 | |
| 	podLogo   = os.Getenv("PODCAST_LOGO")        // Es: "Il Mio Podcast"
 | |
| 	podDesc   = os.Getenv("PODCAST_DESCRIPTION") // Es: "Il Mio Podcast"
 | |
| 	podLang   = os.Getenv("PODCAST_LANGUAGE")    // Es: "Il Mio Podcast"
 | |
| )
 | |
| 
 | |
| func init() {
 | |
| 	// Verifica variabili d'ambiente
 | |
| 	for _, v := range []struct {
 | |
| 		name  string
 | |
| 		value string
 | |
| 	}{
 | |
| 		{"PODCAST_PORT", port},
 | |
| 		{"PODCAST_BASE_URL", baseURL},
 | |
| 		{"PODCAST_AUDIO_DIR", audioDir},
 | |
| 		{"PODCAST_COVERS_DIR", coversDir},
 | |
| 		{"PODCAST_TITLE", podTitle},
 | |
| 		{"PODCAST_AUTHOR", podAuthor},
 | |
| 		{"PODCAST_COPYRIGHT", podRights},
 | |
| 		{"PODCAST_LOGO", podLogo},
 | |
| 		{"PODCAST_DESCRIPTION", podDesc},
 | |
| 		{"PODCAST_LANGUAGE", podLang},
 | |
| 	} {
 | |
| 		if v.value == "" {
 | |
| 			log.Fatalf("Variabile d'ambiente mancante: %s", v.name)
 | |
| 		}
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func main() {
 | |
| 
 | |
| 	// Handler principale
 | |
| 	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
 | |
| 		// Verifica se la richiesta è per un file audio o cover
 | |
| 		requestedFile := filepath.Clean(r.URL.Path)
 | |
| 		isAudio := strings.HasPrefix(requestedFile, "/audio/") && strings.HasSuffix(requestedFile, ".mp3")
 | |
| 		isCover := strings.HasPrefix(requestedFile, "/covers/") && strings.HasSuffix(requestedFile, ".jpg")
 | |
| 		isLogo := strings.HasPrefix(requestedFile, "/cover.jpg") && strings.HasSuffix(requestedFile, ".jpg")
 | |
| 		isBadge := strings.HasPrefix(requestedFile, "/covers/") && strings.HasSuffix(requestedFile, ".htm")
 | |
| 
 | |
| 		if isAudio || isCover || isLogo {
 | |
| 			// Servi il file richiesto
 | |
| 			http.ServeFile(w, r, filepath.Join(".", requestedFile))
 | |
| 			// logghiamo che succede
 | |
| 			log.Println("Richiesta da: ", r.UserAgent(), " per ", r.RequestURI)
 | |
| 			return
 | |
| 		}
 | |
| 
 | |
| 		if isBadge {
 | |
| 			badgeHandler(w, r)
 | |
| 		}
 | |
| 
 | |
| 		// Altrimenti servi sempre l'RSS
 | |
| 		if err := generateRSS(); err != nil {
 | |
| 			http.Error(w, err.Error(), http.StatusInternalServerError)
 | |
| 			// logghiamo che succede
 | |
| 			log.Println("Richiesta da: ", r.UserAgent(), " per ", r.RequestURI, " --> feeds.xml ")
 | |
| 			return
 | |
| 		}
 | |
| 		http.ServeFile(w, r, "feed.xml")
 | |
| 	})
 | |
| 
 | |
| 	// Aggiornamento periodico
 | |
| 	go func() {
 | |
| 		for {
 | |
| 			time.Sleep(5 * time.Minute)
 | |
| 			if err := generateRSS(); err != nil {
 | |
| 				log.Printf("Aggiornamento RSS fallito: %v", err)
 | |
| 			}
 | |
| 		}
 | |
| 	}()
 | |
| 
 | |
| 	log.Printf("Server in ascolto su %s%s", baseURL, port)
 | |
| 	log.Fatal(http.ListenAndServe(port, nil))
 | |
| }
 |