-
Notifications
You must be signed in to change notification settings - Fork 0
/
sidecar.go
77 lines (64 loc) · 1.65 KB
/
sidecar.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
package main
import (
"context"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var (
collection *mongo.Collection
jwtKey string
jitsiIssuer string
jitsiKey string
jitsiUrl string
jitsiId int
)
func main() {
/* Read environment variables */
mongoUri := os.Getenv("MONGODB_URI")
jwtKey = os.Getenv("JWT_KEY")
jitsiIssuer = os.Getenv("JITSI_ISS")
jitsiKey = os.Getenv("SECRET_JITSI_KEY")
jitsiUrl = os.Getenv("JITSI_URL")
jitsiId = 1
/* Check if all variables are set */
if mongoUri == "" {
log.Println("Database information incomplete")
os.Exit(1)
}
if jwtKey == "" {
log.Println("No jwt key set")
os.Exit(1)
}
if jitsiIssuer == "" || jitsiKey == "" || jitsiUrl == "" {
log.Println("Jitsi information incomplete")
os.Exit(1)
}
/* Connect to database */
log.Println("Connecting to database...")
mongoClient, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(mongoUri))
if err != nil {
panic(err)
}
defer func() {
if err := mongoClient.Disconnect(context.TODO()); err != nil {
panic(err)
}
}()
collection = mongoClient.Database("workadventure").Collection("users")
log.Println("Connected to database, setting up API routes...")
/* Set up router */
r := mux.NewRouter()
r.HandleFunc("/auth", authenticate).Methods("GET")
r.HandleFunc("/issuance", issuance).Methods("GET")
r.HandleFunc("/userinfo", userinfo).Methods("GET")
r.HandleFunc("/validate", validate).Methods("GET")
log.Println("Starting up sidecar")
err = http.ListenAndServe(":8080", r)
if err != nil {
log.Fatalln("Sidecar failed: ", err)
}
}