mirror of
https://github.com/GrassrootsEconomics/cic-dw.git
synced 2026-05-27 15:07:57 +02:00
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:
@@ -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"`
|
||||
|
||||
22
cmd/main.go
22
cmd/main.go
@@ -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
18
cmd/server.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user