summaryrefslogtreecommitdiffstats
path: root/roseh.moe.go
blob: ea7f4a37b0603457b8f9a689fa71ec31bcbbb463 (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
package main

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

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

func index(w http.ResponseWriter, _ *http.Request) {
	fmt.Fprint(w, `<!DOCTYPE html>
<html lang="en">
	<head>
		<title>roseh.moe</title>
	</head>
	<body>
		<p>Hello world</p>
	</body>
</html>
`)
}

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

var 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 {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}

//go:embed static
var static embed.FS

func main() {
	flag.Parse()

	jellyfinURL, err := url.Parse("http://localhost:8096")
	if err != nil {
		log.Fatalf("Jellyfin URL: %s", err)
	}

	mux := http.NewServeMux()
	jellyfinProxy := httputil.NewSingleHostReverseProxy(jellyfinURL)
	mux.Handle("/cinema", jellyfinProxy)
	mux.Handle("/cinema/", jellyfinProxy)
	mux.Handle("/static/", http.FileServerFS(static))
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path == "/" {
			index(w, r)
		} else {
			notFound(w, r)
		}
	})

	httpServer := http.Server{
		Addr:              fmt.Sprintf(":%d", *port),
		Handler:           mux,
		ReadHeaderTimeout: 5 * time.Second,
		WriteTimeout:      5 * time.Second,
	}

	log.Printf("Listening on %q", httpServer.Addr)
	if *https {
		log.Fatal(httpServer.ListenAndServeTLS("/var/lib/acme/roseh.moe/cert.pem", "/var/lib/acme/roseh.moe/key.pem"))
	} else {
		log.Fatal(httpServer.ListenAndServe())
	}
}