From b22d64a568556d52ed4797f6b5f09cbb77203e03 Mon Sep 17 00:00:00 2001 From: Andre Henriques Date: Mon, 18 Sep 2023 13:50:03 +0100 Subject: [PATCH] chore: improved handler and improved login --- handler.go | 183 +++++++++++++++++++++++++++++++------ main.go | 24 ++--- views/js/main.js | 10 ++ views/layout.html | 2 + views/login.html | 26 ++++-- views/partials/header.html | 2 +- views/styles/main.css | 73 +++++++++++++++ 7 files changed, 270 insertions(+), 50 deletions(-) create mode 100644 views/js/main.js diff --git a/handler.go b/handler.go index ef25fe5..089b1c4 100644 --- a/handler.go +++ b/handler.go @@ -82,7 +82,7 @@ type Error struct { type AnswerType int const ( - NORMAL AnswerType = iota + NORMAL AnswerType = 1 << iota HTML JSON HTMLFULL @@ -113,19 +113,29 @@ func LoadBasedOnAnswer(ans AnswerType, w http.ResponseWriter, path string, data } +type HandleFunc struct { + path string + mode AnswerType + fn func(mode AnswerType, w http.ResponseWriter, r *http.Request) *Error +} + type Handler interface { New() Startup() Get(fn func(mode AnswerType, w http.ResponseWriter, r *http.Request) *Error) + Post(fn func(mode AnswerType, w http.ResponseWriter, r *http.Request) *Error) } -type Handle struct{} +type Handle struct { + gets []HandleFunc + posts []HandleFunc +} func handleError(err *Error, answerType AnswerType, w http.ResponseWriter) { if err != nil { w.WriteHeader(err.code) if err.code == 404 { - LoadBasedOnAnswer(answerType, w, "404.html", nil) + LoadBasedOnAnswer(answerType, w, "404.html", nil) return } if err.msg != nil { @@ -134,39 +144,129 @@ func handleError(err *Error, answerType AnswerType, w http.ResponseWriter) { } } -func (x Handle) Get(path string, fn func(mode AnswerType, w http.ResponseWriter, r *http.Request) *Error) { - http.HandleFunc("/"+path, func(w http.ResponseWriter, r *http.Request) { - if r.Method != "Get" { - header := r.Header.Get("REQUEST-TYPE") - if header == "html" { - handleError(fn(HTMLFULL, w, r), HTMLFULL, w) - return - } - handleError(fn(NORMAL, w, r), NORMAL, w) +func (x *Handle) Get(path string, fn func(mode AnswerType, w http.ResponseWriter, r *http.Request) *Error) { + nhandler := + HandleFunc{ + fn: fn, + path: path, + mode: NORMAL | HTML | HTMLFULL | JSON, } - }) - http.HandleFunc("/api/html/"+path, func(w http.ResponseWriter, r *http.Request) { - if r.Method != "Get" { - handleError(fn(HTML, w, r), HTML, w) - } - }) - http.HandleFunc("/api/json/"+path, func(w http.ResponseWriter, r *http.Request) { - if r.Method != "Get" { - handleError(fn(JSON, w, r), JSON, w) - } - }) + + x.gets = append(x.gets, nhandler) } -func (x Handle) New() { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != "/" { - w.WriteHeader(http.StatusNotFound) - LoadView(w, "404.html", nil) +func (x *Handle) GetHTML(path string, fn func(mode AnswerType, w http.ResponseWriter, r *http.Request) *Error) { + nhandler := + HandleFunc{ + fn: fn, + path: path, + mode: NORMAL | HTML | HTMLFULL, + } + + x.gets = append(x.gets, nhandler) +} + +func (x *Handle) GetJSON(path string, fn func(mode AnswerType, w http.ResponseWriter, r *http.Request) *Error) { + nhandler := + HandleFunc{ + fn: fn, + path: path, + mode: JSON, + } + + x.gets = append(x.gets, nhandler) +} + +func (x *Handle) Post(path string, fn func(mode AnswerType, w http.ResponseWriter, r *http.Request) *Error) { + nhandler := + HandleFunc{ + fn: fn, + path: path, + mode: NORMAL | HTML | HTMLFULL | JSON, + } + + x.posts = append(x.posts, nhandler) +} + +func (x *Handle) PostHTML(path string, fn func(mode AnswerType, w http.ResponseWriter, r *http.Request) *Error) { + nhandler := + HandleFunc{ + fn: fn, + path: path, + mode: NORMAL | HTML | HTMLFULL, + } + + x.posts = append(x.posts, nhandler) +} + +func (x *Handle) PostJSON(path string, fn func(mode AnswerType, w http.ResponseWriter, r *http.Request) *Error) { + nhandler := + HandleFunc{ + fn: fn, + path: path, + mode: JSON, + } + + x.posts = append(x.posts, nhandler) +} + +func (x *Handle) handleGets(ans AnswerType, w http.ResponseWriter, r *http.Request) { + for _, s := range x.gets { + fmt.Printf("target: %s, paths: %s\n", s.path, r.URL.Path) + if s.path == r.URL.Path && ans&s.mode != 0 { + s.fn(ans, w, r) return } - LoadView(w, "index.html", nil) + } + w.WriteHeader(http.StatusNotFound) + LoadBasedOnAnswer(ans, w, "404.html", nil) +} + +func (x *Handle) handlePosts(ans AnswerType, w http.ResponseWriter, r *http.Request) { + for _, s := range x.posts { + if s.path == r.URL.Path && ans&s.mode != 0 { + s.fn(ans, w, r) + return + } + } + w.WriteHeader(http.StatusNotFound) + LoadBasedOnAnswer(ans, w, "404.html", nil) +} + +func AnswerTemplate(path string, data interface{}) func(mode AnswerType, w http.ResponseWriter, r *http.Request) *Error { + return func(mode AnswerType, w http.ResponseWriter, r *http.Request) *Error { + LoadBasedOnAnswer(mode, w, path, nil) + return nil + } +} + +func NewHandler() *Handle { + + x := &Handle{} + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + ans := NORMAL + + if r.Header.Get("Request-Type") == "htmlfull" { + ans = HTMLFULL + } + if r.Header.Get("Request-Type") == "html" { + ans = HTML + } + //TODO JSON + + if r.Method == "GET" { + x.handleGets(ans, w, r) + return + } + if r.Method == "POST" { + x.handlePosts(ans, w, r) + return + } + panic("TODO handle: " + r.Method) }) + // TODO Handle this in other way http.HandleFunc("/styles/", func(w http.ResponseWriter, r *http.Request) { path := r.URL.Path[len("/styles/"):] if !strings.HasSuffix(path, ".css") { @@ -188,6 +288,31 @@ func (x Handle) New() { w.Header().Set("Content-Type", "text/css; charset=utf-8") t.Execute(w, nil) }) + + // TODO Handle this in other way + http.HandleFunc("/js/", func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path[len("/js/"):] + if !strings.HasSuffix(path, ".js") { + w.WriteHeader(http.StatusNotFound) + w.Write([]byte("File not found")) + return + } + + t, err := template.ParseFiles("./views/js/" + path) + + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("Failed to load template")) + fmt.Println("Error:") + fmt.Println(err) + return + } + + w.Header().Set("Content-Type", "text/javascript; charset=utf-8") + t.Execute(w, nil) + }) + + return x } func (x Handle) Startup() { diff --git a/main.go b/main.go index 398f70e..62438dc 100644 --- a/main.go +++ b/main.go @@ -8,19 +8,19 @@ import ( func main() { fmt.Println("Starting server on :8000!") - handle := Handle{} + handle := NewHandler() - handle.New() + handle.GetHTML("/", AnswerTemplate("index.html", nil)) + handle.GetHTML("/login", AnswerTemplate("login.html", nil)) + handle.Post("/login", func(mode AnswerType, w http.ResponseWriter, r *http.Request) *Error { + if mode == JSON { + return &Error{code: 404} + } - handle.Get("login", func(mode AnswerType, w http.ResponseWriter, r *http.Request) *Error { - if mode == JSON { - return &Error{ - code: 404, - }; - } - LoadBasedOnAnswer(mode, w, "login.html", nil) - return nil; - }) + w.Header().Set("Location", "/") + w.WriteHeader(http.StatusSeeOther) + return nil + }) - handle.Startup() + handle.Startup() } diff --git a/views/js/main.js b/views/js/main.js new file mode 100644 index 0000000..3f72e55 --- /dev/null +++ b/views/js/main.js @@ -0,0 +1,10 @@ +function load() { + for (const elm of document.querySelectorAll("form > button")) { + elm.addEventListener('click', (e) => { + e.target.parentElement.classList.add("submitted"); + }); + } +} + +window.onload = load; +htmx.on('htmx:afterSwap', load); diff --git a/views/layout.html b/views/layout.html index c27a00b..2e5b79f 100644 --- a/views/layout.html +++ b/views/layout.html @@ -16,6 +16,8 @@ + + {{ block "js_imports" . }} {{end}} {{ block "body" . }} {{ block "header.html" . }} {{end}}
diff --git a/views/login.html b/views/login.html index 51044c9..84a88c7 100644 --- a/views/login.html +++ b/views/login.html @@ -1,15 +1,25 @@ {{ define "title"}} Home : AI Stuff {{ end }} {{ define "mainbody" }} -
+
{{ end }} diff --git a/views/partials/header.html b/views/partials/header.html index e21a461..e7283bd 100644 --- a/views/partials/header.html +++ b/views/partials/header.html @@ -2,7 +2,7 @@