summaryrefslogtreecommitdiffstats
path: root/roseh.moe.go
blob: 8ca88729e8c9f5aa63f67746d9d2d98ba67af276 (plain) (blame)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main

import (
	"embed"
	"flag"
	"fmt"
	"html/template"
	"io"
	"log"
	"net/http"
	"net/http/httputil"
	"net/url"
	"strings"
	"time"
)

var (
	port  = flag.Int("port", 42069, "port to listen on")
	https = flag.Bool("https", false, "use https")

	serverStartTime = time.Now()
)

var (
	//go:embed templates/404.html.template
	notFoundString string

	notFoundTemplate = template.Must(template.New("notFound").Parse(notFoundString))
)

func notFound(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusNotFound)
	type args struct {
		Path string
	}
	if err := notFoundTemplate.Execute(w, args{Path: r.URL.Path}); err != nil {
		log.Printf("Warning: notFound: %s", err)
	}
}

var (
	//go:embed templates/outline.html.template
	outlineString string

	outlineTemplate = template.Must(template.New("outline").Parse(outlineString))
)

var (
	//go:embed templates/index.html.template
	indexString string

	indexTemplate = template.Must(template.Must(outlineTemplate.Clone()).New("body").Parse(indexString))
)

func index(w http.ResponseWriter, _ *http.Request) {
	if err := indexTemplate.ExecuteTemplate(w, "outline", nil); err != nil {
		log.Printf("Warning: index: %s", err)
	}
}

var (
	//go:embed templates/pong.html.template
	pongString string

	pongTemplate = template.Must(template.New("pong").Parse(pongString))
)

func pong(w http.ResponseWriter, _ *http.Request) {
	if err := pongTemplate.Execute(w, nil); err != nil {
		log.Printf("Warning: pong: %s", err)
	}
}

//go:embed static
var staticFiles embed.FS

func serveStaticFile(w http.ResponseWriter, r *http.Request, path string) {
	f, err := staticFiles.Open(path)
	if err != nil {
		notFound(w, r)
		return
	}
	defer f.Close()
	http.ServeContent(w, r, path, serverStartTime, f.(io.ReadSeeker))
}

func static(w http.ResponseWriter, r *http.Request) {
	serveStaticFile(w, r, strings.TrimPrefix(r.URL.Path, "/"))
}

func favicon(w http.ResponseWriter, r *http.Request) {
	serveStaticFile(w, r, "static/favicon.ico")
}

func main() {
	flag.Parse()

	http.HandleFunc("GET /pong", pong)
	jellyfinURL, err := url.Parse("http://localhost:8096")
	if err != nil {
		log.Fatalf("Jellyfin URL: %s", err)
	}
	http.Handle("/cinema/", httputil.NewSingleHostReverseProxy(jellyfinURL))
	http.HandleFunc("GET /static/", static)
	http.HandleFunc("GET /favicon.ico", favicon)
	http.HandleFunc("GET /{$}", index)
	notFoundMux := http.NewServeMux()
	notFoundMux.HandleFunc("GET /", notFound)
	http.Handle("/", notFoundMux)

	addr := fmt.Sprintf(":%d", *port)
	log.Printf("Listening on %q", addr)
	if *https {
		log.Fatal(http.ListenAndServeTLS(addr, "/var/lib/acme/roseh.moe/cert.pem", "/var/lib/acme/roseh.moe/key.pem", nil))
	} else {
		log.Fatal(http.ListenAndServe(addr, nil))
	}
}