From 448ed4805a1e0358b19987af807a8ac0c0c527b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20K=C3=A1n=C4=9B?= Date: Mon, 24 Jun 2019 14:15:51 +0200 Subject: [PATCH] Serve static files --- main.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/main.go b/main.go index 2437898..1f8c4e7 100644 --- a/main.go +++ b/main.go @@ -3,8 +3,10 @@ package main import ( "encoding/json" "github.com/gorilla/websocket" + "io" "log" "net/http" + "os" ) var upgrader = websocket.Upgrader{ @@ -44,10 +46,30 @@ func main() { } nudges <- nudge w.WriteHeader(http.StatusNoContent) + case "GET": + w.Header().Add("Content-Type", "text/html") + w.WriteHeader(http.StatusOK) + file, _ := os.Open("./controller/controller.html") + defer file.Close() + io.Copy(w, file) default: w.WriteHeader(http.StatusMethodNotAllowed) } }) + 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") + defer file.Close() + io.Copy(w, file) + }) + mux.HandleFunc("/controller.js", func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "application/javascript") + w.WriteHeader(http.StatusOK) + file, _ := os.Open("./controller/controller.js") + defer file.Close() + io.Copy(w, file) + }) log.Println(http.ListenAndServe(":8080", mux)) }()