-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (69 loc) · 2.28 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
package main // import github.com/bcspragu/Radiotation/cmd/server
import (
"context"
"log"
"math/rand"
"net/http"
"os"
"os/signal"
"syscall"
"time"
firebase "firebase.google.com/go"
"github.com/bcspragu/Radiotation/spotify"
"github.com/bcspragu/Radiotation/sqldb"
"github.com/bcspragu/Radiotation/srv"
"github.com/namsral/flag"
"google.golang.org/api/option"
)
var (
addr = flag.String("addr", ":8000", "HTTP service address")
clientID = flag.String("client_id", "", "The Google ClientID to use")
fcmKey = flag.String("fcm_key", "", "The Firebase Cloud Messaging Key to use")
spotifyClient = flag.String("spotify_client_id", "", "The client ID of the Spotify application")
spotifySecret = flag.String("spotify_secret", "", "The secret of the Spotify application")
projectID = flag.String("project_id", "", "The Firebase/GCP project ID to authenticate with.")
creds = flag.String("service_account_creds", "", "The location of the JSON-formatted service account credentials.")
dbPath = flag.String("db_path", "", "The location to store/load the SQLite database.")
)
func main() {
rand.Seed(time.Now().Unix())
flag.Parse()
if *clientID == "" || *spotifyClient == "" || *spotifySecret == "" {
log.Fatalf("Missing a required flag, all of --client_id, --spotify_client_id, and --spotify_secret are required.")
}
db, err := sqldb.New(*dbPath, sqldb.CryptoRandSource{})
if err != nil {
log.Fatalf("Failed to initialize datastore: %v", err)
}
ctx := context.Background()
app, err := firebase.NewApp(ctx, &firebase.Config{
ProjectID: *projectID,
}, option.WithCredentialsFile(*creds))
if err != nil {
log.Fatalf("Failed to initialize Firebase: %v", err)
}
auth, err := app.Auth(ctx)
if err != nil {
log.Fatalf("Failed to initialize Firebase Auth: %v", err)
}
s, err := srv.New(db, &srv.Config{
ClientID: *clientID,
FCMKey: *fcmKey,
AuthClient: auth,
SongServer: spotify.NewSongServer("spotify.com", *spotifyClient, *spotifySecret),
})
if err != nil {
log.Fatalf("Failed to start DB: %v", err)
}
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-c
db.Close()
os.Exit(1)
}()
err = http.ListenAndServe(*addr, s)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}