-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
125 lines (101 loc) · 3.36 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
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
package main
import (
"context"
"log"
"net/http"
"os"
"strings"
"groupinary/ent"
"groupinary/ent/migrate"
"groupinary/graph/resolvers"
"groupinary/middleware"
"entgo.io/ent/dialect"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
_ "github.com/lib/pq" // Import the PostgreSQL driver package
)
const (
defaultPort = "8080"
defaultCertFile = "cert.pem" // Path to your SSL/TLS certificate file
defaultKeyFile = "key.pem" // Path to your SSL/TLS key file
)
func main() {
environment := os.Getenv("ENVIRONMENT")
log.Printf("Starting Server in: %s ENVIRONMENT", environment)
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
// Create ent.Client and run the schema migration.
client, err := ent.Open(dialect.Postgres, os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatal("opening ent client", err)
}
defer client.Close()
// Seed the data
ctx := context.Background()
if err := client.Schema.Create(
ctx,
migrate.WithGlobalUniqueID(true),
migrate.WithDropIndex(true),
migrate.WithDropColumn(true),
); err != nil {
log.Fatal("opening ent client", err)
}
srv := handler.NewDefaultServer(resolvers.NewSchema(client))
// Middleware for logging requests
loggingMiddleware := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
showLogs := false
if showLogs {
// Log the incoming request
log.Printf("Received request: %s %s", r.Method, r.URL.Path)
// Log the headers for preflight request or actual request
if r.Method == http.MethodOptions {
log.Println("Preflight Request Headers:")
} else {
log.Println("Actual Request Headers:")
}
// Print each header key-value pair
for name, values := range r.Header {
// Join multiple values for the same header with commas
value := strings.Join(values, ", ")
log.Printf("%s: %s", name, value)
}
log.Printf("==========================================================================================")
}
// Call the next handler
next.ServeHTTP(w, r)
})
}
corsHandler := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Set the Access-Control-Allow-Origin header
// w.Header().Set("Access-Control-Allow-Origin", "https://studio.apollographql.com")
// Call the next handler
next.ServeHTTP(w, r)
})
}
issueURL := os.Getenv("ISSUERURL")
audienceAPI := os.Getenv("AUDIENCE_API")
audienceHash := os.Getenv("AUDIENCE_HASH")
secretKey := os.Getenv("SECRET_KEY")
jwtENV := middleware.EnvJWTStruct{
IssuerURL: issueURL,
Audience: []string{audienceAPI, audienceHash},
SecretKey: secretKey,
}
log.Printf("SECRET_KEY: %s", secretKey)
userTokenOperations := middleware.NewUserTokenOperator(client)
authMiddleware := middleware.EnsureValidToken(userTokenOperations, jwtENV)
http.Handle("/", playground.Handler("GraphQL playground", "/query"))
http.Handle("/viz", ent.ServeEntviz())
http.Handle("/query", loggingMiddleware(corsHandler(authMiddleware(srv))))
log.Printf("connect to https://localhost:%s/ for GraphQL playground", port)
if environment == "dev" {
log.Printf("started in TLS mode")
log.Fatal(http.ListenAndServeTLS(":"+port, defaultCertFile, defaultKeyFile, nil))
} else {
log.Fatal(http.ListenAndServe(":"+port, nil))
}
}