Serve static files

This commit is contained in:
Vojtěch Káně 2019-06-24 14:15:51 +02:00
parent 45fc4bf135
commit 448ed4805a

22
main.go
View File

@ -3,8 +3,10 @@ package main
import ( import (
"encoding/json" "encoding/json"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"io"
"log" "log"
"net/http" "net/http"
"os"
) )
var upgrader = websocket.Upgrader{ var upgrader = websocket.Upgrader{
@ -44,10 +46,30 @@ func main() {
} }
nudges <- nudge nudges <- nudge
w.WriteHeader(http.StatusNoContent) 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: default:
w.WriteHeader(http.StatusMethodNotAllowed) 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)) log.Println(http.ListenAndServe(":8080", mux))
}() }()