Skip to content

Commit

Permalink
✨ Add HTTP server scaffolding
Browse files Browse the repository at this point in the history
  • Loading branch information
sameersubudhi committed Jan 22, 2024
1 parent 87774a9 commit 1440f8c
Show file tree
Hide file tree
Showing 15 changed files with 723 additions and 26 deletions.
101 changes: 99 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,112 @@
package main

import (
"context"
"errors"
"flag"
"fmt"
"os"
"os/signal"
"path"
"strings"
"sync"
"syscall"

"github.com/LiskHQ/op-fault-detector/pkg/api"
"github.com/LiskHQ/op-fault-detector/pkg/config"
"github.com/LiskHQ/op-fault-detector/pkg/log"
"github.com/spf13/viper"
)

var apiServer *api.HTTPServerWrapper

func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

logger, err := log.NewDefaultProductionLogger()
if err != nil {
panic(err)
}

// To be removed after implementation
logger.Info("Running fault detector")
configFilepath := flag.String("config", "./config.yaml", "Path to the config file")
flag.Parse()
config, err := getAppConfig(logger, *configFilepath)
if err != nil {
panic(err)
}

wg := sync.WaitGroup{}

doneChan := make(chan struct{})
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)

// Start Fault Detector

// Start API Server
serverChan := make(chan error, 1)
apiServer = api.NewHTTPServer(ctx, logger, &wg, config, serverChan)
wg.Add(1)
go apiServer.Start()

go func() {
wg.Wait()
close(doneChan)
}()

for {
select {
case <-doneChan:
performCleanup(logger)
return

case <-signalChan:
performCleanup(logger)
return

case err := <-serverChan:
logger.Errorf("Received error of %v", err)
return
}
}
}

// getAppConfig is the function that takes in the absolute path to the config file, parses the content and returns it.
func getAppConfig(logger log.Logger, configFilepath string) (*config.Config, error) {
configDir := path.Dir(configFilepath)
configFilenameWithExt := path.Base(configFilepath)

splits := strings.FieldsFunc(configFilenameWithExt, func(r rune) bool { return r == '.' })
configType := splits[len(splits)-1] // Config file extension

viper.AddConfigPath(".")
viper.AddConfigPath("..")
viper.AddConfigPath("$HOME/.op-fault-detector")
viper.AddConfigPath(configDir)
viper.SetConfigName(configFilenameWithExt)
viper.SetConfigType(configType)
err := viper.ReadInConfig()
if err != nil {
return nil, fmt.Errorf("failed to load the config from disk: %w", err)
}

var config config.Config
err = viper.Unmarshal(&config)
if err != nil {
return nil, errors.New("failed to unmarshal config. Verify the 'Config' struct definition in 'pkg/config/config.go'")
}

if err := config.Validate(); err != nil {
return nil, err
}

return &config, nil
}

func performCleanup(logger log.Logger) {
err := apiServer.Stop()
if err != nil {
logger.Error("Server shutdown not successful: %w", err)
}
}
15 changes: 15 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# General system configurations
system:
log_level: "info"

# API related configurations
api:
server:
host: "127.0.0.1"
port: 8080
base_path: "/api"
register_versions:
- v1

# Faultdetector configurations
fault_detector:
43 changes: 40 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ go 1.21.5

require (
github.com/ethereum/go-ethereum v1.13.10
github.com/gin-gonic/gin v1.9.1
github.com/magiconair/properties v1.8.7
github.com/spf13/viper v1.18.2
go.uber.org/multierr v1.10.0
go.uber.org/zap v1.26.0
)

Expand All @@ -12,26 +16,59 @@ require (
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/bits-and-blooms/bitset v1.10.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/bytedance/sonic v1.10.2 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/crate-crypto/go-kzg-4844 v0.7.0 // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/ethereum/c-kzg-4844 v0.4.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-ole/go-ole v1.2.5 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.17.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.15.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)
Loading

0 comments on commit 1440f8c

Please sign in to comment.