-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
133 lines (117 loc) · 2.91 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
package main
import (
"context"
"errors"
"log"
"net/http"
"os"
"strings"
_ "github.com/joho/godotenv/autoload"
"github.com/rusher2004/job-board/api/auth"
"github.com/rusher2004/job-board/api/datastore"
"github.com/rusher2004/job-board/api/db"
"github.com/rusher2004/job-board/api/identity"
"github.com/rusher2004/job-board/api/server"
)
func main() {
dbURL, ok := os.LookupEnv("DATABASE_URL")
if !ok {
log.Fatal("DATABASE_URL is not set")
}
db, err := db.NewDB(context.Background(), dbURL)
if err != nil {
log.Fatal(err)
}
defer db.Close()
// setup auth store
authCfg, err := getAuthConfig()
if err != nil {
log.Fatal(err)
}
socCFG, err := getSocialConfigs()
if err != nil {
log.Fatal(err)
}
a := auth.NewAuthStore(authCfg, &db, socCFG)
// setup data store
d := datastore.NewDataStore(&db)
// setup user store
i := identity.NewIdentityStore()
s, err := server.NewServer(&a, d, i)
if err != nil {
log.Fatal(err)
}
log.Println("starting server on port 8080")
log.Fatal(http.ListenAndServe(":8080", s.Router()))
}
func getAuthConfig() (auth.AuthConfig, error) {
var envErrs []string
apiDomain, ok := os.LookupEnv("AUTH_API_DOMAIN")
if !ok {
envErrs = append(envErrs, "AUTH_API_DOMAIN")
}
apiKey, ok := os.LookupEnv("AUTH_API_KEY")
if !ok {
envErrs = append(envErrs, "AUTH_API_KEY")
}
appName, ok := os.LookupEnv("AUTH_APP_NAME")
if !ok {
envErrs = append(envErrs, "AUTH_APP_NAME")
}
basePath, ok := os.LookupEnv("AUTH_BASE_PATH")
if !ok {
envErrs = append(envErrs, "AUTH_BASE_PATH")
}
connURI, ok := os.LookupEnv("AUTH_CONN_URI")
if !ok {
envErrs = append(envErrs, "AUTH_CONN_URI")
}
siteDomain, ok := os.LookupEnv("AUTH_SITE_DOMAIN")
if !ok {
envErrs = append(envErrs, "AUTH_SITE_DOMAIN")
}
siteURI, ok := os.LookupEnv("AUTH_SITE_URI")
if !ok {
envErrs = append(envErrs, "AUTH_SITE_URI")
}
if len(envErrs) > 0 {
msg := "auth env vars missing: " + strings.Join(envErrs, "; ")
return auth.AuthConfig{}, errors.New(msg)
}
return auth.AuthConfig{
APIDomain: apiDomain,
APIKey: apiKey,
AppName: appName,
BasePath: basePath,
ConnURI: connURI,
SiteDomain: siteDomain,
SiteURI: siteURI,
}, nil
}
func getSocialConfigs() ([]auth.SocialConfig, error) {
var envErrs []string
providers := []auth.SocialConfig{
{ProviderName: "github"},
{ProviderName: "google"},
{ProviderName: "linkedin"},
}
for _, p := range providers {
idVar := strings.ToUpper(p.ProviderName) + "_CLIENT_ID"
secretVar := strings.ToUpper(p.ProviderName) + "_CLIENT_SECRET"
clientID, ok := os.LookupEnv(idVar)
if !ok {
envErrs = append(envErrs, secretVar)
}
clientSecret, ok := os.LookupEnv(secretVar)
if !ok {
envErrs = append(envErrs, secretVar)
}
p.ClientID = clientID
p.ClientSecret = clientSecret
}
if len(envErrs) > 0 {
msg := "social env vars missing: " + strings.Join(envErrs, "; ")
return nil, errors.New(msg)
}
return providers, nil
}