This repository has been archived by the owner on Jul 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
89 lines (71 loc) · 2.66 KB
/
server.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
package main
import (
"log"
"net/http"
"time"
"github.com/factly/x/healthx"
"github.com/factly/x/loggerx"
"github.com/factly/x/middlewarex"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/viper"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/factly/dega-api/config"
"github.com/factly/dega-api/graph/generated"
"github.com/factly/dega-api/graph/loaders"
"github.com/factly/dega-api/graph/resolvers"
"github.com/factly/dega-api/graph/validator"
"github.com/factly/dega-api/util"
"github.com/factly/dega-api/util/cache"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
)
func main() {
router := chi.NewRouter()
cors := cors.New(cors.Options{
// AllowedOrigins: []string{"https://foo.com"}, // Use this to allow specific origin hosts
AllowedOrigins: []string{"*"},
// AllowOriginFunc: func(r *http.Request, origin string) bool { return true },
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token", "space"},
ExposedHeaders: []string{"Link"},
AllowCredentials: true,
MaxAge: 300, // Maximum value not ignored by any of major browsers
})
router.Use(cors.Handler)
router.Use(middleware.RequestID)
router.Use(loggerx.Init())
router.Use(middleware.RealIP)
config.SetupVars()
config.SetupDB()
sqlDB, _ := config.DB.DB()
healthx.RegisterRoutes(router, healthx.ReadyCheckers{
"database": sqlDB.Ping,
"kavach": util.KavachChecker,
})
go func() {
promRouter := chi.NewRouter()
promRouter.Mount("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":8001", promRouter))
}()
if cache.IsEnabled() {
cacheExpiration := 30
if viper.IsSet("redis_cache_expiration") {
cacheExpiration = viper.GetInt("redis_cache_expiration")
}
err := cache.SetupCache(viper.GetString("redis_url"), viper.GetString("redis_password"), time.Duration(cacheExpiration)*time.Second, 0)
if err != nil {
log.Fatal(err)
}
}
srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &resolvers.Resolver{}}))
r := router.With(validator.CheckSpace(), validator.CheckOrganisation(), middlewarex.ValidateAPIToken("dega", validator.GetOrganisation))
if cache.IsEnabled() {
r = r.With(cache.CachingMiddleware(), cache.RespMiddleware)
}
r.Handle("/query", loaders.DataloaderMiddleware(srv))
router.Handle("/", playground.Handler("GraphQL playground", "/query"))
log.Printf("connect to http://localhost:%s/ for GraphQL playground", "8000")
log.Fatal(http.ListenAndServe(":8000", router))
}