/
main.go
96 lines (78 loc) · 2.66 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
package main
import (
"context"
"encoding/base64"
"log"
"os"
"github.com/bfallik/cohabitaters"
"github.com/bfallik/cohabitaters/cohabdb"
"github.com/bfallik/cohabitaters/handlers"
"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"golang.org/x/oauth2/google"
"google.golang.org/api/people/v1"
)
const defaultListenAddress = "localhost:8080"
const defaultDBFile = "file:cohab.db"
func main() {
log.Printf("%s", cohabitaters.BuildInfo())
listenAddress, ok := os.LookupEnv("LISTEN_ADDRESS")
if !ok {
listenAddress = defaultListenAddress
}
googleAppCredentials := os.Getenv("GOOGLE_APP_CREDENTIALS")
oauthConfig, err := google.ConfigFromJSON([]byte(googleAppCredentials), people.ContactsReadonlyScope, people.UserinfoEmailScope)
if err != nil {
log.Fatalf("unable to create Google oauth2 config: %v", err)
}
oauthConfig.Endpoint = google.Endpoint
cookieStoreKey, ok := os.LookupEnv("COOKIE_HASH_BLOCK_KEYS")
if !ok {
log.Fatalf("empty COOKIE_HASH_BLOCK_KEYS")
}
keys, err := base64.StdEncoding.DecodeString(cookieStoreKey)
if err != nil {
log.Fatalf("unable to decode COOKIE_HASH_BLOCK_KEYS")
}
if len(keys) != 96 {
log.Fatalf("unexpected key length: %d", len(keys))
}
store := sessions.NewCookieStore(keys[0:64], keys[64:96])
db, err := cohabdb.Open(defaultDBFile)
if err != nil {
log.Fatalf("database open: %v", err)
}
ctx := context.Background()
if err := cohabdb.CreateTables(ctx, db); err != nil {
log.Fatalf("unable to create tables: %v", err)
}
queries := cohabdb.New(db)
e := echo.New()
e.Use(middleware.Logger())
e.Use(session.Middleware(store))
e.Use(middleware.Secure())
dbgHandler := handlers.Debug{}
oauthHandler := handlers.Oauth2{
OauthConfig: oauthConfig,
Queries: queries,
}
webUIHandler := handlers.WebUI{
OauthConfig: oauthConfig,
Queries: queries,
}
e.GET("/static/fontawesome/*", handlers.FontAwesome)
e.GET("/static/tailwindcss/*", handlers.Tailwind)
e.GET("/", webUIHandler.Root)
e.GET("/partial/tableResults", webUIHandler.PartialTableResults)
e.GET("/about", handlers.About)
e.GET("/error", handlers.Error)
e.GET("/logout", webUIHandler.Logout)
e.GET("/auth/google/callback", oauthHandler.GoogleCallbackAuthz).Name = handlers.RedirectURLAuthz
e.GET("/auth/google/login", oauthHandler.GoogleLoginAuthz).Name = handlers.RedirectURLAuthzLogin
e.GET("/auth/google/force-approval", oauthHandler.GoogleForceApproval)
e.POST("/authn/google/callback", oauthHandler.GoogleCallbackAuthn).Name = handlers.RedirectURLAuthn
e.GET("/debug/buildinfo", dbgHandler.BuildInfo)
e.Logger.Fatal(e.Start(listenAddress))
}