-
Notifications
You must be signed in to change notification settings - Fork 2
/
service.go
83 lines (67 loc) · 1.75 KB
/
service.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
package main
import (
"context"
"encoding/json"
"net/http"
"github.com/canonical-debate-lab/argument-analysis-research/pkg/document"
"github.com/canonical-debate-lab/argument-analysis-research/pkg/keyword"
"github.com/canonical-debate-lab/argument-analysis-research/pkg/api"
"github.com/canonical-debate-lab/argument-analysis-research/pkg/service"
"github.com/go-chi/chi"
"github.com/pkg/errors"
"github.com/seibert-media/golibs/log"
"go.uber.org/zap"
)
const (
svcName = "Segment Keyword Extraction API Service"
svcKey = "segment-keyword-extraction-api-service"
)
// Spec for the service
type Spec struct {
service.BaseSpec
}
func main() {
var svc Spec
ctx := service.Init(svcKey, &svc)
defer service.Defer(ctx)
srv := api.New(":8080", svc.Debug)
Routes(ctx, svc, srv)
go srv.GracefulHandler(ctx)
err := srv.Start(ctx)
if err != nil {
log.From(ctx).Fatal("running server", zap.Error(err))
}
log.From(ctx).Info("finished")
}
// Routes for this service
func Routes(ctx context.Context, svc Spec, srv *api.Server) {
srv.Router.Route("/argument/keyword", func(r chi.Router) {
r.Post("/", api.NewHandler(ctx, Handler(ctx, svc)))
})
}
// Handler for this endpoint
func Handler(ctx context.Context, svc Spec) api.HandlerFunc {
type Response struct {
document.Segment
*api.Error
}
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) api.Response {
var (
req document.Segment
resp = &Response{Error: &api.Error{}}
)
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
resp.Fail(errors.Wrap(err, "decoding request"))
return resp
}
ctx = log.WithFields(ctx,
zap.String("input", req.Text),
)
resp.Segment, err = keyword.Extract(req)
if err != nil {
resp.Fail(err)
}
return resp
}
}