summaryrefslogtreecommitdiffhomepage
path: root/derelict.go
blob: 2f5e8b31abcf0db084261b9eb15f0950cd8e4d66 (plain)
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
package main

import (
	"embed"
	"github.com/gorilla/mux"
	"html/template"
	"log"
	"net/http"
	"os"
)

//go:embed *.json *.css *.js *.svg
var content embed.FS

//go:embed *.tmpl
var internal embed.FS

var templates *template.Template

func main() {
	port := os.Getenv("DERELICTPORT")
	if port == "" {
		port = "8080"
	}
	templates = template.Must(template.ParseFS(internal, "*.tmpl"))

	router := mux.NewRouter()
	router.HandleFunc("/", handleRecent).Methods("GET")
	router.HandleFunc("/view", handleView).Queries("id", "{id:[_0-9]+}").Methods("GET")
	router.PathPrefix("/").Handler(http.FileServer(http.FS(content)))
	log.Fatal(http.ListenAndServe(":"+port, router))
}

func handleRecent(w http.ResponseWriter, r *http.Request) {
	templates.ExecuteTemplate(w, "recent", "")
}

func handleView(w http.ResponseWriter, r *http.Request) {
	templates.ExecuteTemplate(w, "view", "")
}