Loads of stuff, check roadmap changes in the README :P

This commit is contained in:
Miguel de la Cruz 2021-09-12 20:37:51 +02:00
parent e1fca030bc
commit 68ae33e78a
15 changed files with 17179 additions and 10 deletions

View file

@ -0,0 +1,26 @@
package web
import (
"net/http"
)
type NotFoundIndexResponseWriter struct {
http.ResponseWriter
status int
}
func (nfr *NotFoundIndexResponseWriter) WriteHeader(status int) {
nfr.status = status
if status != http.StatusNotFound {
nfr.ResponseWriter.WriteHeader(status)
}
}
func (nfr *NotFoundIndexResponseWriter) Write(p []byte) (int, error) {
if nfr.status != http.StatusNotFound {
return nfr.ResponseWriter.Write(p)
}
nfr.Header().Add("Content-Type", "text/html; charset=utf-8")
return nfr.ResponseWriter.Write(indexBytes)
}

View file

@ -1,10 +1,20 @@
package web
import (
"embed"
"fmt"
"io/fs"
"net/http"
)
//go:embed static/*
var staticFS embed.FS
var indexBytes []byte
func init() {
indexBytes, _ = staticFS.ReadFile("static/index.html")
}
type WebServer struct {
*http.Server
}
@ -12,9 +22,13 @@ type WebServer struct {
func NewWebServer(port int) (*WebServer, error) {
mux := http.NewServeMux()
// ToDo: configure routes
// ToDo: register WS and API endpoints here
staticFSSub, _ := fs.Sub(staticFS, "static")
staticFileServerHandler := http.FileServer(http.FS(staticFSSub))
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello craban!!")
srw := &NotFoundIndexResponseWriter{ResponseWriter: w}
staticFileServerHandler.ServeHTTP(srw, r)
})
s := &WebServer{