moving to custom swap engine

Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
ari melody 2024-04-16 17:53:24 +01:00
parent 749f9bc8b7
commit 13d802d361
21 changed files with 4361 additions and 225 deletions

64
main.go
View file

@ -6,7 +6,6 @@ import (
"log"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
@ -37,7 +36,7 @@ var base_template = template.Must(template.ParseFiles(
"views/footer.html",
"views/prideflag.html",
))
var htmx_template = template.Must(template.New("root").Parse(`<head>{{block "head" .}}{{end}}</head>{{block "content" .}}{{end}}`))
// var htmx_template = template.Must(template.New("root").Parse(`<head>{{block "head" .}}{{end}}</head>{{block "content" .}}{{end}}`))
func log_request(req *http.Request, code int, start_time time.Time) {
now := time.Now()
@ -61,25 +60,27 @@ func handle_request(writer http.ResponseWriter, req *http.Request) {
uri := req.URL.Path
start_time := time.Now()
hx_request := len(req.Header["Hx-Request"]) > 0 && req.Header["Hx-Request"][0] == "true"
// don't bother fulfilling requests to a page that's already loaded on the client!
if hx_request && len(req.Header["Referer"]) > 0 && len(req.Header["Hx-Current-Url"]) > 0 {
regex := regexp.MustCompile(`https?:\/\/[^\/]+`)
current_location := regex.ReplaceAllString(req.Header["Hx-Current-Url"][0], "")
if current_location == req.URL.Path {
writer.WriteHeader(204);
return
}
}
// hx_request := len(req.Header["Hx-Request"]) > 0 && req.Header["Hx-Request"][0] == "true"
//
// // don't bother fulfilling requests to a page that's already loaded on the client!
// if hx_request && len(req.Header["Referer"]) > 0 && len(req.Header["Hx-Current-Url"]) > 0 {
// regex := regexp.MustCompile(`https?:\/\/[^\/]+`)
// current_location := regex.ReplaceAllString(req.Header["Hx-Current-Url"][0], "")
// if current_location == req.URL.Path {
// writer.WriteHeader(204);
// return
// }
// }
code := func(writer http.ResponseWriter, req *http.Request) int {
var root *template.Template
if hx_request {
root = template.Must(htmx_template.Clone())
} else {
root = template.Must(base_template.Clone())
}
// var root *template.Template
// if hx_request {
// root = template.Must(htmx_template.Clone())
// } else {
// root = template.Must(base_template.Clone())
// }
var root = template.Must(base_template.Clone())
if req.URL.Path == "/" {
return index_handler(writer, root)
@ -93,7 +94,7 @@ func handle_request(writer http.ResponseWriter, req *http.Request) {
return music_gateway_handler(writer, req, root)
}
return static_handler(writer, req)
return static_handler(writer, req, root)
}(writer, req)
log_request(req, code, start_time)
@ -126,8 +127,7 @@ func music_gateway_handler(writer http.ResponseWriter, req *http.Request, root *
// return
release, ok := music.GetRelease(id)
if !ok {
http.Error(writer, "404 not found", http.StatusNotFound)
return 404
return handle_not_found(writer, req, root)
}
gateway_template := template.Must(root.ParseFiles("views/music-gateway.html"))
err := gateway_template.Execute(writer, release)
@ -138,14 +138,13 @@ func music_gateway_handler(writer http.ResponseWriter, req *http.Request, root *
return 200
}
func static_handler(writer http.ResponseWriter, req *http.Request) int {
func static_handler(writer http.ResponseWriter, req *http.Request, root *template.Template) int {
filename := "public/" + req.URL.Path[1:]
// check the file's metadata
info, err := os.Stat(filename)
if err != nil {
http.Error(writer, "404 not found", http.StatusNotFound)
return 404
return handle_not_found(writer, req, root)
}
if len(req.Header["If-Modified-Since"]) > 0 && req.Header["If-Modified-Since"][0] != "" {
@ -185,6 +184,21 @@ func static_handler(writer http.ResponseWriter, req *http.Request) int {
return 200
}
func handle_not_found(writer http.ResponseWriter, req *http.Request, root *template.Template) int {
type ErrorData struct {
Target string
}
error_data := ErrorData{ Target: req.URL.Path }
writer.WriteHeader(404);
error_template := template.Must(root.ParseFiles("views/404.html"))
err := error_template.Execute(writer, error_data)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return 500
}
return 404
}
func parse_markdown(md []byte) []byte {
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
p := parser.NewWithExtensions(extensions)