From 814c8ddd3a57f20edefccc08bc0f7431e1cf30b9 Mon Sep 17 00:00:00 2001 From: Sijisu Date: Fri, 10 Sep 2021 19:09:05 +0200 Subject: [PATCH] Add Docker --- Dockerfile | 18 ++++++++++++++++++ index.html => display/index.html | 0 main.css => display/main.css | 0 main.js => display/main.js | 2 +- main.go | 10 ++++++---- 5 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 Dockerfile rename index.html => display/index.html (100%) rename main.css => display/main.css (100%) rename main.js => display/main.js (98%) diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..83880b7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +FROM golang:1.16-alpine + +WORKDIR /app + +COPY go.mod ./ +COPY go.sum ./ +RUN go mod download + +COPY main.go ./ +COPY controller ./controller +COPY display ./display + +RUN go build -o /fancyn + +EXPOSE 8080 +EXPOSE 8081 + +CMD [ "/fancyn" ] \ No newline at end of file diff --git a/index.html b/display/index.html similarity index 100% rename from index.html rename to display/index.html diff --git a/main.css b/display/main.css similarity index 100% rename from main.css rename to display/main.css diff --git a/main.js b/display/main.js similarity index 98% rename from main.js rename to display/main.js index 28a56e7..f25a0d0 100644 --- a/main.js +++ b/display/main.js @@ -132,7 +132,7 @@ document.addEventListener("mousemove", (e) => { }); document.addEventListener("DOMContentLoaded", () => { - let webSocket = new WebSocket("ws://localhost:8081"); + let webSocket = new WebSocket("ws://localhost:8081/socket"); webSocket.addEventListener("message", (e) => { let nudgeReq = JSON.parse(e.data); n.nudge(nudgeReq[0] / 10, nudgeReq[1] / 10); diff --git a/main.go b/main.go index 1f8c4e7..b9da54a 100644 --- a/main.go +++ b/main.go @@ -2,11 +2,12 @@ package main import ( "encoding/json" - "github.com/gorilla/websocket" "io" "log" "net/http" "os" + + "github.com/gorilla/websocket" ) var upgrader = websocket.Upgrader{ @@ -59,7 +60,7 @@ func main() { mux.HandleFunc("/main.css", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "text/css") w.WriteHeader(http.StatusOK) - file, _ := os.Open("./main.css") + file, _ := os.Open("./display/main.css") defer file.Close() io.Copy(w, file) }) @@ -74,7 +75,8 @@ func main() { }() mux := http.NewServeMux() - mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + mux.Handle("/", http.FileServer(http.Dir("./display/"))) + mux.HandleFunc("/socket", func(w http.ResponseWriter, r *http.Request) { con, err := upgrader.Upgrade(w, r, nil) if err != nil { log.Println(err) @@ -99,5 +101,5 @@ func main() { } } }) - log.Println(http.ListenAndServe("127.0.0.1:8081", mux)) + log.Println(http.ListenAndServe(":8081", mux)) }