Skip to content

Commit

Permalink
darkmode config switch (fixes #3)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwoytek committed May 16, 2023
1 parent 5a6d4e5 commit 9d44261
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ type timerConfig struct {
}

type config struct {
Osc map[string]string
Web map[string]string
Timers []timerConfig
Osc map[string]string
Web map[string]string
Darkmode bool
Timers []timerConfig
}

func main() {
Expand Down Expand Up @@ -123,7 +124,7 @@ func main() {
if err != nil {
log.Fatalf("Unable to parse port for web server from '%s': %s", Config.Web["port"], err)
}
runWebServer(Config.Web["bind"], webPort, ctx)
runWebServer(Config.Web["bind"], webPort, Config.Darkmode, ctx)

oscPort, err := strconv.Atoi(Config.Osc["port"])
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion timers.html → templates/base.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<!doctype html>
<html lang="en" data-bs-theme="dark">
{{block "theme" .}}
<html lang="en" data-bs-theme="light">
{{end}}
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
Expand Down
3 changes: 3 additions & 0 deletions templates/darkmode.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{define "theme"}}
<html lang="en" data-bs-theme="dark">
{{end}}
12 changes: 10 additions & 2 deletions webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"html/template"
"log"
"net/http"
"path/filepath"
"strings"
)

Expand All @@ -19,7 +20,10 @@ type timerValue struct {
Running bool `json:"running"`
}

func runWebServer(bindAddr string, bindPort int, ctx context.Context) {
var dm bool

func runWebServer(bindAddr string, bindPort int, darkmode bool, ctx context.Context) {
dm = darkmode
staticServer := http.FileServer(http.Dir("./static"))
http.Handle("/static/", http.StripPrefix("/static/", staticServer))
http.HandleFunc("/timer/", timerValueHandler)
Expand All @@ -43,7 +47,11 @@ func runWebServer(bindAddr string, bindPort int, ctx context.Context) {
}

func webHandler(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("timers.html")
files := []string{filepath.Join("templates", "base.html")}
if dm {
files = append(files, filepath.Join("templates", "darkmode.html"))
}
t, err := template.ParseFiles(files...)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down

0 comments on commit 9d44261

Please sign in to comment.