|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/go-chi/chi" |
| 9 | + "github.com/go-chi/chi/middleware" |
| 10 | + "github.com/go-chi/render" |
| 11 | + "go.uber.org/zap" |
| 12 | +) |
| 13 | + |
| 14 | +type AppServer struct { |
| 15 | + config *Config |
| 16 | + db *FileDB |
| 17 | + logger *zap.Logger |
| 18 | + server *http.Server |
| 19 | + router http.Handler |
| 20 | +} |
| 21 | + |
| 22 | +func NewAppServer(config *Config) (*AppServer, error) { |
| 23 | + log, err := NewLogger(config) |
| 24 | + if err != nil { |
| 25 | + return nil, err |
| 26 | + } |
| 27 | + log.Info( |
| 28 | + "Starting the service", |
| 29 | + zap.String("prog", Prog), |
| 30 | + zap.String("version", Version), |
| 31 | + zap.Any("config", config), |
| 32 | + ) |
| 33 | + |
| 34 | + app := &AppServer{ |
| 35 | + config: config, |
| 36 | + logger: log, |
| 37 | + db: NewFileDB(log, config.DirPath), |
| 38 | + } |
| 39 | + |
| 40 | + // Setup routes |
| 41 | + router, err := app.Router() |
| 42 | + if err != nil { |
| 43 | + log.Error("Error setting up routes", zap.Error(err)) |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + app.router = router |
| 47 | + |
| 48 | + return app, err |
| 49 | +} |
| 50 | + |
| 51 | +func (a *AppServer) Router() (*chi.Mux, error) { |
| 52 | + router := chi.NewRouter() |
| 53 | + router.MethodNotAllowed(MethodNotAllowed) |
| 54 | + router.NotFound(NotFound) |
| 55 | + |
| 56 | + // Set middleware |
| 57 | + router.Use(a.panicRecovery) |
| 58 | + router.Use(render.SetContentType(render.ContentTypeJSON)) |
| 59 | + |
| 60 | + // Setup routes |
| 61 | + // handler of export states |
| 62 | + router.Get("/chains", a.GetChains) |
| 63 | + router.Route("/chains/{chain}/validators/{validator}", func(r chi.Router) { |
| 64 | + r.Get("/exports", a.GetChainExports) |
| 65 | + r.Get("/exports/{id}", a.GetChainExport) |
| 66 | + r.Post("/exports/{id}", a.SetChainExport) |
| 67 | + r.Get("/snapshots", a.GetChainSnapshots) |
| 68 | + r.Get("/snapshots/{id}", a.GetChainSnapshot) |
| 69 | + r.Post("/snapshots/{id}", a.SetChainSnapshot) |
| 70 | + }) |
| 71 | + |
| 72 | + return router, nil |
| 73 | +} |
| 74 | + |
| 75 | +func (a *AppServer) loggingMiddleware(next http.Handler) http.Handler { |
| 76 | + fn := func(w http.ResponseWriter, r *http.Request) { |
| 77 | + ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor) |
| 78 | + start := time.Now() |
| 79 | + defer func() { |
| 80 | + a.logger.Info("client request", |
| 81 | + zap.Duration("latency", time.Since(start)), |
| 82 | + zap.Int("status", ww.Status()), |
| 83 | + zap.Int("bytes", ww.BytesWritten()), |
| 84 | + zap.String("client_ip", r.RemoteAddr), |
| 85 | + zap.String("method", r.Method), |
| 86 | + zap.String("path", r.URL.Path), |
| 87 | + zap.String("request-id", middleware.GetReqID(r.Context()))) |
| 88 | + }() |
| 89 | + |
| 90 | + next.ServeHTTP(ww, r) |
| 91 | + } |
| 92 | + return http.HandlerFunc(fn) |
| 93 | +} |
| 94 | + |
| 95 | +func (a *AppServer) panicRecovery(next http.Handler) http.Handler { |
| 96 | + fn := func(w http.ResponseWriter, r *http.Request) { |
| 97 | + defer func() { |
| 98 | + if rc := recover(); rc != nil { |
| 99 | + err, ok := rc.(error) |
| 100 | + if !ok { |
| 101 | + err = fmt.Errorf("panic: %v", rc) |
| 102 | + } |
| 103 | + a.logger.Error("panic error", |
| 104 | + zap.String("request-id", middleware.GetReqID(r.Context())), |
| 105 | + zap.Error(err)) |
| 106 | + |
| 107 | + render.Render(w, r, ErrInternalServer) |
| 108 | + return |
| 109 | + } |
| 110 | + }() |
| 111 | + next.ServeHTTP(w, r) |
| 112 | + } |
| 113 | + return http.HandlerFunc(fn) |
| 114 | +} |
| 115 | + |
| 116 | +func (a *AppServer) Run() error { |
| 117 | + a.logger.Info("App starting", zap.Any("Config", a.config)) |
| 118 | + |
| 119 | + // Setup server |
| 120 | + server := &http.Server{ |
| 121 | + Addr: a.config.Addr, |
| 122 | + Handler: a.router, |
| 123 | + } |
| 124 | + a.server = server |
| 125 | + |
| 126 | + // Start http server as long-running go routine |
| 127 | + go func() { |
| 128 | + if err := server.ListenAndServe(); err != nil { |
| 129 | + a.logger.Error("failed to start the App HTTP server", zap.Error(err)) |
| 130 | + } |
| 131 | + }() |
| 132 | + |
| 133 | + return nil |
| 134 | +} |
0 commit comments