Skip to content

Commit

Permalink
Merge pull request #4 from jwoytek/feature/3-light-dark-switch
Browse files Browse the repository at this point in the history
Feature/3 light dark switch (fixes #2 and #3 )
  • Loading branch information
jwoytek authored May 16, 2023
2 parents 1c94bb3 + 9d44261 commit d19636a
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# ignore site config
showtimer.yaml

# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Currently, most setup and startup of the program must be done from the
command line (Windows CMD.EXE, MacOS Terminal, etc.).

1. unzip the bundle in a useful location, like your home directory
2. Edit the `showtimer.yaml` file with your favorite editor
2. Copy `showtimer_example.yaml` to a name of your choice. The default
is `showtimer.yaml`. Edit the file with your favorite editor.
3. Set the port values for the webserver and OSC (or note the defaults)
4. Optionally, if you would like to only listen on a certain interface,
enter the IP address of that interface. This is probably not common.
Expand Down
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
1 change: 1 addition & 0 deletions showtimer.yaml → showtimer_example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ web:
osc:
#bind: 0.0.0.0
port: 8000
darkmode: false
timers:
- name: Act 1
key: act1
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 d19636a

Please sign in to comment.