-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
84 lines (70 loc) · 1.96 KB
/
main.go
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
package main
import (
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"time"
"github.com/diamondburned/distant-front/internal/frontend"
"github.com/diamondburned/distant-front/internal/frontend/index"
"github.com/diamondburned/distant-front/internal/workshopimg"
"github.com/diamondburned/distant-front/lib/distance"
"github.com/diamondburned/distant-front/lib/distance/markup"
"github.com/go-chi/chi"
"github.com/joho/godotenv"
)
func main() {
if err := godotenv.Load(); err != nil {
log.Fatalln("failed to load .env:", err)
}
var (
endpoint = os.Getenv("DISTANCE_ENDPOINT")
siteName = os.Getenv("DISTANCE_NAME")
privToken = os.Getenv("DISTANCE_PRIVTOKEN")
listenAddr = os.Getenv("DISTANCE_LISTEN")
observeFq = os.Getenv("DISTANCE_OBSERVEFQ")
)
// Make all colors darker.
markup.ColorModifier = markup.Darken(+0.8, -0.2)
distanceURL, err := url.Parse(endpoint)
if err != nil {
log.Fatalln("invalid $DISTANCE_ENDPOINT:", err)
}
c, err := distance.NewClient(distanceURL.String())
if err != nil {
log.Fatalln("failed to crete Distance client:", err)
}
if privToken != "" {
c.SetPrivateToken(privToken)
}
imgRoute, err := workshopimg.Mount(workshopimg.CacheOpts{
CachePath: filepath.Join(os.TempDir(), "workshopimg.cache"),
})
if err != nil {
log.Println("Warning: workshop cache load error:", err)
}
observeFreq := 500 * time.Millisecond
if observeFq != "" {
d, err := time.ParseDuration(observeFq)
if err != nil {
log.Fatalln("invalid DISTANCE_OBSERVEFQ:", err)
}
observeFreq = d
}
rs := frontend.RenderState{
Client: c,
Observer: distance.NewObserver(c, observeFreq),
SiteName: siteName,
DistanceURL: distanceURL,
}
r := chi.NewRouter()
r.Mount("/workshopimg", imgRoute)
r.Mount("/static", frontend.MountStatic())
r.Mount("/", index.Mount(rs))
if listenAddr == "" {
listenAddr = ":8081"
}
log.Println("Listen and serve at", listenAddr)
log.Fatalln(http.ListenAndServe(listenAddr, r))
}