-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (92 loc) · 2.83 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
package main
import (
"fmt"
"log"
"os"
"strconv"
"github.com/joho/godotenv"
"github.com/ystv/web-auth/views"
)
//go:generate ./node_modules/.bin/mjml -r ./templates/mjml/forgotEmail.mjml -o ./templates/forgotEmail.tmpl
//go:generate ./node_modules/.bin/mjml -r ./templates/mjml/resetEmail.mjml -o ./templates/resetEmail.tmpl
var Version = "unknown"
func main() {
var local, global bool
var err error
err = godotenv.Load(".env") // Load .env
global = err == nil
err = godotenv.Overload(".env.local") // Load .env.local
local = err == nil
signingKey := os.Getenv("WAUTH_SIGNING_KEY")
dbHost := os.Getenv("WAUTH_DB_HOST")
if !local && !global && signingKey == "" && dbHost == "" {
log.Fatal("unable to find env files and no env variables have been supplied")
}
//nolint:gocritic
if !local && !global {
log.Println("using env variables")
} else if local && global {
log.Println("using global and local env files")
} else if !local {
log.Println("using global env file")
} else {
log.Println("using local env file")
}
sessionCookieName := os.Getenv("WAUTH_SESSION_COOKIE_NAME")
if sessionCookieName == "" {
sessionCookieName = "session"
}
dbPort := os.Getenv("WAUTH_DB_PORT")
dbConnectionString := fmt.Sprintf(
"host=%s port=%s user=%s dbname=%s sslmode=%s password=%s",
dbHost,
dbPort,
os.Getenv("WAUTH_DB_USER"),
os.Getenv("WAUTH_DB_NAME"),
os.Getenv("WAUTH_DB_SSLMODE"),
os.Getenv("WAUTH_DB_PASS"),
)
log.Printf("web-auth version: %s\n", Version)
mailPort, _ := strconv.Atoi(os.Getenv("WAUTH_MAIL_PORT"))
debug, _ := strconv.ParseBool(os.Getenv("WAUTH_DEBUG"))
if debug {
log.Println("***running in debug mode, do not use in production***")
}
address := os.Getenv("WAUTH_ADDRESS")
domainName := os.Getenv("WAUTH_DOMAIN_NAME")
// Generate config
conf := &views.Config{
Version: Version,
Debug: debug,
Address: address,
DatabaseURL: dbConnectionString,
BaseDomainName: os.Getenv("WAUTH_BASE_DOMAIN_NAME"),
DomainName: domainName,
LogoutEndpoint: os.Getenv("WAUTH_LOGOUT_ENDPOINT"),
SessionCookieName: sessionCookieName,
CDNEndpoint: os.Getenv("WAUTH_CDN_ENDPOINT"),
Mail: views.SMTPConfig{
Host: os.Getenv("WAUTH_MAIL_HOST"),
Username: os.Getenv("WAUTH_MAIL_USER"),
Password: os.Getenv("WAUTH_MAIL_PASS"),
Port: mailPort,
DomainName: domainName,
},
Security: views.SecurityConfig{
EncryptionKey: os.Getenv("WAUTH_ENCRYPTION_KEY"),
AuthenticationKey: os.Getenv("WAUTH_AUTHENTICATION_KEY"),
SigningKey: signingKey,
},
}
v := views.New(conf, dbHost)
router := NewRouter(&RouterConf{
Config: conf,
Views: v,
})
//nolint:staticcheck
err = router.Start()
//nolint:staticcheck
if err != nil {
log.Fatalf("The web server couldn't be started!\n\n%s\n\nExiting!", err)
}
}