index — crispy-website @ 18d0067840d6c6b6feba476af07e4de3bd544e73

My personal homepage (very crispy)

index.go (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
package main

import (
	"html/template"
	"log"
	"net/http"
)

type Page struct {
	Title string
	Body  []byte
}

var templates = template.Must(template.ParseFiles("templates/index.html"))

func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
	err := templates.ExecuteTemplate(w, tmpl+".html", p)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

func viewHandler(w http.ResponseWriter, r *http.Request) {
	title := r.URL.Path[len("/"):] // Extract title from URL path
	if title == "" {
		title = "Home" // Default title if none provided
	}
	p := &Page{Title: title}
	renderTemplate(w, "index", p)
}

// New handler for plain text response
func plainTextHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/plain")                   // Set content type to plain text
	w.Write([]byte("dh=b81129b4e9cc388c5ab63919550316fc3ca5ebe4")) // Write the plain text response
}

func main() {
	// Serve static files from the "static" directory at the "/static/" path
	fs := http.FileServer(http.Dir("static"))
	http.Handle("/static/", http.StripPrefix("/static/", fs))

	// Set up the main route
	http.HandleFunc("/", viewHandler)

	// Set up a new route for plain text
	http.HandleFunc("/.well-known/discord", plainTextHandler)

	log.Fatal(http.ListenAndServe("0.0.0.0:8080", nil))
}