stream-server/http/server.go

36 lines
856 B
Go

package http
import (
"net/http"
"strings"
"os"
)
func NewServer(port string) (error) {
server := http.NewServeMux()
server_setup(server)
if err := http.ListenAndServe(":" + port, server); err != nil {
return err
}
return nil
}
func server_setup(server *http.ServeMux) {
server.HandleFunc("/", serve_main_page)
server.HandleFunc("/live/", serve_live_playlist)
}
func serve_live_playlist(w http.ResponseWriter, r *http.Request) {
stream_id := strings.TrimPrefix(r.URL.Path, "/live/")
base_dir, _ := os.UserHomeDir()
stream_playlist_path := base_dir + "/live/" + stream_id + "/stream.m3u8"
if _, err := os.Stat(stream_playlist_path); err != nil {
http.NotFound(w, r)
} else {
http.ServeFile(w, r, stream_playlist_path)
}
}
func serve_main_page(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/index.html")
}