-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
171 lines (157 loc) · 4.22 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strings"
firebase "firebase.google.com/go"
"firebase.google.com/go/auth"
graphql "github.com/graph-gophers/graphql-go"
"google.golang.org/api/option"
_ "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/postgres"
_ "github.com/lib/pq"
)
const UserIDKey = UserID("userID")
var (
// Postgres database
db *sql.DB
// Firebase app and authentication client
app *firebase.App
client *auth.Client
// GraphQL executable schema
schema *graphql.Schema
)
type UserID string
// Represents a GraphQL query or mutation.
type Query struct {
// FIXME: OperationName is not appearing
Query string `json:"query"`
OperationName string `json:"operationName"`
Variables map[string]interface{} `json:"variables"`
}
// var query Query
// err := json.NewDecoder(r.Body).Decode(&query)
// if err != nil {
// log.Print(fmt.Errorf("error due to json.NewDecoder.Decode: %w", err))
// RespondServerError(w)
// return
// }
// client, err := app.Auth(context.TODO())
// if err != nil {
// log.Print(fmt.Errorf("error due to app.Auth: %w", err))
// RespondServerError(w)
// return
// }
var whiteSpaceRe = regexp.MustCompile(`( |\t|\n)+`)
func handler(w http.ResponseWriter, r *http.Request) {
// Set headers:
setHeaders(w)
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
// Get current user ID (authenticated):
var userID string
authHeader := r.Header.Get("Authorization")
if authHeader == "" {
// No-op; defer to query or mutation
} else {
idToken := strings.TrimPrefix(authHeader, "Bearer ")
token, err := client.VerifyIDToken(context.TODO(), idToken)
if err != nil {
log.Print(fmt.Errorf("error due to client.VerifyUserID: %w", err))
RespondUnauthorized(w)
return
}
userID = token.UID
}
// Read request body:
dataIn, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Print(fmt.Errorf("error due to ioutil.ReadAll: %w", err))
RespondServerError(w)
return
}
// Decode query:
var query Query
err = json.Unmarshal(dataIn, &query)
if err != nil {
log.Print(fmt.Errorf("error due to json.Unmarshal: %w", err))
RespondServerError(w)
return
}
// Execute query:
debugQuery := strings.TrimSpace(whiteSpaceRe.ReplaceAllString(query.Query, " "))
log.Printf("query=%s variables=%+v", debugQuery, query.Variables)
ctx := context.WithValue(context.TODO(), UserIDKey, userID)
resp := schema.Exec(ctx, query.Query, query.OperationName, query.Variables)
if resp.Errors != nil {
log.Printf("error due to schema.Exec: %+v", resp.Errors)
}
// Encode response:
dataOut, err := json.Marshal(resp)
if err != nil {
log.Print(fmt.Errorf("error due to json.MarshalIndent: %w", err))
RespondServerError(w)
return
}
// Done:
fmt.Fprintln(w, string(dataOut))
}
func main() {
var err error
/*
* Postgres
*/
log.Print("setting up postgres")
db, err = sql.Open("cloudsqlpostgres", fmt.Sprintf(`
host=codex-ef322:us-west1:codex-db
user=postgres
password=%s
dbname=codex
sslmode=disable
`, os.Getenv("PSQL_PW")))
must(err, "crash due to sql.Open")
var testStr string
err = db.QueryRow(`select 'hello, world!'`).Scan(&testStr)
must(err, "crash due to db.QueryRow")
defer db.Close()
/*
* Firebase auth
*/
log.Print("setting up firebase auth")
opt := option.WithCredentialsFile("secret/firebase-admin-sdk.json")
app, err = firebase.NewApp(context.TODO(), nil, opt)
must(err, "crash due to firebase.NewApp")
client, err = app.Auth(context.TODO())
must(err, "crash due to app.Auth")
/*
* Schema
*/
log.Print("setting up schema")
bstr, err := ioutil.ReadFile("schema.graphql")
must(err, "crash due to ioutil.ReadFile")
schema, err = graphql.ParseSchema(string(bstr), &RootResolver{})
must(err, "crash due to graphql.ParseSchema")
/*
* Web server
*/
log.Print("setting up web server")
log.Print("ready")
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
http.HandleFunc("/graphql", handler)
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
err = http.ListenAndServe(fmt.Sprintf(":%s", port), nil)
must(err, "crash due to http.ListenAndServe")
}