-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
224 lines (183 loc) · 5.47 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"database/sql"
"encoding/gob"
"fmt"
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/aws/aws-sdk-go/service/ses"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/sessions"
"github.com/hyprcubd/dgraphql"
_ "github.com/joho/godotenv/autoload"
"gopkg.in/auth0.v5/management"
)
var (
ss *sessions.CookieStore
sesSvc *ses.SES
s3Uploader *s3manager.Uploader
db *sql.DB
authenticator *Authenticator
t = template.Must(template.ParseGlob("templates/*"))
auth0ClientID string
auth0ClientSecret string
auth0RedirectURL string
auth0LogoutURL string
hereToken string
dgraphToken string
authManager *management.Management
dc *dgraphql.Client
)
const (
auth0DBConnectionID = "con_p2bySCWPL9MKhmH1"
)
func main() {
awsRegion, exists := os.LookupEnv("AWS_DEFAULT_REGION")
if !exists {
awsRegion = "us-east-2"
}
user, exists := os.LookupEnv("MYSQL_USER")
if !exists {
panic("MYSQL_USER not found")
}
pass, exists := os.LookupEnv("MYSQL_PASS")
if !exists {
panic("MYSQL_PASS not found")
}
host, exists := os.LookupEnv("MYSQL_HOST")
if !exists {
panic("MYSQL_HOST not found")
}
mysqldb, exists := os.LookupEnv("MYSQL_DB")
if !exists {
panic("MYSQL_DB not found")
}
auth0ClientID, exists = os.LookupEnv("AUTH0_CLIENT_ID")
if !exists {
panic("AUTH0_CLIENT_ID not found")
}
auth0ClientSecret, exists = os.LookupEnv("AUTH0_CLIENT_SECRET")
if !exists {
panic("AUTH0_CLIENT_SECRET not found")
}
auth0RedirectURL, exists = os.LookupEnv("AUTH0_REDIRECT_URL")
if !exists {
panic("AUTH0_REDIRECT_URL not found")
}
auth0LogoutURL, exists = os.LookupEnv("AUTH0_LOGOUT_URL")
if !exists {
panic("AUTH0_LOGOUT_URL not found")
}
hereToken, exists = os.LookupEnv("HERE_TOKEN")
if !exists {
panic("HERE_TOKEN not found")
}
dgraphToken, exists = os.LookupEnv("DGRAPH_TOKEN")
if !exists {
panic("DGRAPH_TOKEN not found")
}
var err error
authManager, err = management.New("reusefull.us.auth0.com", management.WithClientCredentials(auth0ClientID, auth0ClientSecret))
if err != nil {
panic(err)
}
authenticator, err = NewAuthenticator()
if err != nil {
panic(err)
}
sess, err := session.NewSession(&aws.Config{
Region: aws.String(awsRegion)})
if err != nil {
panic(err)
}
// Simple Email Service
sesSvc = ses.New(sess)
// S3 Uploader
s3Uploader = s3manager.NewUploader(sess)
db, err = sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?parseTime=true&timeout=10s", user, pass, host, mysqldb))
if err != nil {
panic(err)
}
defer db.Close()
db.SetConnMaxLifetime(time.Minute * 5)
db.SetMaxOpenConns(5)
db.SetMaxIdleConns(2)
ss = sessions.NewCookieStore([]byte("8dp/Kx2veOxt1RdXMBMvlLbwH6oFJDofQyQ1pPodbjQ"))
gob.Register(map[string]interface{}{})
r := chi.NewRouter()
r.Use(middleware.Logger)
workDir, _ := os.Getwd()
filesDir := http.Dir(filepath.Join(workDir, "assets"))
FileServer(r, "/assets", filesDir)
r.Group(func(r chi.Router) {
r.Use(AuthMiddleware)
r.Get("/", IndexPage)
r.Get("/charitylist", ListCharities)
r.Get("/charity/{id}", ViewCharity)
r.Get("/charity/{id}/edit", EditCharity)
r.Put("/api/v1/charity/{id}", UpdateCharity)
r.Get("/auth/callback", CallbackHandler)
r.Get("/auth/login", LoginHandler)
r.Get("/auth/logout", LogoutHandler)
r.Get("/charity/signup/step/1", CharitySignUp1)
r.Get("/charity/signup/step/2", CharitySignUp2)
r.Get("/charity/signup/step/3", CharitySignUp3)
r.Get("/charity/signup/thankyou", CharitySignUpThanks)
r.Get("/donate", Donate)
r.Get("/donate/results", DonateSearchResults)
r.Get("/admin", AdminView)
})
r.Route("/api/v1", func(r chi.Router) {
r.Post("/charity/register", CharityRegister)
r.Get("/charity/types", GetCharityTypes)
r.Get("/charity/{id}", GetCharity)
r.Post("/auth0/cb", ChangePasswordCallback)
r.Post("/donate/search", DonateSearch)
r.Post("/charity/{id}/contact", CharityContact)
// Admin only api
r.Group(func(r chi.Router) {
r.Use(AuthMiddleware)
r.Put("/charity/{id}/approve", AdminCharityApprove)
r.Put("/charity/{id}/deny", AdminCharityDeny)
})
})
srv := &http.Server{
Addr: ":80",
WriteTimeout: 30 * time.Second,
ReadTimeout: 30 * time.Second,
Handler: r,
}
log.Println("Succesfully started")
err = srv.ListenAndServe()
if err != http.ErrServerClosed {
log.Fatalf("HTTP server ListenAndServe: %v", err)
}
}
// FileServer conveniently sets up a http.FileServer handler to serve
// static files from a http.FileSystem.
func FileServer(r chi.Router, path string, root http.FileSystem) {
if strings.ContainsAny(path, "{}*") {
panic("FileServer does not permit any URL parameters.")
}
if path != "/" && path[len(path)-1] != '/' {
r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
path += "/"
}
path += "*"
r.Get(path, func(w http.ResponseWriter, r *http.Request) {
rctx := chi.RouteContext(r.Context())
pathPrefix := strings.TrimSuffix(rctx.RoutePattern(), "/*")
fs := http.StripPrefix(pathPrefix, http.FileServer(root))
fs.ServeHTTP(w, r)
})
}