server: init api server

- echo (https://echo.labstack.com/) as web framework
- carbon for auto date-range parsing
- add dashboard sql queries
This commit is contained in:
2022-05-11 16:57:56 +03:00
parent f5642c0e05
commit 128b15407a
10 changed files with 204 additions and 1 deletions

View File

@@ -20,6 +20,9 @@ type config struct {
Postgres string `koanf:"postgres"`
Redis string `koanf:"redis"`
}
Server struct {
Address string `koanf:"address"`
}
Chain struct {
RpcProvider string `koanf:"rpc"`
TokenRegistry string `koanf:"index"`

View File

@@ -1,6 +1,7 @@
package main
import (
"context"
cic_net "github.com/grassrootseconomics/cic-go/net"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/knadh/koanf"
@@ -10,6 +11,8 @@ import (
"golang.org/x/sys/unix"
"os"
"os/signal"
"strings"
"time"
)
var (
@@ -65,6 +68,17 @@ func main() {
}
}()
server := initHTTPServer()
go func() {
if err := server.Start(conf.Server.Address); err != nil {
if strings.Contains(err.Error(), "Server closed") {
log.Info().Msg("shutting down server")
} else {
log.Fatal().Err(err).Msg("could not start server")
}
}
}()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, unix.SIGTERM, unix.SIGINT, unix.SIGTSTP)
for {
@@ -78,5 +92,11 @@ func main() {
}
processor.Shutdown()
log.Info().Msg("gracefully shutdown processor and scheduler")
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err = server.Shutdown(ctx)
if err != nil {
log.Fatal().Err(err).Msg("could not shut down server")
}
log.Info().Msg("gracefully shutdown processor, scheduler and server")
}

18
cmd/server.go Normal file
View File

@@ -0,0 +1,18 @@
package main
import (
"cic-dw/internal/dashboard"
"github.com/labstack/echo/v4"
)
func initHTTPServer() *echo.Echo {
server := echo.New()
server.HideBanner = true
// TODO: Remove after stable release
server.Debug = true
dashboard.InitDashboardApi(server, db, preparedQueries.dashboard)
return server
}