This repository has been archived by the owner on Jun 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
78 lines (68 loc) · 1.93 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
package main
import (
"encoding/json"
"fmt"
"github.com/AndreasBackx/remote-and-chill/model"
"github.com/AndreasBackx/remote-and-chill/resolver"
graphql "github.com/graph-gophers/graphql-go"
"github.com/pusher/pusher-http-go"
"github.com/rs/cors"
"github.com/satori/go.uuid"
"github.com/sirupsen/logrus"
"log"
"net/http"
)
func apiHandler(writer http.ResponseWriter, request *http.Request) {
fmt.Printf("%s %s\n", request.Method, request.RequestURI)
authorization := request.Header.Get("Authorization")
secret, err := uuid.FromString(authorization)
ctx := request.Context()
if err == nil {
ctx, _ = model.Login(secret, ctx, resolver.Me)
}
var params struct {
Query string `json:"query"`
OperationName string `json:"operationName"`
Variables map[string]interface{} `json:"variables"`
}
if err := json.NewDecoder(request.Body).Decode(¶ms); err != nil {
logrus.Error(err)
http.Error(writer, err.Error(), http.StatusBadRequest)
return
}
schema := graphql.MustParseSchema(SchemaString(), &resolver.Resolver{})
// fmt.Printf("%v\n", params.Query)
// fmt.Printf("%v\n", params.Variables)
response := schema.Exec(ctx, params.Query, params.OperationName, params.Variables)
responseJSON, err := json.Marshal(response)
if err != nil {
logrus.Error(err)
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
writer.Header().Set("Content-Type", "application/json")
writer.Write(responseJSON)
}
func main() {
config, err := LoadConfig("config.json")
if err != nil {
log.Fatal(err)
}
client := pusher.Client{
AppId: config.Pusher.AppId,
Key: config.Pusher.Key,
Secret: config.Pusher.Secret,
Host: config.Pusher.Host,
Secure: config.Pusher.Secure,
Cluster: config.Pusher.Cluster,
}
resolver.Setup(client)
log.Fatal(
http.ListenAndServe(
":3000",
cors.AllowAll().Handler(
http.HandlerFunc(apiHandler),
),
),
)
}