Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go Backend #13

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
15 changes: 15 additions & 0 deletions build/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM golang:alpine as builder

WORKDIR /go/src/github.com/willfantom/simple-dash
COPY ./. .
RUN CGO_ENABLED=0 go build -a -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"]
8 changes: 8 additions & 0 deletions build/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM golang:alpine

WORKDIR /app
COPY ./. .

RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .

CMD ["./main"]
5 changes: 0 additions & 5 deletions common/css/fontawesome-all.min.css

This file was deleted.

58 changes: 0 additions & 58 deletions common/css/main.css

This file was deleted.

1 change: 0 additions & 1 deletion common/js/trianglify.min.js

This file was deleted.

Binary file removed common/webfonts/fa-brands-400.eot
Binary file not shown.
3,451 changes: 0 additions & 3,451 deletions common/webfonts/fa-brands-400.svg

This file was deleted.

Binary file removed common/webfonts/fa-brands-400.ttf
Binary file not shown.
Binary file removed common/webfonts/fa-brands-400.woff
Binary file not shown.
Binary file removed common/webfonts/fa-brands-400.woff2
Binary file not shown.
Binary file removed common/webfonts/fa-regular-400.eot
Binary file not shown.
803 changes: 0 additions & 803 deletions common/webfonts/fa-regular-400.svg

This file was deleted.

Binary file removed common/webfonts/fa-regular-400.ttf
Binary file not shown.
Binary file removed common/webfonts/fa-regular-400.woff
Binary file not shown.
Binary file removed common/webfonts/fa-regular-400.woff2
Binary file not shown.
Binary file removed common/webfonts/fa-solid-900.eot
Binary file not shown.
4,649 changes: 0 additions & 4,649 deletions common/webfonts/fa-solid-900.svg

This file was deleted.

Binary file removed common/webfonts/fa-solid-900.ttf
Binary file not shown.
Binary file removed common/webfonts/fa-solid-900.woff
Binary file not shown.
Binary file removed common/webfonts/fa-solid-900.woff2
Binary file not shown.
20 changes: 0 additions & 20 deletions config.json

This file was deleted.

5 changes: 3 additions & 2 deletions config.sample.json → configs/config.sample.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"title" : "Your Homepage Title",
"title" : "Simple Dash",
"items" : [
{
"alt" : "Github",
Expand All @@ -16,5 +16,6 @@
"icon" : "fab fa-docker",
"link" : "https://hub.docker.com/u/kukielka/"
}
]
],
"adminpass": "pass"
}
75 changes: 0 additions & 75 deletions index.html

This file was deleted.

74 changes: 74 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package main

import (
"encoding/json"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
)

type DashItem struct {
Name string `json:"alt"`
Icon string `json:"icon"`
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, *User) {
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
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, &user
}

func main() {
log.Println("Starting Simple Dash")

dashTemplate := template.Must(template.ParseFiles("templates/dash.html"))
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)
}
Loading