From f0cff35bea0eb25fdaf33992dcce06de11291766 Mon Sep 17 00:00:00 2001 From: Daniel Aberger Date: Thu, 19 Jan 2017 22:49:17 +0100 Subject: [PATCH] Functional web interface --- static/css/style.css | 71 -------------------------- templates/footer.html | 11 ++++ templates/{layout.html => header.html} | 23 ++++----- templates/home.html | 2 +- templates/internal.html | 10 ++-- templates/itemrow.html | 5 ++ webserver.go | 57 ++++++++++++++++++--- 7 files changed, 81 insertions(+), 98 deletions(-) delete mode 100644 static/css/style.css create mode 100644 templates/footer.html rename templates/{layout.html => header.html} (87%) create mode 100644 templates/itemrow.html diff --git a/static/css/style.css b/static/css/style.css deleted file mode 100644 index dda9193..0000000 --- a/static/css/style.css +++ /dev/null @@ -1,71 +0,0 @@ -/* http://meyerweb.com/eric/tools/css/reset/ - v2.0 | 20110126 - License: none (public domain) -*/ - -html, body, div, span, applet, object, iframe, -h1, h2, h3, h4, h5, h6, p, blockquote, pre, -a, abbr, acronym, address, big, cite, code, -del, dfn, em, img, ins, kbd, q, s, samp, -small, strike, strong, sub, sup, tt, var, -b, u, i, center, -dl, dt, dd, ol, ul, li, -fieldset, form, label, legend, -table, caption, tbody, tfoot, thead, tr, th, td, -article, aside, canvas, details, embed, -figure, figcaption, footer, header, hgroup, -menu, nav, output, ruby, section, summary, -time, mark, audio, video { - margin: 0; - padding: 0; - border: 0; - font-size: 100%; - font: inherit; - vertical-align: baseline; -} -/* HTML5 display-role reset for older browsers */ -article, aside, details, figcaption, figure, -footer, header, hgroup, menu, nav, section { - display: block; -} -body { - line-height: 1; -} -ol, ul { - list-style: none; -} -blockquote, q { - quotes: none; -} -blockquote:before, blockquote:after, -q:before, q:after { - content: ''; - content: none; -} -table { - border-collapse: collapse; - border-spacing: 0; -} - -a { - text-decoration: none; -} - -a:hover { - text-decoration: underline; -} - -body { - font-size: 13px; - font-family: 'Open Sans', sans-serif; - padding: 0 30px; -} - -header { - padding: 30px 0; - font-size: 15px; -} - -header h1 { - font-size: 20px; -} diff --git a/templates/footer.html b/templates/footer.html new file mode 100644 index 0000000..8bef535 --- /dev/null +++ b/templates/footer.html @@ -0,0 +1,11 @@ +{{ define "footer" }} + + + + + + + + + +{{ end }} diff --git a/templates/layout.html b/templates/header.html similarity index 87% rename from templates/layout.html rename to templates/header.html index 91807f5..848fa95 100644 --- a/templates/layout.html +++ b/templates/header.html @@ -1,4 +1,4 @@ -{{ define "base" }} +{{ define "header" }} @@ -15,6 +15,11 @@ body { padding-top: 50px; } + .vcenter { + display: inline-block; + vertical-align: middle; + float: none; +} @@ -46,17 +51,9 @@
- -
+

Gidbig

-

{{ template "content" . }}

-
+

{{ template "sessionHandler" . }}

+ {{ end }} -
- - - - - - -{{ end }} + diff --git a/templates/home.html b/templates/home.html index bb29ff8..ad7174d 100644 --- a/templates/home.html +++ b/templates/home.html @@ -1,3 +1,3 @@ -{{ define "content" }} +{{ define "sessionHandler" }} Login with Discord {{ end }} diff --git a/templates/internal.html b/templates/internal.html index 9b31fa7..241e382 100644 --- a/templates/internal.html +++ b/templates/internal.html @@ -1,12 +1,12 @@ -{{ define "content" }} +{{ define "sessionHandler" }} Logout
-Play sound {{ end }} +{{ end }} diff --git a/templates/itemrow.html b/templates/itemrow.html new file mode 100644 index 0000000..c52940a --- /dev/null +++ b/templates/itemrow.html @@ -0,0 +1,5 @@ +
+{{ range . }} +
{{ .Itemtext }}
Play!
+{{ end }} +
diff --git a/webserver.go b/webserver.go index 15236b7..7917a4f 100644 --- a/webserver.go +++ b/webserver.go @@ -14,8 +14,9 @@ import ( ) const ( - defaultLayout = "templates/layout.html" - templateDir = "templates/" + header = "templates/header.html" + footer = "templates/footer.html" + templateDir = "templates/" ) var ( @@ -37,10 +38,17 @@ var ( store *sessions.CookieStore ) +type SoundItem struct { + Itemcommand string + Itemsoundname string + Itemtext string +} + // startWebServer func startWebServer(port string, ci string, cs string, redirectURL string) { - tmpls["home.html"] = template.Must(template.ParseFiles(templateDir+"home.html", defaultLayout)) - tmpls["internal.html"] = template.Must(template.ParseFiles(templateDir+"internal.html", defaultLayout)) + tmpls["home.html"] = template.Must(template.ParseFiles(templateDir+"home.html", header, footer)) + tmpls["internal.html"] = template.Must(template.ParseFiles(templateDir+"internal.html", header, footer)) + tmpls["itemrow.html"] = template.Must(template.ParseFiles(templateDir + "itemrow.html")) store = sessions.NewCookieStore([]byte(cs)) discordOauthConfig.ClientID = ci discordOauthConfig.ClientSecret = cs @@ -52,8 +60,6 @@ func startWebServer(port string, ci string, cs string, redirectURL string) { r.HandleFunc("/discordLogin", handlediscordLogin) r.HandleFunc("/discordCallback", handlediscordCallback) r.HandleFunc("/playsound", handlePlaySound) - - http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) http.Handle("/", r) http.ListenAndServe(":"+port, nil) } @@ -79,10 +85,45 @@ func handlePlaySound(w http.ResponseWriter, r *http.Request) { func handleMain(w http.ResponseWriter, r *http.Request) { session, _ := store.Get(r, "session-name") if session.Values["discordUsername"] != nil { - tmpls["internal.html"].ExecuteTemplate(w, "base", map[string]interface{}{}) + err := tmpls["internal.html"].ExecuteTemplate(w, "header", map[string]interface{}{}) + if err != nil { + fmt.Println(err) + return + } + var si [4]SoundItem + cnt := 0 + for _, sc := range COLLECTIONS { + for _, snd := range sc.Sounds { + si[cnt%4].Itemcommand = "!" + sc.Prefix + si[cnt%4].Itemsoundname = snd.Name + si[cnt%4].Itemtext = si[cnt%4].Itemcommand + " " + si[cnt%4].Itemsoundname + if cnt != 0 && cnt%4 == 3 { + err = tmpls["itemrow.html"].Execute(w, si) + if err != nil { + fmt.Println(err) + return + } + } + cnt++ + } + } + if cnt%4 != 0 { + err = tmpls["itemrow.html"].Execute(w, si) + if err != nil { + fmt.Println(err) + return + } + } + + err = tmpls["internal.html"].ExecuteTemplate(w, "footer", map[string]interface{}{}) + if err != nil { + fmt.Println(err) + return + } return } - tmpls["home.html"].ExecuteTemplate(w, "base", map[string]interface{}{}) + tmpls["home.html"].ExecuteTemplate(w, "header", map[string]interface{}{}) + tmpls["home.html"].ExecuteTemplate(w, "footer", map[string]interface{}{}) } func handleLogout(w http.ResponseWriter, r *http.Request) { cookie := &http.Cookie{