forked from cockroachlabs/wikifeedia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
112 lines (102 loc) · 3.04 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
package server
import (
"context"
"fmt"
"log"
"net/http"
"time"
"github.com/NYTimes/gziphandler"
"github.com/cockroachlabs/wikifeedia/db"
"github.com/cockroachlabs/wikifeedia/wikipedia"
"github.com/samsarahq/thunder/graphql"
"github.com/samsarahq/thunder/graphql/graphiql"
"github.com/samsarahq/thunder/graphql/introspection"
"github.com/samsarahq/thunder/graphql/schemabuilder"
)
//go:generate go run github.com/shurcooL/vfsgen/cmd/vfsgendev -source="github.com/cockroachlabs/wikifeedia/server".Assets
// Server is an http.Handler for a graphql server for this application.
type Server struct {
db *db.DB
mux http.ServeMux
}
// New creates a new Server.
func New(conn *db.DB) *Server {
s := &Server{
db: conn,
}
schema := s.schema()
introspection.AddIntrospectionToSchema(schema)
fs := http.FileServer(Assets)
s.mux.Handle("/graphqlhttp",
gziphandler.GzipHandler(graphql.HTTPHandler(schema)))
s.mux.Handle("/graphql", graphql.Handler(schema))
s.mux.Handle("/graphiql/", http.StripPrefix("/graphiql/", graphiql.Handler()))
staticHandler := gziphandler.GzipHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Vary", "Accept-Encoding")
w.Header().Set("Cache-Control", "public, max-age=3600")
fs.ServeHTTP(w, r)
}))
s.mux.Handle("/", staticHandler)
s.mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=UTF-8")
if _, err := w.Write([]byte("OK")); err != nil {
log.Printf("could not write response: %v", err)
}
})
return s
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.mux.ServeHTTP(w, r)
}
type ArticlesResponse struct {
Articles []db.Article `json:"articles"`
AsOf string `json:"as_of"`
}
func (s *Server) getArticles(
ctx context.Context,
args struct {
Project string
Offset int32
Limit int32
FollowerRead *bool
AsOf *string
},
) (*ArticlesResponse, error) {
fmt.Println("h")
start := time.Now()
var asOf string
if args.AsOf != nil {
asOf = *args.AsOf
}
defer func() {
log.Printf("%v?limit=%v&offset=%v&follower_read=%v&as_of=%v - %v",
args.Project, args.Limit, args.Offset, *args.FollowerRead,
time.Since(start), asOf)
}()
if !wikipedia.IsProject(args.Project) {
return nil, fmt.Errorf("%s is not a valid project")
}
articles, newAsOf, err := s.db.GetArticles(ctx, args.Project, int(args.Offset), int(args.Limit),
args.FollowerRead != nil && *args.FollowerRead, asOf)
if err != nil {
return nil, err
}
return &ArticlesResponse{
AsOf: newAsOf,
Articles: articles,
}, nil
}
// schema builds the graphql schema.
func (s *Server) schema() *graphql.Schema {
builder := schemabuilder.NewSchema()
obj := builder.Object("Article", db.Article{})
obj.Key("article")
builder.Object("ArticlesResponse", ArticlesResponse{})
q := builder.Query()
q.FieldFunc("articles", s.getArticles)
mut := builder.Mutation()
mut.FieldFunc("echo", func(args struct{ Message string }) string {
return args.Message
})
return builder.MustBuild()
}