chore: improved handler and improved login

This commit is contained in:
2023-09-18 13:50:03 +01:00
parent 1fd025e6d6
commit b22d64a568
7 changed files with 270 additions and 50 deletions

View File

@@ -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() {