-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
156 lines (132 loc) · 4.65 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
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/go-playground/validator/v10"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/graphql-go/handler"
"github.com/joho/godotenv"
"github.com/soumitradev/Dwitter/backend/auth"
"github.com/soumitradev/Dwitter/backend/cache"
"github.com/soumitradev/Dwitter/backend/cdn"
"github.com/soumitradev/Dwitter/backend/common"
"github.com/soumitradev/Dwitter/backend/database"
"github.com/soumitradev/Dwitter/backend/gql"
"github.com/soumitradev/Dwitter/backend/middleware"
"github.com/soumitradev/Dwitter/frontend"
"github.com/unrolled/secure"
)
func main() {
// When returning from main(), make sure to disconnect from database
defer database.DisconnectDB()
// Load .env
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file: ", err)
}
// Initialize sendgrid
common.InitSendgrid()
// Initialize redis dbs
auth.InitAuth()
cache.InitCache()
// Check for an error in schema at runtime
if gql.SchemaError != nil {
panic(gql.SchemaError)
}
// Set flag for timeout to close all connections before quitting
var wait time.Duration
flag.DurationVar(&wait, "graceful-timeout", time.Second*15, "the duration for which the server gracefully wait for existing connections to finish - e.g. 15s or 1m")
flag.Parse()
// Create a new router
router := mux.NewRouter().StrictSlash(true)
// Create a validator for data validation
common.Validate = validator.New()
// Create a graphql query handler
h := handler.New(&handler.Config{
Schema: &gql.Schema,
Pretty: true,
GraphiQL: false,
Playground: true,
// This is a way to pass context about the request into the resolver function of graphql
RootObjectFn: func(myCtx context.Context, r *http.Request) map[string]interface{} {
// Pass down the authorization token to the graphql query
cookie, _ := r.Cookie("session")
var sid string
if cookie != nil {
cookieString := cookie.Value
session := auth.ParseCookie(cookieString)
sid = session.Sid
} else {
sid = ""
}
return map[string]interface{}{
"sid": sid,
}
},
})
// Map /graphql to the graphql handler, and attach a middleware to it
router.Handle("/api/graphql", h)
// Handle some API endpoints using a non-GraphQL solution
router.HandleFunc("/api/login", auth.LoginHandler).Methods("POST")
router.HandleFunc("/api/verify/{token}", auth.VerifyHandler).Methods("GET")
router.HandleFunc("/api/media_upload", cdn.UploadMediaHandler).Methods("POST")
router.HandleFunc("/api/pfp_upload", cdn.UploadPFPHandler).Methods("POST")
router.HandleFunc("/api/callback", auth.OAuth2callbackHandler)
router.Handle("/api/subscriptions", common.GraphqlwsHandler)
// Handle frontend
frontend := frontend.FrontendHandler{StaticPath: "frontend/dist", IndexPath: "index.html"}
router.PathPrefix("/").Handler(frontend)
// Initialize middleware and use it
secureMiddleware := secure.New(secure.Options{
FrameDeny: true,
})
router.Use(handlers.CompressHandler)
// router.Use(middleware.LoggingHandler)
router.Use(middleware.ContentTypeHandler)
router.Use(middleware.RecoveryHandler)
router.Use(middleware.SizeAndTimeHandler)
router.Use(secureMiddleware.Handler)
// CORS Handler. TODO: Make sure to turn on/off in production!
router.Use(middleware.CORSTestingHandler)
// Create an HTTP server
srv := &http.Server{
Handler: router,
Addr: "127.0.0.1:5000",
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
IdleTimeout: 60 * time.Second,
}
fmt.Println("Server now running on port 5000, access /graphql")
// Run our server in a goroutine so that it doesn't block.
go func() {
if err := srv.ListenAndServe(); err != nil {
log.Println()
log.Println(err)
}
}()
c := make(chan os.Signal, 1)
// We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C)
// SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught.
signal.Notify(c, os.Interrupt)
// Block until we receive our signal.
<-c
// Create a deadline to wait for.
BaseCtx, cancel := context.WithTimeout(common.BaseCtx, wait)
defer cancel()
// Doesn't block if no connections, but will otherwise wait
// until the timeout deadline.
srv.Shutdown(BaseCtx)
// Optionally, you could run srv.Shutdown in a goroutine and block on
// <-main.BaseCtx.Done() if your application should wait for other services
// to finalize based on context cancellation.
log.Println("Shutting down")
os.Exit(0)
}