-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
148 lines (126 loc) · 3.5 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"context"
"database/sql"
"encoding/gob"
"log/slog"
"net/http"
"os"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/gorilla/sessions"
_ "github.com/lib/pq"
"golang.org/x/oauth2"
)
type Person struct {
Id int
Name string
Email string
}
var (
db *sql.DB
logLevel string
oauthConfig *oauth2.Config
sessionStore *sessions.CookieStore
provider *oidc.Provider
logger *slog.Logger
requestLogger *slog.Logger
)
func init() {
slog.Info("Initializing application")
// Initialize the logger.
logLevelEnvironmentVariable := os.Getenv("LOG_LEVEL")
slog.Info("Environment Variables", "LOG_LEVEL", logLevelEnvironmentVariable)
logLevel := slog.LevelInfo
switch logLevelEnvironmentVariable {
case "DEBUG":
logLevel = slog.LevelDebug
case "INFO":
logLevel = slog.LevelInfo
case "WARN":
logLevel = slog.LevelWarn
case "ERROR":
logLevel = slog.LevelError
default:
logLevel = slog.LevelInfo
}
opts := &slog.HandlerOptions{
Level: slog.Level(logLevel),
}
textHandler := slog.NewTextHandler(os.Stdout, opts)
logger = slog.New(textHandler)
logger.Debug(
"Environment variables",
"POSTGRES_URL", os.Getenv("POSTGRES_URL"),
"OIDC_PROVIDER_URL", os.Getenv("OIDC_PROVIDER_URL"),
"CLIENT_ID", os.Getenv("CLIENT_ID"),
"CLIENT_SECRET", os.Getenv("CLIENT_SECRET"),
"CALLBACK_URL", getRedirectURL(),
"SESSION_KEY", os.Getenv("SESSION_KEY"),
)
dbURL := os.Getenv("POSTGRES_URL")
conn, err := sql.Open("postgres", dbURL)
if err != nil {
panic(err)
}
db = conn
provider, err = oidc.NewProvider(context.Background(), os.Getenv("OIDC_PROVIDER_URL"))
if err != nil {
panic(err)
}
oauthConfig = &oauth2.Config{
ClientID: os.Getenv("CLIENT_ID"),
ClientSecret: os.Getenv("CLIENT_SECRET"),
Endpoint: provider.Endpoint(),
RedirectURL: getRedirectURL(),
Scopes: []string{"openid", "profile", "email", "offline"},
}
sessionStore = sessions.NewCookieStore([]byte(os.Getenv("SESSION_KEY")))
gob.Register(UserProfile{})
}
func getLogoutRedirectURL() string {
url := os.Getenv("RENDER_EXTERNAL_URL")
if url == "" {
url = "http://localhost:3000"
}
return url
}
func getRedirectURL() string {
url := os.Getenv("RENDER_EXTERNAL_URL")
if url == "" {
url = "http://localhost:3000"
}
return url + "/callback"
}
func getProfile(r *http.Request) UserProfile {
session, err := sessionStore.Get(r, "session")
if err != nil || len(session.Values) == 0 {
return UserProfile{}
}
profile, exists := session.Values["profile"]
if !exists {
return UserProfile{}
}
profileValue, ok := profile.(UserProfile)
if !ok {
logger.Debug("Profile did not parse into User Profile", "profile", profile)
return UserProfile{}
}
logger.Debug("Profile", "profile", profileValue)
return profileValue
}
func main() {
handler := slog.NewJSONHandler(os.Stdout, nil)
webErrorLogger := slog.NewLogLogger(handler, slog.LevelError)
logger.Info("Starting server on port 3000")
http.Handle("/contact/", LoggingMiddleware((AuthMiddleware(http.HandlerFunc(Handler)))))
http.Handle("/contact", LoggingMiddleware((AuthMiddleware(http.HandlerFunc(Handler)))))
http.Handle("/login", LoggingMiddleware(http.HandlerFunc(LoginHandler)))
http.Handle("/callback", LoggingMiddleware(http.HandlerFunc(CallbackHandler)))
http.Handle("/logout", LoggingMiddleware(http.HandlerFunc(LogoutHandler)))
http.Handle("/", LoggingMiddleware(http.HandlerFunc(IndexHandler)))
server := http.Server{
ErrorLog: webErrorLogger,
Addr: ":3000",
}
server.ListenAndServe()
}