From 68625c63d62214dce67ea94ce5ba3d39ff94e36d Mon Sep 17 00:00:00 2001 From: Settel Date: Wed, 28 Jul 2021 22:21:14 +0200 Subject: [PATCH] project setup --- client/dist/index.html | 10 ++++++++++ server/Makefile | 24 ++++++++++++++++++++++++ server/go.mod | 3 +++ server/handler/authmux.go | 27 +++++++++++++++++++++++++++ server/handler/internal.go | 23 +++++++++++++++++++++++ server/knyc.go | 24 ++++++++++++++++++++++++ 6 files changed, 111 insertions(+) create mode 100644 client/dist/index.html create mode 100644 server/Makefile create mode 100644 server/go.mod create mode 100644 server/handler/authmux.go create mode 100644 server/handler/internal.go create mode 100644 server/knyc.go diff --git a/client/dist/index.html b/client/dist/index.html new file mode 100644 index 0000000..75ddc0e --- /dev/null +++ b/client/dist/index.html @@ -0,0 +1,10 @@ + + + + + KnYC + + +KnYC + + diff --git a/server/Makefile b/server/Makefile new file mode 100644 index 0000000..edf7086 --- /dev/null +++ b/server/Makefile @@ -0,0 +1,24 @@ +info: + @echo available targets: + @perl -ne 'm/^([a-zA-Z0-9\-]+):/ && print(" $$1\n");' Makefile + +build: + go build knyc.go + +run: + go run knyc.go + +run-loop: + pexec -R -c -e TARGET \ + -r _run-endless-loop \ + -r _run-inotify-restart \ + -- $(MAKE) '$$TARGET' + +run-standalone: + ./knyc + +_run-endless-loop: + while true; do $(MAKE) run || sleep 3; done + +_run-inotify-restart: + inotifyloop . curl -s http://localhost:32039/__intern__/exit diff --git a/server/go.mod b/server/go.mod new file mode 100644 index 0000000..acdf66d --- /dev/null +++ b/server/go.mod @@ -0,0 +1,3 @@ +module sirlab.de/go/knyc + +go 1.16 diff --git a/server/handler/authmux.go b/server/handler/authmux.go new file mode 100644 index 0000000..028b969 --- /dev/null +++ b/server/handler/authmux.go @@ -0,0 +1,27 @@ +package handler + +import ( + "net/http" +) + +type AuthMux struct { + mux *http.ServeMux +} + +func NewAuthMux() *AuthMux { + return &(AuthMux{ + mux: http.NewServeMux(), + }) +} + +func (authMux *AuthMux) ServeHTTP(w http.ResponseWriter, r *http.Request) { + authMux.mux.ServeHTTP(w, r) +} + +func (authMux *AuthMux) PublicHandleFunc(pattern string, handlerFunc func(http.ResponseWriter, *http.Request)) { + authMux.mux.HandleFunc(pattern, handlerFunc) +} + +func (authMux *AuthMux) PublicHandle(pattern string, handler http.Handler) { + authMux.mux.Handle(pattern, handler) +} diff --git a/server/handler/internal.go b/server/handler/internal.go new file mode 100644 index 0000000..eacee06 --- /dev/null +++ b/server/handler/internal.go @@ -0,0 +1,23 @@ +package handler + +import ( + "fmt" + "net/http" + "os" + "time" +) + +func Exit(_ http.ResponseWriter, _ *http.Request) { + os.Exit(0) +} + +func CreateVersionHandler() func(w http.ResponseWriter, r *http.Request) { + startTime := time.Now().Unix() + return func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "%d\n", startTime) + } +} + +func IsLoggedIn(w http.ResponseWriter, _ *http.Request) { + fmt.Fprintf(w, "ok") +} diff --git a/server/knyc.go b/server/knyc.go new file mode 100644 index 0000000..a503117 --- /dev/null +++ b/server/knyc.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + "net/http" + "sirlab.de/go/knyc/handler" +) + +func main() { + app := handler.NewAuthMux() + http.Handle("/", app) + + app.PublicHandleFunc("/__intern__/exit", handler.Exit) + + // hanlde login page + fsHandler := http.FileServer(http.Dir("../client/dist/")) + + // default handler + app.PublicHandle("/", fsHandler) + + // start listening + fmt.Println("http://localhost:32039") + http.ListenAndServe(":32039", nil) +}