Skip to content

Commit

Permalink
upgraded routes code
Browse files Browse the repository at this point in the history
  • Loading branch information
ardan-bkennedy committed Jan 22, 2024
1 parent bbcdcd9 commit bff5736
Show file tree
Hide file tree
Showing 60 changed files with 4,047 additions and 447 deletions.
139 changes: 0 additions & 139 deletions app/services/engine/handlers/handlers.go

This file was deleted.

96 changes: 0 additions & 96 deletions app/services/engine/handlers/v1/checkgrp/checkgrp.go

This file was deleted.

43 changes: 14 additions & 29 deletions app/services/engine/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import (
"github.com/ardanlabs/conf/v3"
"github.com/ardanlabs/ethereum"
"github.com/ardanlabs/ethereum/currency"
"github.com/ardanlabs/liarsdice/app/services/engine/handlers"
"github.com/ardanlabs/liarsdice/app/services/engine/v1/build/all"
scbank "github.com/ardanlabs/liarsdice/business/contract/go/bank"
"github.com/ardanlabs/liarsdice/business/core/bank"
"github.com/ardanlabs/liarsdice/business/web/v1/auth"
"github.com/ardanlabs/liarsdice/business/web/v1/debug"
"github.com/ardanlabs/liarsdice/business/web/v1/mux"
"github.com/ardanlabs/liarsdice/foundation/events"
"github.com/ardanlabs/liarsdice/foundation/keystore"
"github.com/ardanlabs/liarsdice/foundation/logger"
Expand Down Expand Up @@ -82,12 +84,13 @@ func run(ctx context.Context, log *logger.Logger) error {
cfg := struct {
conf.Version
Web struct {
ReadTimeout time.Duration `conf:"default:5s"`
WriteTimeout time.Duration `conf:"default:10s"`
IdleTimeout time.Duration `conf:"default:120s"`
ShutdownTimeout time.Duration `conf:"default:20s"`
APIHost string `conf:"default:0.0.0.0:3000"`
DebugHost string `conf:"default:0.0.0.0:4000"`
ReadTimeout time.Duration `conf:"default:5s"`
WriteTimeout time.Duration `conf:"default:10s"`
IdleTimeout time.Duration `conf:"default:120s"`
ShutdownTimeout time.Duration `conf:"default:20s"`
APIHost string `conf:"default:0.0.0.0:3000"`
DebugHost string `conf:"default:0.0.0.0:4000"`
CORSAllowedOrigins []string `conf:"default:*"`
}
Vault struct {
Address string `conf:"default:http://vault-service.liars-system.svc.cluster.local:8200"`
Expand Down Expand Up @@ -219,16 +222,8 @@ func run(ctx context.Context, log *logger.Logger) error {

log.Info(ctx, "startup", "status", "debug v1 router started", "host", cfg.Web.DebugHost)

// The Debug function returns a mux to listen and serve on for all the debug
// related endpoints. This includes the standard library endpoints.

// Construct the mux for the debug calls.
debugMux := handlers.DebugMux(build, log)

// Start the service listening for debug requests.
// Not concerned with shutting this down with load shedding.
go func() {
if err := http.ListenAndServe(cfg.Web.DebugHost, debugMux); err != nil {
if err := http.ListenAndServe(cfg.Web.DebugHost, debug.Mux()); err != nil {
log.Error(ctx, "shutdown", "status", "debug v1 router closed", "host", cfg.Web.DebugHost, "ERROR", err)
}
}()
Expand All @@ -238,13 +233,10 @@ func run(ctx context.Context, log *logger.Logger) error {

log.Info(ctx, "startup", "status", "initializing V1 API support")

// Make a channel to listen for an interrupt or terminate signal from the OS.
// Use a buffered channel because the signal package requires it.
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)

// Construct the mux for the API calls.
apiMux := handlers.APIMux(handlers.APIMuxConfig{
cfgMux := mux.Config{
Shutdown: shutdown,
Log: log,
Auth: authClient,
Expand All @@ -255,23 +247,19 @@ func run(ctx context.Context, log *logger.Logger) error {
ActiveKID: cfg.Auth.ActiveKID,
BankTimeout: cfg.Bank.Timeout,
ConnectTimeout: cfg.Game.ConnectTimeout,
}, handlers.WithCORS("*"))
}

// Construct a server to service the requests against the mux.
api := http.Server{
Addr: cfg.Web.APIHost,
Handler: apiMux,
Handler: mux.WebAPI(cfgMux, all.Routes(), mux.WithCORS(cfg.Web.CORSAllowedOrigins)),
ReadTimeout: cfg.Web.ReadTimeout,
WriteTimeout: cfg.Web.WriteTimeout,
IdleTimeout: cfg.Web.IdleTimeout,
ErrorLog: logger.NewStdLogger(log, logger.LevelError),
}

// Make a channel to listen for errors coming from the listener. Use a
// buffered channel so the goroutine can exit if we don't collect this error.
serverErrors := make(chan error, 1)

// Start the service listening for api requests.
go func() {
log.Info(ctx, "startup", "status", "api router started", "host", api.Addr)
serverErrors <- api.ListenAndServe()
Expand All @@ -280,7 +268,6 @@ func run(ctx context.Context, log *logger.Logger) error {
// -------------------------------------------------------------------------
// Shutdown

// Blocking main and waiting for shutdown.
select {
case err := <-serverErrors:
return fmt.Errorf("server error: %w", err)
Expand All @@ -293,11 +280,9 @@ func run(ctx context.Context, log *logger.Logger) error {
log.Info(ctx, "shutdown", "status", "shutdown web socket channels")
evts.Shutdown()

// Give outstanding requests a deadline for completion.
ctx, cancel := context.WithTimeout(context.Background(), cfg.Web.ShutdownTimeout)
defer cancel()

// Asking listener to shut down and shed load.
if err := api.Shutdown(ctx); err != nil {
api.Close()
return fmt.Errorf("could not stop server gracefully: %w", err)
Expand Down
37 changes: 37 additions & 0 deletions app/services/engine/v1/build/all/all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Package all binds all the routes into the specified app.
package all

import (
"github.com/ardanlabs/liarsdice/app/services/engine/v1/handlers/checkgrp"
"github.com/ardanlabs/liarsdice/app/services/engine/v1/handlers/gamegrp"
"github.com/ardanlabs/liarsdice/business/web/v1/mux"
"github.com/ardanlabs/liarsdice/foundation/web"
)

// Routes constructs the add value which provides the implementation of
// of RouteAdder for specifying what routes to bind to this instance.
func Routes() add {
return add{}
}

type add struct{}

// Add implements the RouterAdder interface.
func (add) Add(app *web.App, cfg mux.Config) {
checkgrp.Routes(app, checkgrp.Config{
Build: cfg.Build,
Log: cfg.Log,
})

gamegrp.Routes(app, gamegrp.Config{
Log: cfg.Log,
Auth: cfg.Auth,
Converter: cfg.Converter,
Bank: cfg.Bank,
Evts: cfg.Evts,
AnteUSD: cfg.AnteUSD,
ActiveKID: cfg.ActiveKID,
BankTimeout: cfg.BankTimeout,
ConnectTimeout: cfg.ConnectTimeout,
})
}
Loading

0 comments on commit bff5736

Please sign in to comment.