-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
120 lines (102 loc) · 2.73 KB
/
main.go
File metadata and controls
120 lines (102 loc) · 2.73 KB
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
package main
import (
"crypto/tls"
"fmt"
"log"
"log/slog"
"net/http"
"os"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/go-chi/httprate"
"github.com/humancorners/humancorners/apps/server/api"
apiutil "github.com/humancorners/humancorners/apps/server/api/utils"
"github.com/humancorners/humancorners/apps/server/moderation"
"github.com/joho/godotenv"
)
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
router := chi.NewRouter()
production := os.Getenv("PRODUCTION") == "true"
if production {
godotenv.Load()
} else {
godotenv.Load(".env.local")
}
setupCORS(router)
setupMiddlewares(router)
setupRoutes(router)
postsQueue, err := moderation.GetDiskQueue()
if err != nil {
return fmt.Errorf("could not start queue: %v", err)
}
defer postsQueue.Close()
go moderation.ProcessQueue(postsQueue)
port := getPort()
slog.Info("Server running", "port", port)
return http.ListenAndServe(":"+port, router)
}
func setupRoutes(router *chi.Mux) {
api.CreateRoutes(router)
}
func setupMiddlewares(router *chi.Mux) {
// recommended stack
router.Use(middleware.RealIP)
router.Use(middleware.RequestID)
router.Use(middleware.Logger)
router.Use(middleware.Recoverer)
router.Use(middleware.Compress(5))
// Skip TLS verification in development
if os.Getenv("PRODUCTION") != "true" {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
}
// rate limiter
router.Use(httprate.Limit(
200,
40*time.Second,
httprate.WithKeyFuncs(httprate.KeyByIP, httprate.KeyByEndpoint),
httprate.WithLimitHandler(func(w http.ResponseWriter, r *http.Request) {
apiutil.ResponseWithError(w, http.StatusTooManyRequests, "Rate limited. Slow down.")
}),
))
}
func setupCORS(router *chi.Mux) {
production := os.Getenv("PRODUCTION") == "true"
corsOptions := cors.Options{
AllowedMethods: []string{"GET", "POST", "DELETE", "PUT", "OPTIONS"},
AllowedHeaders: []string{"Authorization", "Content-Type", "Accept", "Origin"},
ExposedHeaders: []string{"Link"},
AllowCredentials: true,
MaxAge: 300,
}
if production {
corsOptions.AllowedOrigins = []string{
"https://humancorners.com",
"https://www.humancorners.com",
"https://www.corners.club",
"https://corners.club",
}
} else {
// In development, enable the AllowOriginFunc to dynamically check the origin
corsOptions.AllowOriginFunc = func(r *http.Request, origin string) bool {
return true
}
}
router.Use(cors.Handler(corsOptions))
}
func getPort() string {
port := os.Getenv("PORT")
if port == "" {
slog.Warn("Could not get port from env, setting default to 8080")
port = "8080"
}
return port
}