2023-08-21 12:19:31 +05:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2023-08-21 12:39:38 +05:00
|
|
|
"net/http"
|
2023-08-21 13:02:06 +05:00
|
|
|
"strings"
|
|
|
|
"os"
|
2023-08-21 15:11:19 +05:00
|
|
|
"html/template"
|
2023-08-21 12:19:31 +05:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewServer(port string) (error) {
|
2023-08-21 12:39:38 +05:00
|
|
|
server := http.NewServeMux()
|
|
|
|
server_setup(server)
|
|
|
|
if err := http.ListenAndServe(":" + port, server); err != nil {
|
2023-08-21 12:19:31 +05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-21 12:39:38 +05:00
|
|
|
func server_setup(server *http.ServeMux) {
|
2023-08-21 15:11:19 +05:00
|
|
|
server.HandleFunc("/live/", serve_main_page)
|
|
|
|
server.HandleFunc("/list/", serve_live_playlist)
|
|
|
|
server.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
|
|
|
|
server.HandleFunc("/vid/", serve_vid_segment)
|
|
|
|
}
|
|
|
|
|
|
|
|
func serve_vid_segment(w http.ResponseWriter, r *http.Request) {
|
|
|
|
base_dir, _ := os.UserHomeDir()
|
|
|
|
tmp := strings.Split(strings.TrimPrefix(r.URL.Path, "/vid/"), "/")
|
|
|
|
stream_key := tmp[0]
|
|
|
|
segment_file := tmp[1]
|
|
|
|
http.ServeFile(w, r, base_dir + "/live/" + stream_key + "/" + segment_file)
|
2023-08-21 12:19:31 +05:00
|
|
|
}
|
|
|
|
|
2023-08-21 13:02:06 +05:00
|
|
|
func serve_live_playlist(w http.ResponseWriter, r *http.Request) {
|
2023-08-21 15:11:19 +05:00
|
|
|
stream_id := strings.TrimPrefix(r.URL.Path, "/list/")
|
2023-08-21 13:02:06 +05:00
|
|
|
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)
|
2023-08-21 12:19:31 +05:00
|
|
|
}
|
|
|
|
}
|
2023-08-21 13:02:06 +05:00
|
|
|
|
|
|
|
func serve_main_page(w http.ResponseWriter, r *http.Request) {
|
2023-08-21 15:11:19 +05:00
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
page := template.Must(template.ParseFiles("static/index.html"))
|
|
|
|
stream_user := strings.TrimPrefix(r.URL.Path, "/live/")
|
|
|
|
page.Execute(w, stream_user)
|
2023-08-21 13:02:06 +05:00
|
|
|
}
|