From 1e60fdb8ff7bf8cd7c9a6570cb44da24ea85b2b0 Mon Sep 17 00:00:00 2001 From: mStar aka a person <12024604-mstarongitlab@users.noreply.gitlab.com> Date: Mon, 8 Jan 2024 16:59:45 +0100 Subject: [PATCH] Fix links in index template Embed file stuff into app --- main.go | 76 ++++++++++++++++++++++++++++++++++++-------- templates/index.html | 22 ++++++++----- 2 files changed, 77 insertions(+), 21 deletions(-) diff --git a/main.go b/main.go index aa56231..2523a8e 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "embed" "fmt" "html/template" "log" @@ -8,6 +9,24 @@ import ( "os" ) +const HTML_PREFIX = "" + +//go:embed templates +var embed_templates embed.FS + +//go:embed static +var embed_static embed.FS + +//go:embed well-known +var embed_well_known embed.FS + +// go: embed robots.txt +var embed_robots_txt string + +// go: embed humans.txt +var embed_humans_txt string + func main() { port := os.Getenv("PORT") if port == "" { @@ -18,39 +37,70 @@ func main() { // Custom-ish paths. Includes templates http.HandleFunc("/", handleRoot) + // Funny awawawa stream + http.HandleFunc("/cat/awawawa", awawaStream) + // static files in /static - http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static")))) + http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(embed_static)))) // .well-known from /well-known - http.Handle("/.well-known/", http.StripPrefix("/.well-known/", http.FileServer(http.Dir("./well-known")))) + http.Handle("/.well-known/", http.StripPrefix("/.well-known/", http.FileServer(http.FS(embed_well_known)))) // Static files not in /static or /.well-known - http.HandleFunc("/robots.txt", buildHTTPFileReader("robots.txt")) - http.HandleFunc("/humans.txt", buildHTTPFileReader("humans.txt")) + http.HandleFunc("/robots.txt", buildHTTPFileReader(embed_robots_txt)) + http.HandleFunc("/humans.txt", buildHTTPFileReader(embed_humans_txt)) if err := http.ListenAndServe(":"+port, nil); err != nil { log.Fatal(err) } } func handleRoot(w http.ResponseWriter, r *http.Request) { - tmpl, err := template.ParseFiles("templates/index.html") + tmpl, err := template.ParseFS(embed_templates, "templates/index.html") if err != nil { http.Error(w, "Couldn't parse template file", http.StatusInternalServerError) return } - err = tmpl.Execute(w, "Nothing lol") + err = tmpl.Execute(w, nil) if err != nil { http.Error(w, "Failed to execute template", http.StatusInternalServerError) } } -func buildHTTPFileReader(path string) func(w http.ResponseWriter, r *http.Request) { +func buildHTTPFileReader(embed_content string) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { - file, err := os.ReadFile(path) - if err != nil { - http.Error(w, "Failed to load file", http.StatusInternalServerError) - return - } - fmt.Fprintf(w, "%s", file) + fmt.Fprintf(w, "%s", embed_content) } } + +func awawaStream(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html") + w.WriteHeader(206) + w.Header().Set("Status", "206") + genStub(1024, w) // Hardcoded. Firefox & Chrome both have this value and a len of 0 wouldn't work + fmt.Println("Starting awawawa stream") + i := 0 + for { + select { + case <-r.Context().Done(): + fmt.Println("awawawa Stream done") + return + default: + if i%2 == 0 { + fmt.Fprint(w, "a") + } else { + fmt.Fprint(w, "w") + } + i += 1 + } + } +} + +func genStub(length int, w http.ResponseWriter) { + PreSufLen := len(HTML_PREFIX) + len(HTML_SUFFIX) + fmt.Fprint(w, HTML_PREFIX) + for i := 0; i < length-PreSufLen; i++ { + fmt.Fprint(w, '\u0020') + } + fmt.Fprint(w, HTML_SUFFIX) + +} diff --git a/templates/index.html b/templates/index.html index d9cead2..db9b0d3 100644 --- a/templates/index.html +++ b/templates/index.html @@ -5,6 +5,12 @@ m* +

Welcome to my personal webpage

@@ -24,7 +30,6 @@ German, English -

You can find me on:

@@ -34,7 +39,7 @@ - + @@ -46,7 +51,7 @@ - + @@ -57,15 +62,16 @@
Mastodonhttps://mk.absturztau.be/@m_starhttps://mk.absturztau.be/@m_star
Discord
Gitlabhttps://gitlab.com/beckersamhttps://gitlab.com/beckersam
Githubhttps://twitch.tv/mstarontwitch
-

And a fren: Erika

+

Todo: Add proper styling