2019-11-06 12:33:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-12-13 16:02:48 +00:00
|
|
|
"crypto/ecdsa"
|
2019-12-21 10:26:14 +00:00
|
|
|
"net/http"
|
|
|
|
_ "net/http/pprof"
|
2019-11-06 12:33:46 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
2019-12-21 10:26:14 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2019-11-06 12:33:46 +00:00
|
|
|
"go.uber.org/zap"
|
2019-12-13 16:02:48 +00:00
|
|
|
"google.golang.org/grpc/grpclog"
|
2019-11-06 12:33:46 +00:00
|
|
|
)
|
|
|
|
|
2019-12-21 10:26:14 +00:00
|
|
|
type router struct {
|
|
|
|
pool *Pool
|
2019-11-06 12:33:46 +00:00
|
|
|
log *zap.Logger
|
|
|
|
timeout time.Duration
|
2019-12-13 16:02:48 +00:00
|
|
|
key *ecdsa.PrivateKey
|
2019-11-06 12:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var (
|
2019-12-21 10:26:14 +00:00
|
|
|
v = settings()
|
|
|
|
l = newLogger(v)
|
|
|
|
g = newGracefulContext(l)
|
2019-11-06 12:33:46 +00:00
|
|
|
)
|
|
|
|
|
2019-12-13 16:02:48 +00:00
|
|
|
if v.GetBool("verbose") {
|
2019-12-21 10:26:14 +00:00
|
|
|
grpclog.SetLoggerV2(gRPCLogger(l))
|
2019-12-13 16:02:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-21 10:26:14 +00:00
|
|
|
r := &router{
|
|
|
|
log: l,
|
|
|
|
key: fetchKey(l, v),
|
|
|
|
pool: newPool(g, l, v),
|
|
|
|
timeout: v.GetDuration("request_timeout"),
|
2019-11-06 12:33:46 +00:00
|
|
|
}
|
|
|
|
|
2019-12-21 10:26:14 +00:00
|
|
|
go checkConnection(g, r.pool)
|
2019-11-06 12:33:46 +00:00
|
|
|
|
|
|
|
e := echo.New()
|
|
|
|
e.Debug = false
|
|
|
|
e.HidePort = true
|
|
|
|
e.HideBanner = true
|
|
|
|
|
2019-12-21 10:26:14 +00:00
|
|
|
e.GET("/:cid/:oid", r.receiveFile)
|
|
|
|
|
|
|
|
// enable metrics
|
|
|
|
if v.GetBool("metrics") {
|
|
|
|
l.Info("enabled /metrics")
|
|
|
|
e.GET("/metrics", echo.WrapHandler(promhttp.Handler()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// enable pprof
|
|
|
|
if v.GetBool("pprof") {
|
|
|
|
l.Info("enabled /debug/pprof")
|
|
|
|
e.Any("/debug/pprof*", echo.WrapHandler(http.DefaultServeMux))
|
|
|
|
}
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2019-11-06 12:33:46 +00:00
|
|
|
go func() {
|
2019-12-21 10:26:14 +00:00
|
|
|
l.Info("run gateway server",
|
2019-11-06 12:33:46 +00:00
|
|
|
zap.String("address", v.GetString("listen_address")))
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2019-11-06 12:33:46 +00:00
|
|
|
if err := e.Start(v.GetString("listen_address")); err != nil {
|
2019-12-21 10:26:14 +00:00
|
|
|
l.Panic("could not start server", zap.Error(err))
|
2019-11-06 12:33:46 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-12-21 10:26:14 +00:00
|
|
|
<-g.Done()
|
2019-11-06 12:33:46 +00:00
|
|
|
|
2019-12-21 10:26:14 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*30)
|
2019-11-06 12:33:46 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2019-12-21 10:26:14 +00:00
|
|
|
l.Info("stopping server", zap.Error(e.Shutdown(ctx)))
|
2019-11-06 12:33:46 +00:00
|
|
|
}
|
|
|
|
|
2019-12-21 10:26:14 +00:00
|
|
|
func checkConnection(ctx context.Context, p *Pool) {
|
|
|
|
tick := time.NewTicker(time.Second * 15)
|
|
|
|
|
2019-11-06 12:33:46 +00:00
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
break loop
|
|
|
|
case <-tick.C:
|
2019-12-21 10:26:14 +00:00
|
|
|
p.reBalance(ctx)
|
2019-11-06 12:33:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tick.Stop()
|
2019-12-21 10:26:14 +00:00
|
|
|
|
|
|
|
p.log.Info("stop connection worker")
|
2019-11-06 12:33:46 +00:00
|
|
|
}
|