This repository has been archived by the owner on Apr 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
99 lines (84 loc) · 2.29 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
package triangulate
import (
"github.com/gorilla/mux"
"image"
"image/png"
"log"
"math/rand"
"net/http"
"os"
"time"
)
func Run() {
closeHandler()
initSettings()
initQueue()
initDatabase()
initSessions()
httpRateLimiter := initRatelimiter()
staticRouter := mux.NewRouter()
fs := http.FileServer(http.Dir("./assets/build/static"))
staticRouter.PathPrefix("/").Handler(http.StripPrefix("/static/", fs))
apiRouter := mux.NewRouter()
apiRouter.HandleFunc("/api/v1/login", login)
apiRouter.HandleFunc("/api/v1/logout", logout)
apiRouter.HandleFunc("/api/v1/register", register)
apiRouter.HandleFunc("/api/v1/forgot-password", forgotPassword)
apiRouter.HandleFunc("/api/v1/reset-password", resetPassword)
apiRouter.HandleFunc("/api/v1/settings", settings)
apiRouter.HandleFunc("/api/v1/portal", portal)
apiRouter.HandleFunc("/api/v1/stripe/webhook", handleWebhook)
apiRouter.HandleFunc("/api/v1/generate", generate)
apiRouter.HandleFunc("/api/v1/generate/{id}", generatePoll)
apiRouter.HandleFunc("/api/v1/img/{id}.png", img)
apiRouter.Use(AuthMiddleware)
apiRouter.Use(SensitiveHeadersMiddleware)
r := mux.NewRouter()
r.PathPrefix("/api/v1/").Handler(httpRateLimiter.RateLimit(apiRouter))
r.PathPrefix("/static/").Handler(staticRouter)
fs2 := http.FileServer(http.Dir("./assets/build"))
r.Path("/robots.txt").Handler(fs2)
r.Path("/asset-manifest.json").Handler(fs2)
pages := mux.NewRouter()
pages.PathPrefix("/").HandlerFunc(index)
pages.Use(StripeSessionMiddleware)
r.PathPrefix("/").Handler(pages)
r.Use(GeneralHeadersMiddleware)
log.Printf("Listening on %s\n", addr)
err := http.ListenAndServe(addr, r)
if err != nil {
log.Fatal(err)
}
}
func triangulate() {
log.Println("triangulate done")
}
func saveOutput(img image.Image, filePath string) error {
f, err := os.Create(filePath)
if err != nil {
return err
}
defer f.Close()
// Encode to `PNG` with `DefaultCompression` level
// then save to file
err = png.Encode(f, img)
if err != nil {
return err
}
return nil
}
func init() {
rand.Seed(time.Now().UnixNano())
if _, err := os.Stat(sourceDir); os.IsNotExist(err) {
err = os.MkdirAll(sourceDir, 0744)
if err != nil {
panic(err)
}
}
if _, err := os.Stat(outDir); os.IsNotExist(err) {
err = os.MkdirAll(outDir, 0744)
if err != nil {
panic(err)
}
}
}