2023-09-18 09:16:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-01-08 15:59:45 +00:00
|
|
|
"embed"
|
2023-09-30 16:42:33 +00:00
|
|
|
"fmt"
|
2023-09-18 09:16:23 +00:00
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2024-01-23 13:58:37 +00:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"gitlab.com/mstarongitlab/weblogger"
|
2023-09-18 09:16:23 +00:00
|
|
|
)
|
|
|
|
|
2024-01-08 15:59:45 +00:00
|
|
|
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
|
|
|
|
|
2023-09-18 09:16:23 +00:00
|
|
|
func main() {
|
|
|
|
port := os.Getenv("PORT")
|
|
|
|
if port == "" {
|
|
|
|
port = "8080"
|
|
|
|
}
|
2024-01-23 13:58:37 +00:00
|
|
|
logrus.WithField("port", port).Info("starting server")
|
2023-09-18 09:16:23 +00:00
|
|
|
|
2023-09-30 16:42:33 +00:00
|
|
|
// Custom-ish paths. Includes templates
|
2023-09-18 09:16:23 +00:00
|
|
|
http.HandleFunc("/", handleRoot)
|
2023-09-30 16:42:33 +00:00
|
|
|
|
2024-01-08 15:59:45 +00:00
|
|
|
// Funny awawawa stream
|
|
|
|
http.HandleFunc("/cat/awawawa", awawaStream)
|
|
|
|
|
2023-09-30 16:42:33 +00:00
|
|
|
// static files in /static
|
2024-01-08 15:59:45 +00:00
|
|
|
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(embed_static))))
|
2023-09-30 16:42:33 +00:00
|
|
|
|
|
|
|
// .well-known from /well-known
|
2024-01-08 15:59:45 +00:00
|
|
|
http.Handle("/.well-known/", http.StripPrefix("/.well-known/", http.FileServer(http.FS(embed_well_known))))
|
2023-09-30 16:42:33 +00:00
|
|
|
|
|
|
|
// Static files not in /static or /.well-known
|
2024-01-08 15:59:45 +00:00
|
|
|
http.HandleFunc("/robots.txt", buildHTTPFileReader(embed_robots_txt))
|
|
|
|
http.HandleFunc("/humans.txt", buildHTTPFileReader(embed_humans_txt))
|
2024-01-23 13:58:37 +00:00
|
|
|
|
|
|
|
default_mux := http.DefaultServeMux
|
|
|
|
logger := weblogger.LoggingMiddleware(default_mux)
|
|
|
|
|
|
|
|
if err := http.ListenAndServe(":"+port, logger); err != nil {
|
|
|
|
logrus.WithField("event", "stop server").Fatal(err)
|
2023-09-18 09:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleRoot(w http.ResponseWriter, r *http.Request) {
|
2024-01-23 13:58:37 +00:00
|
|
|
// logRequestInfo(r)
|
2024-01-08 15:59:45 +00:00
|
|
|
tmpl, err := template.ParseFS(embed_templates, "templates/index.html")
|
2023-09-18 09:16:23 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Couldn't parse template file", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-01-08 15:59:45 +00:00
|
|
|
err = tmpl.Execute(w, nil)
|
2023-09-18 09:16:23 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Failed to execute template", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
2023-09-30 16:42:33 +00:00
|
|
|
|
2024-01-08 15:59:45 +00:00
|
|
|
func buildHTTPFileReader(embed_content string) func(w http.ResponseWriter, r *http.Request) {
|
2023-09-30 16:42:33 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-01-23 13:58:37 +00:00
|
|
|
// logRequestInfo(r)
|
2024-01-08 15:59:45 +00:00
|
|
|
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")
|
2023-09-30 16:42:33 +00:00
|
|
|
return
|
2024-01-08 15:59:45 +00:00
|
|
|
default:
|
|
|
|
if i%2 == 0 {
|
|
|
|
fmt.Fprint(w, "a")
|
|
|
|
} else {
|
|
|
|
fmt.Fprint(w, "w")
|
|
|
|
}
|
|
|
|
i += 1
|
2023-09-30 16:42:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-08 15:59:45 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|