feat: added users db, login, register, logout
This commit is contained in:
183
handler.go
183
handler.go
@@ -1,11 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func baseLoadTemplate(base string, path string) (*template.Template, any) {
|
||||
@@ -74,6 +78,8 @@ func LoadHtml(writer http.ResponseWriter, path string, data interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
type AnyMap = map[string]interface{}
|
||||
|
||||
type Error struct {
|
||||
code int
|
||||
msg *string
|
||||
@@ -90,10 +96,10 @@ const (
|
||||
|
||||
func LoadBasedOnAnswer(ans AnswerType, w http.ResponseWriter, path string, data map[string]interface{}) {
|
||||
if ans == NORMAL {
|
||||
LoadView(w, path, nil)
|
||||
LoadView(w, path, data)
|
||||
return
|
||||
} else if ans == HTML {
|
||||
LoadHtml(w, path, nil)
|
||||
LoadHtml(w, path, data)
|
||||
return
|
||||
} else if ans == HTMLFULL {
|
||||
if data == nil {
|
||||
@@ -116,26 +122,43 @@ 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
|
||||
fn func(w http.ResponseWriter, r *http.Request, c *Context) *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)
|
||||
Get(fn func(w http.ResponseWriter, r *http.Request, c *Context) *Error)
|
||||
Post(fn func(w http.ResponseWriter, r *http.Request, c *Context) *Error)
|
||||
}
|
||||
|
||||
type Handle struct {
|
||||
db *sql.DB
|
||||
gets []HandleFunc
|
||||
posts []HandleFunc
|
||||
}
|
||||
|
||||
func handleError(err *Error, answerType AnswerType, w http.ResponseWriter) {
|
||||
func decodeBody(r *http.Request) (string, *Error) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err == nil {
|
||||
return "", &Error{code: http.StatusBadRequest}
|
||||
}
|
||||
|
||||
return string(body[:]), nil
|
||||
}
|
||||
|
||||
func handleError(err *Error, w http.ResponseWriter, context *Context) {
|
||||
|
||||
data := context.toMap()
|
||||
|
||||
if err != nil {
|
||||
w.WriteHeader(err.code)
|
||||
if err.code == 404 {
|
||||
LoadBasedOnAnswer(answerType, w, "404.html", nil)
|
||||
if err.code == http.StatusNotFound {
|
||||
LoadBasedOnAnswer(context.Mode, w, "404.html", data)
|
||||
return
|
||||
}
|
||||
if err.code == http.StatusBadRequest {
|
||||
LoadBasedOnAnswer(context.Mode, w, "400.html", data)
|
||||
return
|
||||
}
|
||||
if err.msg != nil {
|
||||
@@ -144,7 +167,7 @@ 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) {
|
||||
func (x *Handle) Get(path string, fn func(w http.ResponseWriter, r *http.Request, c *Context) *Error) {
|
||||
nhandler :=
|
||||
HandleFunc{
|
||||
fn: fn,
|
||||
@@ -155,7 +178,7 @@ func (x *Handle) Get(path string, fn func(mode AnswerType, w http.ResponseWriter
|
||||
x.gets = append(x.gets, nhandler)
|
||||
}
|
||||
|
||||
func (x *Handle) GetHTML(path string, fn func(mode AnswerType, w http.ResponseWriter, r *http.Request) *Error) {
|
||||
func (x *Handle) GetHTML(path string, fn func(w http.ResponseWriter, r *http.Request, c *Context) *Error) {
|
||||
nhandler :=
|
||||
HandleFunc{
|
||||
fn: fn,
|
||||
@@ -166,7 +189,7 @@ func (x *Handle) GetHTML(path string, fn func(mode AnswerType, w http.ResponseWr
|
||||
x.gets = append(x.gets, nhandler)
|
||||
}
|
||||
|
||||
func (x *Handle) GetJSON(path string, fn func(mode AnswerType, w http.ResponseWriter, r *http.Request) *Error) {
|
||||
func (x *Handle) GetJSON(path string, fn func(w http.ResponseWriter, r *http.Request, c *Context) *Error) {
|
||||
nhandler :=
|
||||
HandleFunc{
|
||||
fn: fn,
|
||||
@@ -177,7 +200,7 @@ func (x *Handle) GetJSON(path string, fn func(mode AnswerType, w http.ResponseWr
|
||||
x.gets = append(x.gets, nhandler)
|
||||
}
|
||||
|
||||
func (x *Handle) Post(path string, fn func(mode AnswerType, w http.ResponseWriter, r *http.Request) *Error) {
|
||||
func (x *Handle) Post(path string, fn func(w http.ResponseWriter, r *http.Request, c *Context) *Error) {
|
||||
nhandler :=
|
||||
HandleFunc{
|
||||
fn: fn,
|
||||
@@ -188,7 +211,7 @@ func (x *Handle) Post(path string, fn func(mode AnswerType, w http.ResponseWrite
|
||||
x.posts = append(x.posts, nhandler)
|
||||
}
|
||||
|
||||
func (x *Handle) PostHTML(path string, fn func(mode AnswerType, w http.ResponseWriter, r *http.Request) *Error) {
|
||||
func (x *Handle) PostHTML(path string, fn func(w http.ResponseWriter, r *http.Request, c *Context) *Error) {
|
||||
nhandler :=
|
||||
HandleFunc{
|
||||
fn: fn,
|
||||
@@ -199,7 +222,7 @@ func (x *Handle) PostHTML(path string, fn func(mode AnswerType, w http.ResponseW
|
||||
x.posts = append(x.posts, nhandler)
|
||||
}
|
||||
|
||||
func (x *Handle) PostJSON(path string, fn func(mode AnswerType, w http.ResponseWriter, r *http.Request) *Error) {
|
||||
func (x *Handle) PostJSON(path string, fn func(w http.ResponseWriter, r *http.Request, c *Context) *Error) {
|
||||
nhandler :=
|
||||
HandleFunc{
|
||||
fn: fn,
|
||||
@@ -210,43 +233,130 @@ func (x *Handle) PostJSON(path string, fn func(mode AnswerType, w http.ResponseW
|
||||
x.posts = append(x.posts, nhandler)
|
||||
}
|
||||
|
||||
func (x *Handle) handleGets(ans AnswerType, w http.ResponseWriter, r *http.Request) {
|
||||
func (x *Handle) handleGets(w http.ResponseWriter, r *http.Request, context *Context) {
|
||||
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)
|
||||
if s.path == r.URL.Path && context.Mode&s.mode != 0 {
|
||||
handleError(s.fn(w, r, context), w, context)
|
||||
return
|
||||
}
|
||||
}
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
LoadBasedOnAnswer(ans, w, "404.html", nil)
|
||||
if context.Mode != HTMLFULL {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
LoadBasedOnAnswer(context.Mode, w, "404.html", map[string]interface{}{
|
||||
"context": context,
|
||||
})
|
||||
}
|
||||
|
||||
func (x *Handle) handlePosts(ans AnswerType, w http.ResponseWriter, r *http.Request) {
|
||||
func (x *Handle) handlePosts(w http.ResponseWriter, r *http.Request, context *Context) {
|
||||
for _, s := range x.posts {
|
||||
if s.path == r.URL.Path && ans&s.mode != 0 {
|
||||
s.fn(ans, w, r)
|
||||
if s.path == r.URL.Path && context.Mode&s.mode != 0 {
|
||||
handleError(s.fn(w, r, context), w, context)
|
||||
return
|
||||
}
|
||||
}
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
LoadBasedOnAnswer(ans, w, "404.html", nil)
|
||||
if context.Mode != HTMLFULL {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
LoadBasedOnAnswer(context.Mode, w, "404.html", map[string]interface{}{
|
||||
"context": context,
|
||||
})
|
||||
}
|
||||
|
||||
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)
|
||||
func AnswerTemplate(path string, data AnyMap) func(w http.ResponseWriter, r *http.Request, c *Context) *Error {
|
||||
return func(w http.ResponseWriter, r *http.Request, c *Context) *Error {
|
||||
if data == nil {
|
||||
LoadBasedOnAnswer(c.Mode, w, path, c.toMap())
|
||||
} else {
|
||||
LoadBasedOnAnswer(c.Mode, w, path, c.addMap(data))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func NewHandler() *Handle {
|
||||
type Context struct {
|
||||
Token *string
|
||||
User *User
|
||||
Mode AnswerType
|
||||
}
|
||||
|
||||
x := &Handle{}
|
||||
func (c Context) addMap(m AnyMap) AnyMap {
|
||||
m["Context"] = c;
|
||||
return m;
|
||||
}
|
||||
|
||||
func (c *Context) toMap() map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"Context": c,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Context) requireAuth(w http.ResponseWriter, r *http.Request) bool {
|
||||
if c.User == nil {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
var LogoffError = errors.New("Invalid token!")
|
||||
|
||||
func (x Handle) createContext(mode AnswerType, r *http.Request) (*Context, error) {
|
||||
|
||||
var token *string
|
||||
|
||||
for _, r := range r.Cookies() {
|
||||
if r.Name == "auth" {
|
||||
token = &r.Value
|
||||
}
|
||||
}
|
||||
|
||||
if token == nil {
|
||||
return &Context{
|
||||
Mode: mode,
|
||||
}, nil
|
||||
}
|
||||
|
||||
user, err := userFromToken(x.db, *token)
|
||||
if err != nil {
|
||||
return nil, errors.Join(err, LogoffError)
|
||||
}
|
||||
|
||||
return &Context{token, user, mode}, nil
|
||||
}
|
||||
|
||||
func logoff(mode AnswerType, w http.ResponseWriter, r *http.Request) {
|
||||
// Delete cookie
|
||||
cookie := &http.Cookie{
|
||||
Name: "auth",
|
||||
Value: "",
|
||||
Expires: time.Unix(0, 0),
|
||||
}
|
||||
http.SetCookie(w, cookie)
|
||||
|
||||
// Setup response
|
||||
w.Header().Set("Location", "/login")
|
||||
if mode == JSON {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write([]byte("\"Bye Bye\""));
|
||||
return
|
||||
}
|
||||
if mode & (HTMLFULL | HTML) != 0 {
|
||||
w.WriteHeader(http.StatusUnauthorized);
|
||||
w.Write([]byte("Bye Bye"));
|
||||
} else {
|
||||
w.WriteHeader(http.StatusSeeOther);
|
||||
}
|
||||
}
|
||||
|
||||
func NewHandler(db *sql.DB) *Handle {
|
||||
|
||||
var gets []HandleFunc
|
||||
var posts []HandleFunc
|
||||
x := &Handle{ db, gets, posts, }
|
||||
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
// Decide answertype
|
||||
ans := NORMAL
|
||||
|
||||
if r.Header.Get("Request-Type") == "htmlfull" {
|
||||
ans = HTMLFULL
|
||||
}
|
||||
@@ -255,12 +365,19 @@ func NewHandler() *Handle {
|
||||
}
|
||||
//TODO JSON
|
||||
|
||||
//Login state
|
||||
context, err := x.createContext(ans, r)
|
||||
if err != nil {
|
||||
logoff(ans, w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if r.Method == "GET" {
|
||||
x.handleGets(ans, w, r)
|
||||
x.handleGets(w, r, context)
|
||||
return
|
||||
}
|
||||
if r.Method == "POST" {
|
||||
x.handlePosts(ans, w, r)
|
||||
x.handlePosts(w, r, context)
|
||||
return
|
||||
}
|
||||
panic("TODO handle: " + r.Method)
|
||||
|
||||
Reference in New Issue
Block a user