From a7bddb4ce2a3df8cd0306429b8ce9ddd55c8528b Mon Sep 17 00:00:00 2001 From: Will Fantom Date: Sun, 8 Dec 2019 19:29:23 +0000 Subject: [PATCH 01/11] Structre project for golang Signed-off-by: Will Fantom --- build/Dockerfile.dev | 1 + config.json | 20 ------------------- .../config.sample.json | 0 main.go | 7 +++++++ index.html => templates/dash.html | 0 5 files changed, 8 insertions(+), 20 deletions(-) create mode 100644 build/Dockerfile.dev delete mode 100644 config.json rename config.sample.json => configs/config.sample.json (100%) create mode 100644 main.go rename index.html => templates/dash.html (100%) diff --git a/build/Dockerfile.dev b/build/Dockerfile.dev new file mode 100644 index 0000000..4ef3a41 --- /dev/null +++ b/build/Dockerfile.dev @@ -0,0 +1 @@ +FROM golang:alpine \ No newline at end of file diff --git a/config.json b/config.json deleted file mode 100644 index 6fe812d..0000000 --- a/config.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "title" : "Simple Dash", - "items" : [ - { - "alt" : "Github", - "icon" : "fab fa-github", - "link" : "https://github.com/kutyla-philipp" - }, - { - "alt" : "Twitter", - "icon" : "fab fa-twitter", - "link" : "https://twitter.com/Kukielka_" - }, - { - "alt" : "Docker Hub", - "icon" : "fab fa-docker", - "link" : "https://hub.docker.com/u/kukielka/" - } - ] -} diff --git a/config.sample.json b/configs/config.sample.json similarity index 100% rename from config.sample.json rename to configs/config.sample.json diff --git a/main.go b/main.go new file mode 100644 index 0000000..04e1500 --- /dev/null +++ b/main.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Printf("Hi\n") +} diff --git a/index.html b/templates/dash.html similarity index 100% rename from index.html rename to templates/dash.html From f44a58292013bef5b432ab058e38b43f2fcca092 Mon Sep 17 00:00:00 2001 From: Will Fantom Date: Sun, 8 Dec 2019 19:30:56 +0000 Subject: [PATCH 02/11] Use CDNs for external JS content Signed-off-by: Will Fantom --- templates/dash.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/dash.html b/templates/dash.html index 700913e..fc70f5b 100644 --- a/templates/dash.html +++ b/templates/dash.html @@ -4,7 +4,7 @@ Loading... - + @@ -22,7 +22,7 @@ - + - + + - var resizeTimer; - window.addEventListener("resize", function () { - clearTimeout(resizeTimer); - resizeTimer = setTimeout(function() { - addTriangleTo(homepage); - }, 400); - }) - - addTriangleTo(homepage); - - - + \ No newline at end of file From 4f99cddbbd5d1095c90a31c0cfde5e3ad7d6f72d Mon Sep 17 00:00:00 2001 From: Will Fantom Date: Sun, 8 Dec 2019 19:37:08 +0000 Subject: [PATCH 04/11] Add a basic Dockerfile for building with Docker Signed-off-by: Will Fantom --- build/Dockerfile.dev | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/build/Dockerfile.dev b/build/Dockerfile.dev index 4ef3a41..86b26b6 100644 --- a/build/Dockerfile.dev +++ b/build/Dockerfile.dev @@ -1 +1,8 @@ -FROM golang:alpine \ No newline at end of file +FROM golang:alpine + +WORKDIR /app +COPY ./. . + +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main . + +CMD ["./main"] \ No newline at end of file From 87c3626a7cb338e6a274f7fdc155835441b4a89e Mon Sep 17 00:00:00 2001 From: Will Fantom Date: Sun, 8 Dec 2019 19:41:51 +0000 Subject: [PATCH 05/11] Parse json config from golang backend Signed-off-by: Will Fantom --- main.go | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 04e1500..89966f7 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,46 @@ package main -import "fmt" +import ( + "encoding/json" + "io/ioutil" + "log" + "os" +) + +type DashItem struct { + Name string `json:"alt"` + Icon string `json:"icon"` + Link string `json:"link"` +} + +type DashData struct { + Title string `json:"title"` + Items []DashItem `json:"items"` +} + +func parseConfig() *DashData { + log.Println("Finding Config") + configPath := os.Getenv("CONFIG_DIR") + "/config.json" + if _, err := os.Stat(configPath); err != nil { + log.Fatalf("Can not find config at: %s\n", configPath) + } + configBytes, err := ioutil.ReadFile(configPath) + if err != nil { + log.Fatalf("Can not open config at: %s\n", configPath) + } + var data DashData + err = json.Unmarshal(configBytes, &data) + if err != nil { + log.Fatalf("Can not parse config as json || %s\n", configPath) + } + log.Printf("Found %d items in the config\n", len(data.Items)) + return &data +} func main() { - fmt.Printf("Hi\n") + log.Println("Starting Simple Dash") + + data := parseConfig() + + log.Printf("Data Title: %s\n", data.Title) } From 429ed0b2316bc8a2b9a1fe25f4c717088ea85a71 Mon Sep 17 00:00:00 2001 From: Will Fantom Date: Sun, 8 Dec 2019 19:45:30 +0000 Subject: [PATCH 06/11] Use HTML templates to add items to dash via golang Signed-off-by: Will Fantom --- main.go | 9 +++++++-- templates/dash.html | 30 +++++------------------------- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/main.go b/main.go index 89966f7..28ea67a 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,10 @@ package main import ( "encoding/json" + "html/template" "io/ioutil" "log" + "net/http" "os" ) @@ -40,7 +42,10 @@ func parseConfig() *DashData { func main() { log.Println("Starting Simple Dash") + dashTemplate := template.Must(template.ParseFiles("templates/dash.html")) data := parseConfig() - - log.Printf("Data Title: %s\n", data.Title) + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + dashTemplate.Execute(w, data) + }) + http.ListenAndServe(":80", nil) } diff --git a/templates/dash.html b/templates/dash.html index 22132e6..f504aa9 100644 --- a/templates/dash.html +++ b/templates/dash.html @@ -5,7 +5,8 @@ Loading... - + + + + + + + + + + + +
+
+
+ +
+
+ + +
+
+ +
+
+
+ + + + + + \ No newline at end of file From 7a134db5cbac5ef38753c4b1d8257b6ba55e9469 Mon Sep 17 00:00:00 2001 From: Will Fantom Date: Tue, 10 Dec 2019 16:53:28 +0000 Subject: [PATCH 08/11] Parse user and allow for simple logon Signed-off-by: Will Fantom --- main.go | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 28ea67a..98d3cb4 100644 --- a/main.go +++ b/main.go @@ -15,12 +15,16 @@ type DashItem struct { Link string `json:"link"` } +type User struct { + Password string `json:"adminpass"` +} + type DashData struct { Title string `json:"title"` Items []DashItem `json:"items"` } -func parseConfig() *DashData { +func parseConfig() (*DashData, *User) { log.Println("Finding Config") configPath := os.Getenv("CONFIG_DIR") + "/config.json" if _, err := os.Stat(configPath); err != nil { @@ -31,21 +35,40 @@ func parseConfig() *DashData { log.Fatalf("Can not open config at: %s\n", configPath) } var data DashData - err = json.Unmarshal(configBytes, &data) - if err != nil { + var user User + errA := json.Unmarshal(configBytes, &data) + errB := json.Unmarshal(configBytes, &user) + if errA != nil || errB != nil { log.Fatalf("Can not parse config as json || %s\n", configPath) } log.Printf("Found %d items in the config\n", len(data.Items)) - return &data + return &data, &user } func main() { log.Println("Starting Simple Dash") dashTemplate := template.Must(template.ParseFiles("templates/dash.html")) - data := parseConfig() + sigininTemplate := template.Must(template.ParseFiles("templates/login.html")) + + data, user := parseConfig() + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { dashTemplate.Execute(w, data) }) + http.HandleFunc("/admin", func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + sigininTemplate.Execute(w, nil) + return + } + + if r.FormValue("password") == user.Password { + log.Printf("Admin account accessed\n") + dashTemplate.Execute(w, data) + return + } + + sigininTemplate.Execute(w, struct{ Invalid bool }{true}) + }) http.ListenAndServe(":80", nil) } From 5345fc06562bda0ccad53c83ffa508991be92dfc Mon Sep 17 00:00:00 2001 From: Will Fantom Date: Tue, 10 Dec 2019 19:03:19 +0000 Subject: [PATCH 09/11] Use multi-stage docker build Signed-off-by: Will Fantom --- build/Dockerfile | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 build/Dockerfile diff --git a/build/Dockerfile b/build/Dockerfile new file mode 100644 index 0000000..9365129 --- /dev/null +++ b/build/Dockerfile @@ -0,0 +1,15 @@ +FROM golang:alpine as builder + +WORKDIR /go/src/github.com/willfantom/simple-dash +COPY ./. . +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main . + +FROM scratch + +ENV CONFIG_DIR /config + +WORKDIR /app +COPY --from=builder /go/src/github.com/willfantom/simple-dash/templates templates +COPY --from=builder /go/src/github.com/willfantom/simple-dash/main main + +CMD ["./main"] \ No newline at end of file From b7c8c54101eebf9030784b70fa239a70500b2e94 Mon Sep 17 00:00:00 2001 From: Will Fantom Date: Tue, 10 Dec 2019 19:10:57 +0000 Subject: [PATCH 10/11] Modify documentation for docker usage Signed-off-by: Will Fantom --- README.md | 8 ++++++++ configs/config.sample.json | 5 +++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9e3192c..f11685c 100644 --- a/README.md +++ b/README.md @@ -19,5 +19,13 @@ This project uses: ## To Use Copy the config.sample.json file and rename to config.json. Be sure to update the fields as you see appropriate. +### On Docker + +This project can run atop the docker engine. To run, simply create a config JSON based of the `configs/config.sample.json` provided. Then, simply navigate to the dir that contains the config and run the image like so: + +```bash +docker run --rm -p 8080:80 -v $(pwd)/:/config willfantom/simple-dash:latest +``` + ## Configure Homepage - 'items' => The menu will scale to the amount of items you want to display. Insert any link you'd like, or {{cur}} for the current URL of the page. Choose icons from [Font Awesome](http://fontawesome.io/icons/) diff --git a/configs/config.sample.json b/configs/config.sample.json index 1213367..47e2720 100644 --- a/configs/config.sample.json +++ b/configs/config.sample.json @@ -1,5 +1,5 @@ { - "title" : "Your Homepage Title", + "title" : "Simple Dash", "items" : [ { "alt" : "Github", @@ -16,5 +16,6 @@ "icon" : "fab fa-docker", "link" : "https://hub.docker.com/u/kukielka/" } - ] + ], + "adminpass": "pass" } From a08eb3a8bab9b09ff944912c869d3c1b41c1a581 Mon Sep 17 00:00:00 2001 From: Will Fantom Date: Tue, 10 Dec 2019 19:47:03 +0000 Subject: [PATCH 11/11] Modify dockerfile to work with buildx Signed-off-by: Will Fantom --- build/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/Dockerfile b/build/Dockerfile index 9365129..c6d4adc 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -2,7 +2,7 @@ FROM golang:alpine as builder WORKDIR /go/src/github.com/willfantom/simple-dash COPY ./. . -RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main . +RUN CGO_ENABLED=0 go build -a -o main . FROM scratch