diff --git a/build/Dockerfile b/Dockerfile similarity index 100% rename from build/Dockerfile rename to Dockerfile diff --git a/docs/docker-compose.yml b/docs/docker-compose.yml new file mode 100644 index 0000000..e169494 --- /dev/null +++ b/docs/docker-compose.yml @@ -0,0 +1,28 @@ +# Example of Docker Compose setup using Traefik reverse proxy + +version: '3' + +services: + traefik: + image: traefik:v2.2.11 + restart: always + ports: + - 443:443 + - 80:80 + - 1935:1935 + volumes: + - /var/run/docker.sock:/var/run/docker.sock + - ./traefik_data:/data + command: + # Read docker-compose labels but do not expose by default + - "--providers.docker=true" + - "--providers.docker.exposedbydefault=false" + # Define entrypoints + - "--entrypoints.web.address=:80" + - "--entrypoints.web.http.redirections.entryPoint.to=websecure" + - "--entrypoints.websecure.address=:443" + - "--entrypoints.rtmpsecure.address=:1935" + # Define certificates + - "--certificatesResolvers.mytlschallenge.acme.email=root@example.com" + - "--certificatesResolvers.mytlschallenge.acme.storage=/data/acme.json" + - "--certificatesResolvers.mytlschallenge.acme.httpChallenge.entryPoint=web" diff --git a/init/ghostream.service b/docs/ghostream.service similarity index 100% rename from init/ghostream.service rename to docs/ghostream.service diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c375311 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module gitlab.crans.org/nounous/ghostream + +go 1.13 diff --git a/main.go b/main.go index e92dc23..d82007a 100644 --- a/main.go +++ b/main.go @@ -1,51 +1,9 @@ package main import ( - "os" - "log" - "net/http" - "html/template" + "gitlab.crans.org/nounous/ghostream/web" ) -// Preload templates -var templates = template.Must(template.ParseGlob("web/template/*.tmpl")) - -// Handle site index and viewer pages -func handlerViewer(w http.ResponseWriter, r *http.Request) { - // Remove traling slash - //path := r.URL.Path[1:] - - // Render template - err := templates.ExecuteTemplate(w, "base", nil) - if err != nil { - log.Println(err.Error()) - http.Error(w, "Internal Server Error", http.StatusInternalServerError) - } -} - -// Auth incoming stream -func handleStreamAuth(w http.ResponseWriter, r *http.Request) { - // FIXME POST request only with "name" and "pass" - // if name or pass missing => 400 Malformed request - // else login in against LDAP or static users - http.Error(w, "Not implemented", 400) -} - -// Handle static files -// We do not use http.FileServer as we do not want directory listing -func handleStatic(w http.ResponseWriter, r *http.Request) { - path := "./" + r.URL.Path - if f, err := os.Stat(path); err == nil && !f.IsDir() { - http.ServeFile(w, r, path) - } else { - http.NotFound(w, r) - } -} - func main() { - // Set up HTTP router and server - http.HandleFunc("/", handlerViewer) - http.HandleFunc("/rtmp/auth", handleStreamAuth) - http.HandleFunc("/static/", handleStatic) - log.Fatal(http.ListenAndServe(":8080", nil)) + web.ServeHTTP() } diff --git a/web/web.go b/web/web.go new file mode 100644 index 0000000..b3864b4 --- /dev/null +++ b/web/web.go @@ -0,0 +1,55 @@ +package web + +import ( + "html/template" + "log" + "net/http" + "os" +) + +// Preload templates +var templates = template.Must(template.ParseGlob("web/template/*.tmpl")) + +// Handle site index and viewer pages +func handlerViewer(w http.ResponseWriter, r *http.Request) { + // Remove traling slash + //path := r.URL.Path[1:] + + // Render template + err := templates.ExecuteTemplate(w, "base", nil) + if err != nil { + log.Println(err.Error()) + http.Error(w, "Internal Server Error", http.StatusInternalServerError) + } +} + +// Auth incoming stream +func handleStreamAuth(w http.ResponseWriter, r *http.Request) { + // FIXME POST request only with "name" and "pass" + // if name or pass missing => 400 Malformed request + // else login in against LDAP or static users + http.Error(w, "Not implemented", 400) +} + +// Handle static files +// We do not use http.FileServer as we do not want directory listing +func handleStatic(w http.ResponseWriter, r *http.Request) { + path := "./web/" + r.URL.Path + if f, err := os.Stat(path); err == nil && !f.IsDir() { + http.ServeFile(w, r, path) + } else { + http.NotFound(w, r) + } +} + +func ServeHTTP() { + // Load settings + listen_address := "127.0.0.1:8080" + + // Set up HTTP router and server + http.HandleFunc("/", handlerViewer) + http.HandleFunc("/rtmp/auth", handleStreamAuth) + http.HandleFunc("/static/", handleStatic) + log.Print("Listening on http://" + listen_address) + log.Fatal(http.ListenAndServe(listen_address, nil)) +}