-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (54 loc) · 1.59 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
package main
import (
"./helpers"
"encoding/json"
"log"
"net/http"
"regexp"
"strconv"
)
func main() {
go startHandlers()
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
route(writer, request)
})
log.Fatal(http.ListenAndServe(":9107", nil))
}
func route(writer http.ResponseWriter, request *http.Request) {
err := request.ParseForm()
Helper.ErrorHandler.Handle(err, func(err error) {}, func(err error) {})
if string(request.URL.Path[len(request.URL.Path)-1:]) != "/" {
http.Redirect(writer, request, request.URL.Path+"/", 301)
} else {
var routeName string
for key := range Helper.Routing {
match, _ := regexp.MatchString(key+"$", request.URL.Path)
if match {
routeName = key
break
}
}
if routeName != "" {
var result map[string]interface{}
var res []byte
if Helper.Routing[routeName].AfterPublish != nil {
// Выполнение через NATS (изменение и удаление данных)
Helper.NatsHandler.Publish(Helper.Routing[routeName])
result = Helper.Routing[routeName].AfterPublish(Helper.Routing[routeName].Request)
} else {
// Выполнение напрямую
result = Helper.Routing[routeName].Handler(Helper.Routing[routeName].Request)
}
res, err = json.Marshal(result)
Helper.ErrorHandler.Handle(err, func(err error) {}, func(err error) {})
writer.Header().Set("Content-Type", "application/json")
writer.Write(res)
}
}
}
func startHandlers() {
for i, handler := range Helper.Config {
handler.ClientID = "client" + strconv.Itoa(i)
handler.Subscribe()
}
}