-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
140 lines (116 loc) · 3.37 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"context"
"embed"
"errors"
"flag"
"fmt"
"io/fs"
"log"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/coreos/go-systemd/daemon"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/golangcollege/sessions"
"github.com/klauspost/compress/gzhttp"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/theandrew168/jamql/internal/config"
"github.com/theandrew168/jamql/internal/core"
"github.com/theandrew168/jamql/internal/spotify"
"github.com/theandrew168/jamql/internal/test"
"github.com/theandrew168/jamql/internal/web"
)
//go:embed static
var staticFS embed.FS
//go:embed static/img/logo.webp
var logo []byte
func main() {
logger := log.New(os.Stdout, "", log.Lshortfile)
conf := flag.String("conf", "jamql.conf", "app config file")
flag.Parse()
cfg, err := config.ReadFile(*conf)
if err != nil {
logger.Fatalln(err)
}
// create and configure session storage
session := sessions.New([]byte(cfg.SecretKey))
session.Lifetime = 1 * time.Hour
session.HttpOnly = true
session.Secure = true
// use test storage if spotify creds are unset
var storage core.TrackStorage
if cfg.SpotifyClientID == "" || cfg.SpotifyClientSecret == "" {
logger.Println("missing Spotify creds, running with sample tracks")
storage = test.NewTrackStorage()
} else {
storage = spotify.NewTrackStorage(session)
}
app := web.NewApplication(cfg, storage, session, logger)
// setup http.Handler for static files
static, _ := fs.Sub(staticFS, "static")
staticServer := http.FileServer(http.FS(static))
gzipStaticServer := gzhttp.GzipHandler(staticServer)
// construct the top-level router
r := chi.NewRouter()
r.Use(middleware.Recoverer)
r.Mount("/", app.Router())
r.Handle("/metrics", promhttp.Handler())
r.Handle("/static/*", http.StripPrefix("/static", gzipStaticServer))
r.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/webp")
w.Write(logo)
})
r.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("pong"))
})
addr := fmt.Sprintf("127.0.0.1:%s", cfg.Port)
srv := &http.Server{
Addr: addr,
Handler: r,
IdleTimeout: time.Minute,
ReadTimeout: 10 * time.Second,
WriteTimeout: 30 * time.Second,
}
// open up the socket listener
l, err := net.Listen("tcp", addr)
if err != nil {
logger.Fatalln(err)
}
// let systemd know that we are good to go (no-op if not using systemd)
daemon.SdNotify(false, daemon.SdNotifyReady)
logger.Printf("started server on %s\n", addr)
// kick off a goroutine to listen for SIGINT and SIGTERM
shutdownError := make(chan error)
go func() {
// idle until a signal is caught
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
// give the web server 5 seconds to shutdown gracefully
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// shutdown the web server and track any errors
logger.Println("stopping server")
srv.SetKeepAlivesEnabled(false)
err := srv.Shutdown(ctx)
if err != nil {
shutdownError <- err
}
shutdownError <- nil
}()
err = srv.Serve(l)
if !errors.Is(err, http.ErrServerClosed) {
logger.Fatalln(err)
}
// check for shutdown errors
err = <-shutdownError
if err != nil {
logger.Fatalln(err)
}
logger.Println("stopped server")
}