-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
39 lines (31 loc) · 970 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
"fmt"
"log"
"mime"
"net/http"
"path/filepath"
)
func main() {
const songsDir = "songs"
const port = 8080
// Register custom MIME types for HLS streaming
mime.AddExtensionType(".m3u8", "application/vnd.apple.mpegurl")
mime.AddExtensionType(".ts", "video/MP2T")
// Serve files with proper headers
http.Handle("/", addHeaders(http.FileServer(http.Dir(songsDir))))
fmt.Printf("Starting server on %v\n", port)
log.Printf("Serving %s on HTTP port: %v\n", songsDir, port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", port), nil))
}
// Middleware to add headers for CORS and set MIME types dynamically
func addHeaders(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
ext := filepath.Ext(r.URL.Path)
if mimeType := mime.TypeByExtension(ext); mimeType != "" {
w.Header().Set("Content-Type", mimeType)
}
h.ServeHTTP(w, r)
}
}