-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
217 lines (190 loc) · 5.98 KB
/
server.go
File metadata and controls
217 lines (190 loc) · 5.98 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
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
package main
import (
"context"
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/99designs/gqlgen/graphql/playground"
authDlv "github.com/ariefsn/gembok/apps/auth/delivery"
authSvc "github.com/ariefsn/gembok/apps/auth/service"
rootDlv "github.com/ariefsn/gembok/apps/root/delivery"
userRepo "github.com/ariefsn/gembok/apps/user/repository"
"github.com/ariefsn/gembok/constant"
. "github.com/ariefsn/gembok/docs"
"github.com/ariefsn/gembok/env"
"github.com/ariefsn/gembok/graph"
"github.com/ariefsn/gembok/graph/resolvers"
"github.com/ariefsn/gembok/helper"
"github.com/ariefsn/gembok/logger"
"github.com/ariefsn/gembok/middlewares"
"github.com/ariefsn/gembok/models"
"github.com/ariefsn/gembok/notification"
"github.com/ariefsn/gembok/validator"
"github.com/chi-middleware/proxy"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/gorilla/websocket"
httpSwagger "github.com/swaggo/http-swagger"
)
func init() {
env.InitEnv()
logger.InitLogger()
validator.InitValidator()
}
// @title Auth Service
// @version 3.0
// @description API Auth Service.
// @contact.name API Support
// @contact.url https://ariefsn.dev
// @contact.email hello@ariefsn.dev
// @BasePath /
// @securityDefinitions.apikey Bearer
// @in header
// @name Authorization
// @description Description for what is this security definition being used
func main() {
env := env.GetEnv()
notif := notification.NewNotification()
r := chi.NewRouter()
r.Use(middleware.Logger)
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
w.Header().Set("content-type", "application/json")
helper.ResponseJson(w, models.ResponseModel{
Success: false,
Code: constant.ResponseStatusFail.String(),
Message: "route not found",
})
})
r.MethodNotAllowed(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(405)
w.Header().Set("content-type", "application/json")
helper.ResponseJson(w, models.ResponseModel{
Success: false,
Code: constant.ResponseStatusFail.String(),
Message: "method not allowed",
})
})
corsOpt := cors.Options{
AllowedOrigins: []string{
"https://*",
"http://*",
"http://localhost:5173",
"http://localhost:3000",
env.Urls.Studio,
},
AllowOriginFunc: func(r *http.Request, origin string) bool {
return true
},
AllowedHeaders: []string{
"Authorization",
"Content-Type",
"Accept",
"Origin",
"User-Agent",
"Referrer",
"Host",
"X-Requested-With",
"X-CSRF-Token",
string(constant.HeaderAuthorization),
},
AllowCredentials: true,
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
ExposedHeaders: []string{"Content-Length", "Link"},
Debug: false,
}
corsCfg := cors.New(corsOpt)
// Setup db
dbEnv := env.Mongo
dbAddress := fmt.Sprintf("mongodb://%s:%s@%s:%s", dbEnv.User, dbEnv.Password, dbEnv.Host, dbEnv.Port)
client, _ := helper.MongoClient(dbAddress)
db := client.Database(dbEnv.Db)
rdb := helper.RedisClient(env.Redis)
// Migration
addressForMigration := dbAddress + "/" + dbEnv.Db
// Uncomment this to force, clean dirty and remigrate
// helper.MongoMigrateForce(addressForMigration, 1)
err := helper.MongoMigrateUp(addressForMigration)
if err != nil {
logger.Error(fmt.Errorf("migrating failed. Error: %s", err.Error()))
}
r.Use(proxy.ForwardedHeaders())
r.Use(middlewares.Inject(*env))
r.Use(corsCfg.Handler)
// Repository
userRepository := userRepo.NewRepository(db, rdb)
// Service
authService := authSvc.NewService(userRepository, notif)
// GraphQL Resolvers Config
gqlConfig := graph.Config{
Resolvers: &resolvers.Resolver{
AuthService: authService,
},
}
srv := handler.NewDefaultServer(graph.NewExecutableSchema(gqlConfig))
srv.AroundOperations(func(ctx context.Context, next graphql.OperationHandler) graphql.ResponseHandler {
oc := graphql.GetOperationContext(ctx)
logger.Info(fmt.Sprintf("[%s]", strings.ToUpper(string(oc.Operation.Operation))), models.M{
"operationName": oc.OperationName,
"variables": oc.Variables,
})
return next(ctx)
})
srv.AddTransport(transport.Options{})
srv.AddTransport(transport.GET{})
srv.AddTransport(transport.POST{})
srv.AddTransport(transport.MultipartForm{
MaxUploadSize: 10 * 1024 * 1024,
})
srv.AddTransport(transport.Websocket{
KeepAlivePingInterval: 10 * time.Second,
Upgrader: websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
},
InitFunc: func(ctx context.Context, initPayload transport.InitPayload) (context.Context, *transport.InitPayload, error) {
fmt.Println(initPayload)
return ctx, &initPayload, nil
// return webSocketInit(ctx, initPayload)
},
})
srv.Use(extension.Introspection{})
r.Handle("/graphui", playground.Handler("GraphQL playground", "/graphql"))
r.Handle("/graphql", srv)
// Handlers
r.Mount("/", rootDlv.NewHandlers())
r.Route("/api/v1", func(r chi.Router) {
// Register Handlers Here
r.Mount("/auth", authDlv.NewHandler(authService))
})
// Swagger Docs
origin := fmt.Sprintf("http://%s:%s", env.App.Host, env.App.Port)
if env.Urls.Origin != "" {
origin = env.Urls.Origin
}
originSplit := strings.Split(origin, "//")
SwaggerInfo.Host = originSplit[1] + "/api/v1"
r.Get("/docs/*", httpSwagger.Handler(
httpSwagger.URL(env.Urls.Origin+"/docs/doc.json"),
))
walkFunc := func(method string, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error {
route = strings.Replace(route, "/*/", "/", -1)
fmt.Printf("%s %s\n", method, route)
return nil
}
if err := chi.Walk(r, walkFunc); err != nil {
fmt.Printf("Logging err: %s\n", err.Error())
}
host := env.App.Host
port := env.App.Port
log.Printf("connect to http://%s:%s/ for GraphQL playground", host, port)
log.Fatal(http.ListenAndServe(":"+port, r))
}