-
Notifications
You must be signed in to change notification settings - Fork 3
/
httpgo.go
156 lines (129 loc) · 3.81 KB
/
httpgo.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
// Copyright 2017 Author: Ruslan Bikchentaev. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package httpGo
import (
"strings"
"net"
"os"
"os/signal"
"regexp"
"syscall"
"time"
"github.com/pkg/errors"
"github.com/valyala/fasthttp"
. "github.com/ruslanBik4/httpgo/apis"
"github.com/ruslanBik4/httpgo/logs"
)
// HttpGo implement rest api http/https server for operation with storage
type HttpGo struct {
mainServer *fasthttp.Server
listener net.Listener
broadcast chan string
apis *Apis
cfg *CfgHttp
}
var regIp = regexp.MustCompile(`for=s*(\d+\.?)+,`)
// NewHttpgo get configuration option from cfg
// listener to receive requests
func NewHttpgo(cfg *CfgHttp, listener net.Listener, apis *Apis) *HttpGo {
if apis.Ctx == nil {
apis.Ctx = make(map[string]interface{}, 0)
}
apis.Ctx["ACC_VERSION"] = httpgoVersion
cfg.Server.Handler = apis.Handler
cfg.Server.ErrorHandler = func(ctx *fasthttp.RequestCtx, err error) {
logs.ErrorLog(err, ctx.String())
}
cfg.Server.Logger = &fastHTTPLogger{}
logs.DebugLog("Server get files under %db size", cfg.Server.MaxRequestBodySize)
if cfg.Access.ChkConn {
listener = &blockListener{
listener,
cfg.Access.Allow,
cfg.Access.Deny,
}
} else if len(cfg.Access.Allow) > 0 || len(cfg.Access.Deny) > 0 {
cfg.Server.Handler = func (ctx *fasthttp.RequestCtx) {
ipClient := ctx.Request.Header.Peek("X-Forwarded-For")
addr := string(ipClient)
if len(ipClient) == 0 {
ipClient = ctx.Request.Header.Peek("Forwarded")
ips := regIp.FindSubmatch(ipClient)
if len(ips) == 0 {
addr = string( ctx.Request.Header.Peek("X-ProxyUser-Ip") )
} else {
addr = string(ips[0])
}
}
for _, str := range cfg.Access.Allow {
if strings.HasPrefix(addr, str) {
apis.Handler(ctx)
return
}
}
logs.DebugLog(addr, ctx.Request.Header.String() )
ctx.Error(`DataBase is currently updating.
It may took from a few minutes to half an hour.
We are sorry for inconvenience.`, fasthttp.StatusForbidden)
}
// add cfg refresh routers, ignore errors
apisRoute := ApiRoutes{
"/httpgo/cfg/reload": {
Desc: "full routers list",
Fnc: func (ctx *fasthttp.RequestCtx) (interface{}, error) {
//err = yaml.Unmarshal(buf, &cfg)
return "config reload", nil
},
},
}
_ = apis.AddRoutes(apisRoute)
}
return &HttpGo{
mainServer: cfg.Server,
listener: listener,
broadcast: make(chan string),
apis: apis,
cfg: cfg,
}
}
// Run starting http or https server according to secure
// certFile and keyFile are paths to TLS certificate and key files for https server
func (a *HttpGo) Run(secure bool, certFile, keyFile string) error {
go a.listenOnShutdown()
if secure {
return a.mainServer.ServeTLS(a.listener, certFile, keyFile)
}
return a.mainServer.Serve(a.listener)
}
// listenOnShutdown implement correct shutdown server
func (a *HttpGo) listenOnShutdown() {
ch := make(chan os.Signal)
KillSignal := syscall.Signal(a.cfg.KillSignal)
// syscall.SIGTTIN
signal.Notify(ch, os.Interrupt, os.Kill, KillSignal, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
logs.StatusLog("Shutdown service starting %v on signal '%v'", time.Now(), KillSignal)
signShut := <-ch
logs.StatusLog(signShut.String())
close(a.broadcast)
err := a.mainServer.Shutdown()
if err != nil {
logs.ErrorLog(err)
}
err = a.listener.Close()
if err != nil {
logs.ErrorLog(err)
}
}
// fastHTTPLogger wrap logging server
type fastHTTPLogger struct {
logs.LogsType
}
func (log *fastHTTPLogger) Printf(mess string, args ...interface{}) {
args = append([]interface{}{mess}, args...)
if strings.Contains(mess, "error") {
logs.ErrorLog(errors.New("fasthttp"), args...)
} else {
logs.DebugLog(args...)
}
}