X-Git-Url: https://code.octet-stream.net/broadcaster/blobdiff_plain/33a19d553807d171f6ba9f4dafe30f43bc4bab5e..7423c6c97eb5d6dc063e7185c50137bbb5e25a23:/broadcaster-server/main.go diff --git a/broadcaster-server/main.go b/broadcaster-server/main.go index 079f977..5a3cbb9 100644 --- a/broadcaster-server/main.go +++ b/broadcaster-server/main.go @@ -2,7 +2,7 @@ package main import ( "bufio" - "embed" + _ "embed" "flag" "fmt" "golang.org/x/net/websocket" @@ -20,8 +20,9 @@ import ( const version = "v1.0.0" const formatString = "2006-01-02T15:04" -//go:embed templates/* -var content embed.FS +// //go:embed templates/* +//var content embed.FS +var content = os.DirFS("../broadcaster-server/") var config ServerConfig = NewServerConfig() @@ -98,18 +99,45 @@ func main() { } } +type HeaderData struct { + SelectedMenu string + Username string +} + +func renderHeader(w http.ResponseWriter, selectedMenu string) { + tmpl := template.Must(template.ParseFS(content, "templates/header.html")) + data := HeaderData{ + SelectedMenu: selectedMenu, + Username: "username", + } + err := tmpl.Execute(w, data) + if err != nil { + log.Fatal(err) + } +} + +func renderFooter(w http.ResponseWriter) { + tmpl := template.Must(template.ParseFS(content, "templates/footer.html")) + err := tmpl.Execute(w, nil) + if err != nil { + log.Fatal(err) + } +} + type HomeData struct { LoggedIn bool Username string } func homePage(w http.ResponseWriter, r *http.Request) { + renderHeader(w, "status") tmpl := template.Must(template.ParseFS(content, "templates/index.html")) data := HomeData{ LoggedIn: true, Username: "Bob", } tmpl.Execute(w, data) + renderFooter(w) } type LogInData struct { @@ -136,9 +164,10 @@ func logInPage(w http.ResponseWriter, r *http.Request) { data := LogInData{ Error: errText, } - + renderHeader(w, "") tmpl := template.Must(template.ParseFS(content, "templates/login.html")) tmpl.Execute(w, data) + renderFooter(w) } func playlistSection(w http.ResponseWriter, r *http.Request) { @@ -244,11 +273,13 @@ func changePasswordPage(w http.ResponseWriter, r *http.Request) { data.Message = "" data.ShowForm = true } + renderHeader(w, "change-password") tmpl := template.Must(template.ParseFS(content, "templates/change_password.html")) err = tmpl.Execute(w, data) if err != nil { log.Fatal(err) } + renderFooter(w) } type PlaylistsPageData struct { @@ -256,6 +287,7 @@ type PlaylistsPageData struct { } func playlistsPage(w http.ResponseWriter, _ *http.Request) { + renderHeader(w, "playlists") data := PlaylistsPageData{ Playlists: db.GetPlaylists(), } @@ -264,6 +296,7 @@ func playlistsPage(w http.ResponseWriter, _ *http.Request) { if err != nil { log.Fatal(err) } + renderFooter(w) } type RadiosPageData struct { @@ -271,6 +304,7 @@ type RadiosPageData struct { } func radiosPage(w http.ResponseWriter, _ *http.Request) { + renderHeader(w, "radios") data := RadiosPageData{ Radios: db.GetRadios(), } @@ -279,6 +313,7 @@ func radiosPage(w http.ResponseWriter, _ *http.Request) { if err != nil { log.Fatal(err) } + renderFooter(w) } type EditPlaylistPageData struct { @@ -306,8 +341,10 @@ func editPlaylistPage(w http.ResponseWriter, r *http.Request, id int) { data.Playlist = playlist data.Entries = db.GetEntriesForPlaylist(id) } + renderHeader(w, "radios") tmpl := template.Must(template.ParseFS(content, "templates/playlist.html")) tmpl.Execute(w, data) + renderFooter(w) } func submitPlaylist(w http.ResponseWriter, r *http.Request) { @@ -393,8 +430,10 @@ func editRadioPage(w http.ResponseWriter, r *http.Request, id int) { } data.Radio = radio } + renderHeader(w, "radios") tmpl := template.Must(template.ParseFS(content, "templates/radio.html")) tmpl.Execute(w, data) + renderFooter(w) } func submitRadio(w http.ResponseWriter, r *http.Request) { @@ -434,6 +473,7 @@ type FilesPageData struct { } func filesPage(w http.ResponseWriter, _ *http.Request) { + renderHeader(w, "files") data := FilesPageData{ Files: files.Files(), } @@ -443,6 +483,7 @@ func filesPage(w http.ResponseWriter, _ *http.Request) { if err != nil { log.Fatal(err) } + renderFooter(w) } func deleteFile(w http.ResponseWriter, r *http.Request) { @@ -473,8 +514,10 @@ func uploadFile(w http.ResponseWriter, r *http.Request) { func logOutPage(w http.ResponseWriter, r *http.Request) { clearSessionCookie(w) + renderHeader(w, "") tmpl := template.Must(template.ParseFS(content, "templates/logout.html")) tmpl.Execute(w, nil) + renderFooter(w) } func stopPage(w http.ResponseWriter, r *http.Request) {