-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.go
228 lines (211 loc) · 5.86 KB
/
http.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package parsefield
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
"strings"
"sync"
"github.com/MonaxGT/parsefields/storage"
"github.com/julienschmidt/httprouter"
)
const (
defaultDBName = "parse"
defaultTableField = "fields"
defaultTableEvent = "events"
)
// Config is main struct with common connectors
type Config struct {
Fields *sync.Map
Events *sync.Map
separator string
DB storage.Database
}
// JSONHandler collector single JSON request
func (c *Config) JSONHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "can't read the request body", http.StatusBadRequest)
return
}
if len(body) == 0 {
http.Error(w, "no data provided", http.StatusBadRequest)
return
}
err = c.parse(body)
if err != nil {
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
}
// MJSONHandler collector multiply JSON request
func (c *Config) MJSONHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "can't read the request body", http.StatusBadRequest)
return
}
if len(body) == 0 {
http.Error(w, "no data provided", http.StatusBadRequest)
return
}
err = c.parseMulti(body)
if err != nil {
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
}
// FieldsHandler return all unique fields
func (c *Config) FieldsHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var fields []string
c.Fields.Range(func(key, value interface{}) bool {
fields = append(fields, fmt.Sprintf("%v", key))
return true
})
fmt.Fprintln(w, fmt.Sprintf(strings.Join(fields, "\n")))
}
func (c *Config) eventsHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var events []string
c.Events.Range(func(key, value interface{}) bool {
events = append(events, fmt.Sprintf("%v", key))
return true
})
fmt.Fprintln(w, fmt.Sprintf(strings.Join(events, "\n")))
}
// RestoreDBF restore database fields from DB
func (c *Config) RestoreDBF() error {
t, err := c.DB.RestoreFields()
if err != nil {
return err
}
for k := range t {
c.Fields.Store(t[k].Field, 1)
}
return nil
}
// RestoreDBE restore database events from DB
func (c *Config) RestoreDBE() error {
t, err := c.DB.RestoreEvents()
if err != nil {
return err
}
for k := range t {
str := fmt.Sprintf("%s - %d", t[k].LogName, t[k].EventID)
c.Events.Store(str, 1)
}
return nil
}
// Init - transformer function
func Init(separator string, dbType string, dbURL string) (*Config, error) {
var fields sync.Map
var events sync.Map
var db storage.Database
switch dbType {
case "reindexer":
reindexer := &storage.Reindexer{
DBName: defaultDBName,
NamespaceField: defaultTableField,
NamespaceEvent: defaultTableEvent,
}
url := fmt.Sprintf("%s%s", dbURL, reindexer.DBName)
if err := reindexer.Open(url); err != nil {
return nil, err
}
db = reindexer
default:
log.Println("You didn't choose DB, the API works in short mode")
}
return &Config{
Fields: &fields,
Events: &events,
separator: separator,
DB: db,
}, nil
}
func (c *Config) eventDropHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if ps.ByName("logname") == "" && ps.ByName("eventid") == "" {
http.Error(w, "No data provided", http.StatusBadRequest)
return
}
eventID, err := strconv.ParseUint(ps.ByName("eventid"), 10, 64)
if err != nil {
http.Error(w, "Can't decode eventid number, please use numbers", http.StatusBadRequest)
return
}
str := fmt.Sprintf("%s - %d", ps.ByName("logname"), int32(eventID))
c.Events.Delete(str)
log.Printf("Deleted event: %s \n", str)
if c.DB != nil {
err = c.DB.DeleteEvents(ps.ByName("logname"), int32(eventID))
if err != nil {
http.Error(w, "Can't delete record", http.StatusInternalServerError)
return
}
}
fmt.Fprintln(w, "Delete")
}
func (c *Config) fieldDropHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if ps.ByName("field") == "" {
http.Error(w, "No data provided", http.StatusBadRequest)
return
}
c.Fields.Delete(ps.ByName("field"))
log.Printf("Deleted field: %s \n", ps.ByName("field"))
if c.DB != nil {
err := c.DB.DeleteFields(ps.ByName("field"))
if err != nil {
http.Error(w, "Can't delete record", http.StatusInternalServerError)
return
}
}
fmt.Fprintln(w, "Delete")
}
func (c *Config) eventsBodyHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if ps.ByName("logname") == "" && ps.ByName("eventid") == "" {
http.Error(w, "No data provided", http.StatusBadRequest)
return
}
eventID, err := strconv.ParseUint(ps.ByName("eventid"), 10, 64)
if err != nil {
http.Error(w, "Can't decode eventid number, please use numbers", http.StatusBadRequest)
return
}
if c.DB != nil {
body, err := c.DB.GetByEvent(ps.ByName("logname"), int32(eventID))
if err != nil {
http.Error(w, "Can't find record", http.StatusInternalServerError)
return
}
if body == nil {
fmt.Fprintln(w, "Not Found")
return
}
fmt.Fprintln(w, string(body))
return
}
fmt.Fprintln(w, "Database isn't initiate. Please use database for this option")
}
// Serve is started API service
func (c *Config) Serve(addr string) error {
if c.DB != nil {
err := c.RestoreDBF()
if err != nil {
return err
}
err = c.RestoreDBE()
if err != nil {
return err
}
}
router := httprouter.New()
router.POST("/v1/json/", c.JSONHandler)
router.POST("/v1/mjson/", c.MJSONHandler)
router.GET("/v1/fields/", c.FieldsHandler)
router.GET("/v1/events/", c.eventsHandler)
router.GET("/v1/events/:logname/:eventid", c.eventsBodyHandler)
router.DELETE("/v1/events/:logname/:eventid", c.eventDropHandler)
router.DELETE("/v1/fields/:field", c.fieldDropHandler)
log.Printf("Listening on %s", addr)
return http.ListenAndServe(addr, router)
}