2023-09-21 16:43:11 +01:00
|
|
|
package utils
|
2023-09-18 00:26:42 +01:00
|
|
|
|
|
|
|
import (
|
2023-09-19 13:39:59 +01:00
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2023-09-18 00:26:42 +01:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
2023-09-19 13:39:59 +01:00
|
|
|
"io"
|
2023-09-18 00:26:42 +01:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2023-09-21 15:38:02 +01:00
|
|
|
"os"
|
|
|
|
"path"
|
2023-09-18 00:26:42 +01:00
|
|
|
"strings"
|
2023-09-19 13:39:59 +01:00
|
|
|
"time"
|
2023-09-21 16:43:11 +01:00
|
|
|
|
|
|
|
dbtypes "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types"
|
2023-09-18 00:26:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func baseLoadTemplate(base string, path string) (*template.Template, any) {
|
|
|
|
return template.New(base).ParseFiles(
|
|
|
|
"./views/"+base,
|
|
|
|
"./views/"+path,
|
|
|
|
"./views/partials/header.html",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadTemplate(path string) (*template.Template, any) {
|
|
|
|
return baseLoadTemplate("layout.html", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadView(writer http.ResponseWriter, path string, data interface{}) {
|
|
|
|
tmpl, err := loadTemplate(path)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to load view %s\n", path)
|
|
|
|
fmt.Println(err)
|
|
|
|
if path == "500.html" {
|
|
|
|
writer.Write([]byte("<h1>Failed to load 500.html check console for more info</h1>"))
|
|
|
|
} else {
|
|
|
|
LoadView(writer, "500.html", nil)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Execute(writer, data); err != nil {
|
|
|
|
fmt.Printf("Failed to load view %s\n", path)
|
|
|
|
fmt.Println(err)
|
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
|
|
|
if path == "500.html" {
|
|
|
|
writer.Write([]byte("<h1>Failed to load 500.html check console for more info</h1>"))
|
|
|
|
} else {
|
|
|
|
LoadView(writer, "500.html", nil)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Only returns the html without template */
|
|
|
|
func LoadHtml(writer http.ResponseWriter, path string, data interface{}) {
|
|
|
|
tmpl, err := baseLoadTemplate("html.html", path)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to load template %s\n", path)
|
|
|
|
fmt.Println(err)
|
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
|
|
|
if path == "500.html" {
|
|
|
|
writer.Write([]byte("<h1>Failed to load 500.html check console for more info</h1>"))
|
|
|
|
} else {
|
|
|
|
LoadHtml(writer, "500.html", nil)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Execute(writer, data); err != nil {
|
|
|
|
fmt.Printf("Failed to execute template %s\n", path)
|
|
|
|
fmt.Println(err)
|
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
|
|
|
if path == "500.html" {
|
|
|
|
writer.Write([]byte("<h1>Failed to load 500.html check console for more info</h1>"))
|
|
|
|
} else {
|
|
|
|
LoadHtml(writer, "500.html", nil)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-21 15:38:02 +01:00
|
|
|
func LoadError(writer http.ResponseWriter, path string, base string, data AnyMap) {
|
|
|
|
if data == nil {
|
|
|
|
data = map[string]interface{} {
|
|
|
|
"Error": true,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
data["Error"] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl, err := template.New("").Parse("{{template \"" + base + "\" . }}")
|
|
|
|
if err != nil {
|
|
|
|
panic("Lol")
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl, err = tmpl.ParseFiles(
|
|
|
|
"./views/"+path,
|
|
|
|
"./views/partials/header.html",
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to load template %s\n", path)
|
|
|
|
fmt.Println(err)
|
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
|
|
|
if path == "500.html" {
|
|
|
|
writer.Write([]byte("<h1>Failed to load 500.html check console for more info</h1>"))
|
|
|
|
} else {
|
|
|
|
LoadHtml(writer, "500.html", nil)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Execute(writer, data); err != nil {
|
|
|
|
fmt.Printf("Failed to execute template %s\n", path)
|
|
|
|
fmt.Println(err)
|
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
|
|
|
if path == "500.html" {
|
|
|
|
writer.Write([]byte("<h1>Failed to load 500.html check console for more info</h1>"))
|
|
|
|
} else {
|
|
|
|
LoadHtml(writer, "500.html", nil)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-19 13:39:59 +01:00
|
|
|
type AnyMap = map[string]interface{}
|
|
|
|
|
2023-09-18 00:26:42 +01:00
|
|
|
type Error struct {
|
2023-09-21 16:43:11 +01:00
|
|
|
Code int
|
|
|
|
Msg *string
|
2023-09-21 15:38:02 +01:00
|
|
|
data AnyMap
|
2023-09-18 00:26:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type AnswerType int
|
|
|
|
|
|
|
|
const (
|
2023-09-18 13:50:03 +01:00
|
|
|
NORMAL AnswerType = 1 << iota
|
2023-09-18 00:26:42 +01:00
|
|
|
HTML
|
|
|
|
JSON
|
|
|
|
HTMLFULL
|
|
|
|
)
|
|
|
|
|
|
|
|
func LoadBasedOnAnswer(ans AnswerType, w http.ResponseWriter, path string, data map[string]interface{}) {
|
|
|
|
if ans == NORMAL {
|
2023-09-19 13:39:59 +01:00
|
|
|
LoadView(w, path, data)
|
2023-09-18 00:26:42 +01:00
|
|
|
return
|
|
|
|
} else if ans == HTML {
|
2023-09-19 13:39:59 +01:00
|
|
|
LoadHtml(w, path, data)
|
2023-09-18 00:26:42 +01:00
|
|
|
return
|
|
|
|
} else if ans == HTMLFULL {
|
|
|
|
if data == nil {
|
|
|
|
LoadHtml(w, path, map[string]interface{}{
|
|
|
|
"App": true,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
data["App"] = true
|
|
|
|
LoadHtml(w, path, data)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
} else if ans == JSON {
|
|
|
|
panic("TODO JSON!")
|
|
|
|
} else {
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-09-18 13:50:03 +01:00
|
|
|
type HandleFunc struct {
|
|
|
|
path string
|
|
|
|
mode AnswerType
|
2023-09-19 13:39:59 +01:00
|
|
|
fn func(w http.ResponseWriter, r *http.Request, c *Context) *Error
|
2023-09-18 13:50:03 +01:00
|
|
|
}
|
|
|
|
|
2023-09-18 00:26:42 +01:00
|
|
|
type Handler interface {
|
|
|
|
New()
|
|
|
|
Startup()
|
2023-09-19 13:39:59 +01:00
|
|
|
Get(fn func(w http.ResponseWriter, r *http.Request, c *Context) *Error)
|
|
|
|
Post(fn func(w http.ResponseWriter, r *http.Request, c *Context) *Error)
|
2023-09-18 00:26:42 +01:00
|
|
|
}
|
|
|
|
|
2023-09-18 13:50:03 +01:00
|
|
|
type Handle struct {
|
2023-09-21 16:43:11 +01:00
|
|
|
Db *sql.DB
|
2023-09-21 15:38:02 +01:00
|
|
|
gets []HandleFunc
|
|
|
|
posts []HandleFunc
|
|
|
|
deletes []HandleFunc
|
2023-09-18 13:50:03 +01:00
|
|
|
}
|
2023-09-18 00:26:42 +01:00
|
|
|
|
2023-09-19 13:39:59 +01:00
|
|
|
func decodeBody(r *http.Request) (string, *Error) {
|
|
|
|
body, err := io.ReadAll(r.Body)
|
|
|
|
if err == nil {
|
2023-09-21 16:43:11 +01:00
|
|
|
return "", &Error{Code: http.StatusBadRequest}
|
2023-09-19 13:39:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return string(body[:]), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleError(err *Error, w http.ResponseWriter, context *Context) {
|
|
|
|
|
2023-09-18 00:26:42 +01:00
|
|
|
if err != nil {
|
2023-09-21 16:43:11 +01:00
|
|
|
data := context.AddMap(err.data)
|
|
|
|
w.WriteHeader(err.Code)
|
|
|
|
if err.Code == http.StatusNotFound {
|
2023-09-19 13:39:59 +01:00
|
|
|
LoadBasedOnAnswer(context.Mode, w, "404.html", data)
|
|
|
|
return
|
|
|
|
}
|
2023-09-21 16:43:11 +01:00
|
|
|
if err.Code == http.StatusBadRequest {
|
2023-09-19 13:39:59 +01:00
|
|
|
LoadBasedOnAnswer(context.Mode, w, "400.html", data)
|
2023-09-18 00:26:42 +01:00
|
|
|
return
|
|
|
|
}
|
2023-09-21 16:43:11 +01:00
|
|
|
if err.Msg != nil {
|
|
|
|
w.Write([]byte(*err.Msg))
|
2023-09-18 00:26:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-19 13:39:59 +01:00
|
|
|
func (x *Handle) Get(path string, fn func(w http.ResponseWriter, r *http.Request, c *Context) *Error) {
|
2023-09-21 15:38:02 +01:00
|
|
|
x.gets = append(x.gets, HandleFunc{path, NORMAL | HTML | HTMLFULL | JSON, fn})
|
2023-09-18 13:50:03 +01:00
|
|
|
}
|
|
|
|
|
2023-09-19 13:39:59 +01:00
|
|
|
func (x *Handle) GetHTML(path string, fn func(w http.ResponseWriter, r *http.Request, c *Context) *Error) {
|
2023-09-21 15:38:02 +01:00
|
|
|
x.gets = append(x.gets, HandleFunc{path, NORMAL | HTML | HTMLFULL, fn})
|
2023-09-18 13:50:03 +01:00
|
|
|
}
|
|
|
|
|
2023-09-19 13:39:59 +01:00
|
|
|
func (x *Handle) GetJSON(path string, fn func(w http.ResponseWriter, r *http.Request, c *Context) *Error) {
|
2023-09-21 15:38:02 +01:00
|
|
|
x.gets = append(x.gets, HandleFunc{path, JSON, fn})
|
2023-09-18 13:50:03 +01:00
|
|
|
}
|
|
|
|
|
2023-09-19 13:39:59 +01:00
|
|
|
func (x *Handle) Post(path string, fn func(w http.ResponseWriter, r *http.Request, c *Context) *Error) {
|
2023-09-21 15:38:02 +01:00
|
|
|
x.posts = append(x.posts, HandleFunc{path, NORMAL | HTML | HTMLFULL | JSON, fn})
|
2023-09-18 00:26:42 +01:00
|
|
|
}
|
|
|
|
|
2023-09-19 13:39:59 +01:00
|
|
|
func (x *Handle) PostHTML(path string, fn func(w http.ResponseWriter, r *http.Request, c *Context) *Error) {
|
2023-09-21 15:38:02 +01:00
|
|
|
x.posts = append(x.posts, HandleFunc{path, NORMAL | HTML | HTMLFULL, fn})
|
2023-09-18 13:50:03 +01:00
|
|
|
}
|
|
|
|
|
2023-09-19 13:39:59 +01:00
|
|
|
func (x *Handle) PostJSON(path string, fn func(w http.ResponseWriter, r *http.Request, c *Context) *Error) {
|
2023-09-21 15:38:02 +01:00
|
|
|
x.posts = append(x.posts, HandleFunc{path, JSON, fn})
|
|
|
|
}
|
2023-09-18 13:50:03 +01:00
|
|
|
|
2023-09-21 15:38:02 +01:00
|
|
|
func (x *Handle) Delete(path string, fn func(w http.ResponseWriter, r *http.Request, c *Context) *Error) {
|
|
|
|
x.deletes = append(x.deletes, HandleFunc{path, NORMAL | HTML | HTMLFULL | JSON, fn})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *Handle) DeleteHTML(path string, fn func(w http.ResponseWriter, r *http.Request, c *Context) *Error) {
|
|
|
|
x.deletes = append(x.deletes, HandleFunc{path, NORMAL | HTML | HTMLFULL, fn})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *Handle) DeleteJSON(path string, fn func(w http.ResponseWriter, r *http.Request, c *Context) *Error) {
|
|
|
|
x.deletes = append(x.deletes, HandleFunc{path, JSON, fn})
|
2023-09-18 13:50:03 +01:00
|
|
|
}
|
|
|
|
|
2023-09-19 13:39:59 +01:00
|
|
|
func (x *Handle) handleGets(w http.ResponseWriter, r *http.Request, context *Context) {
|
2023-09-18 13:50:03 +01:00
|
|
|
for _, s := range x.gets {
|
2023-09-19 13:39:59 +01:00
|
|
|
if s.path == r.URL.Path && context.Mode&s.mode != 0 {
|
|
|
|
handleError(s.fn(w, r, context), w, context)
|
2023-09-18 13:50:03 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-09-19 13:39:59 +01:00
|
|
|
if context.Mode != HTMLFULL {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
}
|
2023-09-21 16:43:11 +01:00
|
|
|
LoadBasedOnAnswer(context.Mode, w, "404.html", context.AddMap(nil))
|
2023-09-18 13:50:03 +01:00
|
|
|
}
|
|
|
|
|
2023-09-19 13:39:59 +01:00
|
|
|
func (x *Handle) handlePosts(w http.ResponseWriter, r *http.Request, context *Context) {
|
2023-09-18 13:50:03 +01:00
|
|
|
for _, s := range x.posts {
|
2023-09-19 13:39:59 +01:00
|
|
|
if s.path == r.URL.Path && context.Mode&s.mode != 0 {
|
|
|
|
handleError(s.fn(w, r, context), w, context)
|
2023-09-18 13:50:03 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-09-19 13:39:59 +01:00
|
|
|
if context.Mode != HTMLFULL {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
}
|
2023-09-21 16:43:11 +01:00
|
|
|
LoadBasedOnAnswer(context.Mode, w, "404.html", context.AddMap(nil))
|
2023-09-18 13:50:03 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 15:38:02 +01:00
|
|
|
func (x *Handle) handleDeletes(w http.ResponseWriter, r *http.Request, context *Context) {
|
|
|
|
for _, s := range x.deletes {
|
|
|
|
if s.path == r.URL.Path && context.Mode&s.mode != 0 {
|
|
|
|
handleError(s.fn(w, r, context), w, context)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if context.Mode != HTMLFULL {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
}
|
2023-09-21 16:43:11 +01:00
|
|
|
LoadBasedOnAnswer(context.Mode, w, "404.html", context.AddMap(nil))
|
2023-09-21 15:38:02 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 16:43:11 +01:00
|
|
|
func CheckAuthLevel(authLevel int, w http.ResponseWriter, r *http.Request, c *Context) bool {
|
2023-09-21 15:38:02 +01:00
|
|
|
if authLevel > 0 {
|
|
|
|
if c.requireAuth(w, r) {
|
2023-09-21 16:43:11 +01:00
|
|
|
Logoff(c.Mode, w, r)
|
2023-09-21 15:38:02 +01:00
|
|
|
return false
|
|
|
|
}
|
2023-09-21 16:43:11 +01:00
|
|
|
if c.User.UserType < authLevel {
|
2023-09-21 15:38:02 +01:00
|
|
|
notAuth(c.Mode, w, r)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func AnswerTemplate(path string, data AnyMap, authLevel int) func(w http.ResponseWriter, r *http.Request, c *Context) *Error {
|
2023-09-19 13:39:59 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request, c *Context) *Error {
|
2023-09-21 16:43:11 +01:00
|
|
|
if !CheckAuthLevel(authLevel, w, r, c) {
|
2023-09-21 15:38:02 +01:00
|
|
|
return nil
|
|
|
|
}
|
2023-09-21 16:43:11 +01:00
|
|
|
LoadBasedOnAnswer(c.Mode, w, path, c.AddMap(data))
|
2023-09-18 13:50:03 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-19 13:39:59 +01:00
|
|
|
type Context struct {
|
|
|
|
Token *string
|
2023-09-21 16:43:11 +01:00
|
|
|
User *dbtypes.User
|
2023-09-21 15:38:02 +01:00
|
|
|
Mode AnswerType
|
2023-09-19 13:39:59 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 16:43:11 +01:00
|
|
|
func (c Context) AddMap(m AnyMap) AnyMap {
|
2023-09-21 15:38:02 +01:00
|
|
|
if m == nil {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"Context": c,
|
|
|
|
}
|
2023-09-19 13:39:59 +01:00
|
|
|
}
|
2023-09-21 15:38:02 +01:00
|
|
|
m["Context"] = c
|
|
|
|
return m
|
2023-09-19 13:39:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) requireAuth(w http.ResponseWriter, r *http.Request) bool {
|
2023-09-21 15:38:02 +01:00
|
|
|
if c.User == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2023-09-19 13:39:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-21 15:38:02 +01:00
|
|
|
// TODO check that the token is still valid
|
|
|
|
|
2023-09-19 13:39:59 +01:00
|
|
|
if token == nil {
|
|
|
|
return &Context{
|
2023-09-21 15:38:02 +01:00
|
|
|
Mode: mode,
|
|
|
|
}, nil
|
2023-09-19 13:39:59 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 16:43:11 +01:00
|
|
|
user, err := dbtypes.UserFromToken(x.Db, *token)
|
2023-09-19 13:39:59 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Join(err, LogoffError)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Context{token, user, mode}, nil
|
|
|
|
}
|
|
|
|
|
2023-09-21 15:38:02 +01:00
|
|
|
// TODO check if I can use http.Redirect
|
2023-09-21 16:43:11 +01:00
|
|
|
func Redirect(path string, mode AnswerType, w http.ResponseWriter, r *http.Request) {
|
2023-09-21 15:38:02 +01:00
|
|
|
w.Header().Set("Location", path)
|
|
|
|
if mode == JSON {
|
|
|
|
w.WriteHeader(http.StatusSeeOther)
|
|
|
|
w.Write([]byte(path))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if mode&(HTMLFULL|HTML) != 0 {
|
2023-09-22 19:22:36 +01:00
|
|
|
w.Header().Add("HX-Redirect", path)
|
|
|
|
w.WriteHeader(204)
|
2023-09-21 15:38:02 +01:00
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-21 16:43:11 +01:00
|
|
|
func Logoff(mode AnswerType, w http.ResponseWriter, r *http.Request) {
|
2023-09-21 15:38:02 +01:00
|
|
|
// Delete cookie
|
|
|
|
cookie := &http.Cookie{
|
|
|
|
Name: "auth",
|
|
|
|
Value: "",
|
|
|
|
Expires: time.Unix(0, 0),
|
|
|
|
}
|
|
|
|
http.SetCookie(w, cookie)
|
2023-09-21 16:43:11 +01:00
|
|
|
Redirect("/login", mode, w, r)
|
2023-09-21 15:38:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func notAuth(mode AnswerType, w http.ResponseWriter, r *http.Request) {
|
|
|
|
if mode == JSON {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
w.Write([]byte("\"You can not access this resource!\""))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if mode&(HTMLFULL|HTML) != 0 {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
w.Write([]byte("You can not access this resource!"))
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-21 16:43:11 +01:00
|
|
|
func (x Handle) StaticFiles(pathTest string, fileType string, contentType string) {
|
2023-09-21 15:38:02 +01:00
|
|
|
http.HandleFunc(pathTest, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
path := r.URL.Path[len(pathTest):]
|
|
|
|
|
|
|
|
if !strings.HasSuffix(path, fileType) {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
w.Write([]byte("File not found"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := template.ParseFiles("./views" + pathTest + path)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
w.Write([]byte("Failed to load template"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", contentType+"; charset=utf-8")
|
|
|
|
t.Execute(w, nil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-09-21 16:43:11 +01:00
|
|
|
func ErrorCode(err error, code int, data AnyMap) *Error {
|
2023-09-21 15:38:02 +01:00
|
|
|
// TODO Improve Logging
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Something went wrong returning with: %d\n.Err:\n", code)
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
return &Error{code, nil, data}
|
|
|
|
}
|
|
|
|
|
2023-09-21 16:43:11 +01:00
|
|
|
func Error500(err error) *Error {
|
|
|
|
return ErrorCode(err, http.StatusInternalServerError, nil)
|
2023-09-21 15:38:02 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 16:43:11 +01:00
|
|
|
func (x Handle) ReadFiles(pathTest string, baseFilePath string, fileType string, contentType string) {
|
2023-09-21 15:38:02 +01:00
|
|
|
http.HandleFunc(pathTest, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
user_path := r.URL.Path[len(pathTest):]
|
|
|
|
|
|
|
|
fmt.Printf("Requested path: %s\n", user_path)
|
|
|
|
|
|
|
|
if !strings.HasSuffix(user_path, fileType) {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
w.Write([]byte("File not found"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes, err := os.ReadFile(path.Join(baseFilePath, pathTest, user_path))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
w.Write([]byte("Failed to load file"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", contentType)
|
|
|
|
w.Write(bytes)
|
|
|
|
})
|
2023-09-19 13:39:59 +01:00
|
|
|
}
|
2023-09-18 13:50:03 +01:00
|
|
|
|
2023-09-19 13:39:59 +01:00
|
|
|
func NewHandler(db *sql.DB) *Handle {
|
2023-09-21 15:38:02 +01:00
|
|
|
|
|
|
|
var gets []HandleFunc
|
|
|
|
var posts []HandleFunc
|
|
|
|
var deletes []HandleFunc
|
|
|
|
x := &Handle{db, gets, posts, deletes}
|
2023-09-18 13:50:03 +01:00
|
|
|
|
2023-09-18 00:26:42 +01:00
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
2023-09-19 13:39:59 +01:00
|
|
|
// Decide answertype
|
2023-09-18 13:50:03 +01:00
|
|
|
ans := NORMAL
|
|
|
|
if r.Header.Get("Request-Type") == "htmlfull" {
|
|
|
|
ans = HTMLFULL
|
|
|
|
}
|
|
|
|
if r.Header.Get("Request-Type") == "html" {
|
|
|
|
ans = HTML
|
|
|
|
}
|
|
|
|
//TODO JSON
|
|
|
|
|
2023-09-19 13:39:59 +01:00
|
|
|
//Login state
|
|
|
|
context, err := x.createContext(ans, r)
|
|
|
|
if err != nil {
|
2023-09-21 16:43:11 +01:00
|
|
|
Logoff(ans, w, r)
|
2023-09-19 13:39:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-18 13:50:03 +01:00
|
|
|
if r.Method == "GET" {
|
2023-09-19 13:39:59 +01:00
|
|
|
x.handleGets(w, r, context)
|
2023-09-18 00:26:42 +01:00
|
|
|
return
|
|
|
|
}
|
2023-09-18 13:50:03 +01:00
|
|
|
if r.Method == "POST" {
|
2023-09-19 13:39:59 +01:00
|
|
|
x.handlePosts(w, r, context)
|
2023-09-18 13:50:03 +01:00
|
|
|
return
|
|
|
|
}
|
2023-09-21 15:38:02 +01:00
|
|
|
if r.Method == "DELETE" {
|
|
|
|
x.handleDeletes(w, r, context)
|
2023-09-18 00:26:42 +01:00
|
|
|
return
|
|
|
|
}
|
2023-09-21 15:38:02 +01:00
|
|
|
panic("TODO handle method: " + r.Method)
|
2023-09-18 13:50:03 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
return x
|
2023-09-18 00:26:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (x Handle) Startup() {
|
|
|
|
fmt.Printf("Starting up!\n")
|
|
|
|
log.Fatal(http.ListenAndServe(":8000", nil))
|
|
|
|
}
|