-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
82 lines (66 loc) · 2 KB
/
server.go
File metadata and controls
82 lines (66 loc) · 2 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
package main
import (
"log"
"net/http"
"os"
// "github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/geraldhoxha/resume-backend/config"
"github.com/geraldhoxha/resume-backend/directives"
"github.com/geraldhoxha/resume-backend/graph"
"github.com/geraldhoxha/resume-backend/middlewares"
"github.com/geraldhoxha/resume-backend/migration"
"github.com/geraldhoxha/resume-backend/service"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
var ALLOWED_ORIGINS = []string{
"http://localhost:3000",
"http://localhost:8080",
"http://localhosr:3000/refresh",
"http://localhost:8080/refresh",
"http://192.168.1.167:3000",
}
const defaultPort = "8080"
func main() {
// AutoMigration
// AutoMigration
migration.MigrateTable()
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
// defer database
db := config.GetDB()
sqlDB, _ := db.DB()
defer func() {
err := sqlDB.Close()
if err != nil {
panic(err)
}
}()
// Create a new router
router := mux.NewRouter()
// Apply Auth Middleware to all routes
router.Use(middlewares.AuthMiddleware)
// GraphQL server setup
c := graph.Config{Resolvers: &graph.Resolver{}}
c.Directives.Auth = directives.Auth
srv := handler.NewDefaultServer(graph.NewExecutableSchema(c))
// Define routes
router.Handle("/", playground.Handler("GraphQL playground", "/query")).Methods("GET", "POST", "DELETE")
router.Handle("/query", srv).Methods("POST")
router.HandleFunc("/refresh", service.RefreshToken).Methods("POST")
// CORS configuration
CORS := handlers.CORS(
handlers.AllowedOrigins(ALLOWED_ORIGINS),
handlers.AllowedMethods([]string{"GET", "POST", "DELETE", "OPTIONS"}),
handlers.AllowedHeaders([]string{"Content-Type", "Authorization"}),
handlers.AllowCredentials(),
handlers.MaxAge(3600),
)
log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
// Start the server
log.Fatal(http.ListenAndServe(":"+port, CORS(router)))
}