Fix links in index template

Embed file stuff into app
This commit is contained in:
mStar aka a person 2024-01-08 16:59:45 +01:00
parent fe37258d49
commit 1e60fdb8ff
2 changed files with 77 additions and 21 deletions

76
main.go
View file

@ -1,6 +1,7 @@
package main
import (
"embed"
"fmt"
"html/template"
"log"
@ -8,6 +9,24 @@ import (
"os"
)
const HTML_PREFIX = "<!-- "
const HTML_SUFFIX = " -->"
//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)
}

View file

@ -5,6 +5,12 @@
<link rel="stylesheet" href="/static/styles/index.css">
<meta charset="UTF-8">
<title>m*</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Cantarell&display=swap');
* {
font-family: Cantarell, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to my personal webpage</h1>
@ -24,7 +30,6 @@
<td>German, English</td>
</tr>
</table>
<br>
<h3>You can find me on:</h3>
<table>
@ -34,7 +39,7 @@
</tr>
<tr>
<td>Mastodon</td>
<td><a rel="me" href="https://mk.absturztau.be/@m_star">https://mk.absturztau.be/@m_star</a></td>
<td><a rel="me" href="https://woem.men/@mstar">https://mk.absturztau.be/@m_star</a></td>
</tr>
<tr>
<td>Discord</td>
@ -46,7 +51,7 @@
</tr>
<tr>
<td>Gitlab</td>
<td><a href="https://gitlab.com/beckersam">https://gitlab.com/beckersam</a></td>
<td><a href="https://gitlab.com/mstarongitlab">https://gitlab.com/beckersam</a></td>
</tr>
<tr>
<td>Github</td>
@ -57,15 +62,16 @@
<td><a href="https://twitch.tv/mstarontwitch">https://twitch.tv/mstarontwitch</a></td>
</tr>
</table>
<p>And a fren: <a href="https://foxgirls.love/web">Erika</a></p>
<ul>
<li><a href="https://foxgirls.love/web">Fren Erika</a></li>
<li><a href="https://linktr.ee/akijam">Fren Aki</a></li>
</ul>
<hr>
<p>Todo: Add proper styling</p>
<footer>
<p>Last updated: 30.09.2023: 18:37</p>
<p>Last updated: 08.01.2024: 14:19</p>
<p>
Privacy notice: This webpage uses Google Cloud Run for hosting.
If any tracking beyond generic request counting happens, I am not aware of it.
The server itself does not perform any tracking. I wouldn't even know how to start with that even if I wanted
Privacy notice: This webserver stores no information
</p>
</footer>
</body>