This repository has been archived by the owner on Mar 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
185 lines (161 loc) · 5.41 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/gorilla/handlers"
"github.com/graphql-go/graphql"
"github.com/graphql-go/handler"
"nagase/components/auth"
"nagase/models"
)
func main() {
schema, _ := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "RootQuery",
Fields: graphql.Fields{
"me": models.MeQuery,
"members": models.MembersQuery,
"board": models.BoardQuery,
"boards": models.BoardsQuery,
"post": models.PostQuery,
"postPage": models.PostPageQuery,
"project": models.ProjectQuery,
"projects": models.ProjectsQuery,
"vote": models.VoteQuery,
},
}),
Mutation: graphql.NewObject(graphql.ObjectConfig{
Name: "RootMutation",
Fields: graphql.Fields{
// Access tokens
"createAccessToken": models.CreateAccessTokenMutation,
"refreshAccessToken": models.RefreshAccessTokenMutation,
// Boards
"createBoard": models.CreateBoardMutation,
"updateBoard": models.UpdateBoardMutation,
"deleteBoard": models.DeleteBoardMutation,
// Comments
"createComment": models.CreateCommentMutation,
"deleteComment": models.DeleteCommentMutation,
// Members
"createMember": models.CreateMemberMutation,
"updateMember": models.UpdateMemberMutation,
"updateMemberPassword": models.UpdateMemberPasswordMutation,
"deleteMember": models.DeleteMemberMutation,
"toggleMemberIsActivated": models.ToggleMemberIsActivatedMutation,
"toggleMemberIsAdmin": models.ToggleMemberIsAdminMutation,
"requestMemberPasswordReset": models.RequestPasswordResetMutation,
// Posts
"createPost": models.CreatePostMutation,
"deletePost": models.DeletePostMutation,
"updatePost": models.UpdatePostMutation,
// Projects
"createProject": models.CreateProjectMutation,
"updateProject": models.UpdateProjectMutation,
"deleteProject": models.DeleteProjectMutation,
// Votes
"selectVoteOption": models.SelectVoteOptionMutation,
// Push tokens & subscriptions
"registerPushToken": models.RegisterPushTokenMutation,
"deregisterPushToken": models.DeregisterPushTokenMutation,
"subscribeBoard": models.SubscribeBoardMutation,
"unsubscribeBoard": models.UnsubscribeBoardMutation,
"subscribePost": models.SubscribePostMutation,
"unsubscribePost": models.UnsubscribePostMutation,
},
}),
})
// Set GraphQL endpoint.
h := handler.New(&handler.Config{
Schema: &schema,
Playground: true,
})
server := http.NewServeMux()
server.Handle("/graphql", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// CORS configurations.
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, Authorization")
// Set context.
ctx := context.Background()
authorization := r.Header.Get("Authorization")
if authorization != "" {
memberUUID, err := auth.ValidatedToken(strings.Split(authorization, " ")[1])
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
member, err := models.GetMemberByUUID(memberUUID)
if err != nil || !member.IsActivated {
w.WriteHeader(http.StatusUnauthorized)
return
}
ctx = context.WithValue(ctx, "member", member)
}
h.ContextHandler(ctx, w, r)
})))
server.Handle("/files/", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Parse fileName.
paths := strings.Split(r.URL.Path, "/")
if len(paths) != 3 {
w.WriteHeader(http.StatusNotFound)
return
}
fileName := paths[2]
// CORS configurations.
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, Authorization")
if r.Method == "GET" {
buffer, contentType, err := models.GetFile(fileName)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
if contentType != "" {
w.Header().Set("Content-Type", contentType)
}
io.Copy(w, buffer)
} else if r.Method == "POST" {
// Only logged-in member can upload new files.
authorization := r.Header.Get("Authorization")
if authorization == "" {
w.WriteHeader(http.StatusUnauthorized)
return
}
memberUUID, err := auth.ValidatedToken(strings.Split(authorization, " ")[1])
if memberUUID == "" || err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
// Limit file size to 5MB.
r.Body = http.MaxBytesReader(w, r.Body, 5*1024*1024)
// Get bytes from the HTTP request.
var buffer bytes.Buffer
upload, _, err := r.FormFile("upload")
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
io.Copy(&buffer, upload)
// Save file
err = models.SaveFile(&buffer, fileName)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
} else if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
}
})))
fmt.Println("Server listening port 8080...")
http.ListenAndServe(":8080", handlers.CompressHandler(server))
}