/
main.go
144 lines (116 loc) · 3.34 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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"text/template"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
chiprometheus "github.com/jamscloud/chi-prometheus"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/chi/v5"
cip "github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider"
)
var Version string
type CognitoClient struct {
*cip.Client
AppClientID string
UserPoolID string
}
func Init() *CognitoClient {
// Load the shared AWS config (~/.aws/config)
cfg, err := config.LoadDefaultConfig(context.Background())
if err != nil {
log.Fatal(err)
}
AppID := os.Getenv("COGNITO_APP_CLIENT_ID")
PoolID := os.Getenv("COGNITO_USER_POOL_ID")
log.Println("AWS Region ", cfg.Region)
log.Println("AppID ", AppID)
log.Println("PoolID ", PoolID)
log.Println("Configuring Cognito Client ", cfg.Region)
return &CognitoClient{
cip.NewFromConfig(cfg),
AppID,
PoolID,
}
}
func main() {
appVersion := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "app_version_info",
Help: "App info at buildtime",
ConstLabels: prometheus.Labels{"version": Version},
})
prometheus.Register(appVersion)
appVersion.Set(1)
log.Println("App Version ", Version, ", registered in Prometheus")
cognitoClient := Init()
log.Println("Cognito Client Inititated")
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger, middleware.WithValue("CognitoClient", cognitoClient))
r.Use(middleware.Heartbeat("/ping"))
m := chiprometheus.NewMiddleware("serviceName")
r.Use(m)
r.Use(middleware.Recoverer)
r.Handle("/metrics", promhttp.Handler())
r.HandleFunc("/favicon.ico", faviconHandler)
r.Get("/", http.HandlerFunc(handleIndex))
r.Post("/signup", cognitoClient.signUp)
// r.Post("/signin", signIn)
// r.Get("/verify", verifyToken)
log.Println("Handlers Initiated")
port := os.Getenv("PORT")
if port == "" {
port = "5001"
}
log.Println("Listening Port configured, starting server on:", port)
http.ListenAndServe(fmt.Sprintf(":%s", port), r)
}
func handleIndex(w http.ResponseWriter, r *http.Request) {
data := struct {
PageTitle string
Version string
}{
PageTitle: "Welcome!",
Version: Version,
}
f := filepath.Join(filepath.FromSlash("html"), "SignUp.html")
t, err := template.ParseFiles(f)
if err == nil {
t.Execute(w, data)
} else {
log.Printf("Template error: %q", err)
}
}
func faviconHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "html/favicon.ico")
}
func (cc *CognitoClient) signUp(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
username := r.Form.Get("username")
password := r.Form.Get("password")
// phoneNumber := r.Form.Get("phone_number")
user := &cip.SignUpInput{
Username: aws.String(username),
Password: aws.String(password),
ClientId: aws.String(cc.AppClientID),
}
d := time.Now().Add(time.Minute)
ctx, cancel := context.WithDeadline(context.Background(), d)
defer cancel()
_, err := cc.Client.SignUp(ctx, user)
if err != nil {
fmt.Println(err)
http.Redirect(w, r, fmt.Sprintf("/register?message=%s", err.Error()), http.StatusSeeOther)
return
}
http.Redirect(w, r, fmt.Sprintf("/otp?username=%s", username), http.StatusFound)
}