-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
176 lines (144 loc) · 4.1 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
// Copyright (c) 2020 Jorge Luis Betancourt. All rights reserved.
// Use of this source code is governed by the Apache License, Version 2.0
// that can be found in the LICENSE file.
package main
import (
"encoding/json"
"flag"
"fmt"
"html/template"
"net/http"
"strings"
"time"
"github.com/elastic/beats/v7/libbeat/processors/dissect"
"github.com/elastic/beats/v7/libbeat/version"
"go.uber.org/automaxprocs/maxprocs"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
// several timeout options for the HTTP server
readTimeout = 5 * time.Second
writeTimeout = 5 * time.Second
procTimeout = 3 * time.Second
)
// A list of HTTP endpoints to register
const (
staticPath = "/static/"
apiPath = "/api/"
)
var versionInfo = struct {
Version string
}{
Version: version.GetDefaultVersion(),
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("templates/index.html"))
if err := tmpl.Execute(w, versionInfo); err != nil {
zap.L().Error("Could not parse template.",
zap.String("template", "templates/index.html"),
zap.Error(err),
)
}
}
func apiHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
http.Error(w, fmt.Sprintf("Couldn't parse POST request: %s", err.Error()),
http.StatusBadRequest)
return
}
str := r.Form.Get("str")
if len(str) == 0 {
http.Error(w, "str parameter not found", http.StatusBadRequest)
return
}
tokenizer := r.Form.Get("tokenizer")
if len(tokenizer) == 0 {
http.Error(w, "tokenizer parameter not found", http.StatusBadRequest)
return
}
zap.L().Sugar().Infow("Received request",
"str", str,
"tokenizer", tokenizer,
)
samples := strings.Split(str, "\n")
tokenized := make([]map[string]interface{}, 0)
for i, s := range samples {
processor, err := dissect.New(tokenizer)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
m, err := processor.DissectConvert(s)
if err != nil {
http.Error(w, fmt.Sprintf("sample: %d, error: %s", i, err), http.StatusBadRequest)
return
}
tokenized = append(tokenized, m)
}
payload, err := json.Marshal(tokenized)
if err != nil {
http.Error(w, "couldn't encode response", http.StatusNoContent)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(payload) // nolint: errcheck
}
// RegisterAppHandlers registers the app handlers with the given mux
func RegisterAppHandlers(mux *http.ServeMux) {
mux.Handle(staticPath,
http.StripPrefix(staticPath, http.FileServer(http.Dir("static"))),
)
mux.HandleFunc("/", indexHandler)
mux.HandleFunc(apiPath, apiHandler)
}
func main() {
config := zap.NewProductionConfig()
config.EncoderConfig.TimeKey = "timestamp"
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
logger, err := config.Build()
if err != nil {
panic("Couldn't configure the logger. Aborting!")
}
zap.ReplaceGlobals(logger)
defer logger.Sync() // nolint: errcheck
logger.Info("elastic/beats engine", zap.String("version", versionInfo.Version))
listenAddr := flag.String("listen", ":8080", "address to listen on")
flag.Parse()
_, err = maxprocs.Set(maxprocs.Logger(
func(logMessage string, args ...interface{}) {
logger.Sugar().Info(fmt.Sprintf(logMessage, args...))
},
))
if err != nil {
logger.Error("Failed to set maxprocs: %v", zap.Error(err))
}
mux := http.NewServeMux()
pprofMux := http.NewServeMux()
RegisterAppHandlers(mux)
RegisterDebugHandler(pprofMux)
go func() {
pprofServer := http.Server{
Addr: "localhost:6060",
Handler: pprofMux,
}
logger.Info("Starting debug server")
logger.Error("Error starting pprof server", zap.Error(pprofServer.ListenAndServe()))
}()
server := http.Server{
Handler: http.TimeoutHandler(mux, procTimeout, "Processing your request took too long."),
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
}
defer server.Close()
server.Addr = *listenAddr
logger.Sugar().Infow("Server is running",
"address", *listenAddr,
)
if err := server.ListenAndServe(); err != http.ErrServerClosed {
logger.Error("Could not start HTTP server.",
zap.Error(err),
)
}
}