Skip to content

Commit

Permalink
add basic http server structure
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulh committed Jul 15, 2023
1 parent 8824c34 commit 0e87813
Show file tree
Hide file tree
Showing 10 changed files with 611 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# Go workspace file
go.work

./calaos-container
bin
debian/calaos-container
debian/.debhelper
Expand Down
87 changes: 87 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package app

import (
"strconv"
"sync"
"time"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
fiberLog "github.com/gofiber/fiber/v2/middleware/logger"

"github.com/calaos/calaos-container/config"
logger "github.com/calaos/calaos-container/log"
"github.com/sirupsen/logrus"
)

const (
maxFileSize = 1 * 1024 * 1024 * 1024
)

type AppServer struct {
quitHeartbeat chan interface{}
wgDone sync.WaitGroup

appFiber *fiber.App
}

var logging *logrus.Entry

func init() {
logging = logger.NewLogger("app")
}

// Init the app
func NewApp() (a *AppServer, err error) {
logging.Infoln("Init server")

a = &AppServer{
quitHeartbeat: make(chan interface{}),
appFiber: fiber.New(fiber.Config{
ServerHeader: "Greenhouse (Linux)",
ReadTimeout: time.Second * 20,
AppName: "Greenhouse",
DisableStartupMessage: true,
EnablePrintRoutes: false,
BodyLimit: maxFileSize,
}),
}

a.appFiber.
Use(fiberLog.New(fiberLog.Config{}))

a.appFiber.Use(cors.New(cors.Config{
AllowOrigins: "http://127.0.0.1",
}))

a.appFiber.Hooks().OnShutdown(func() error {
a.wgDone.Done()
return nil
})

//API
//api := a.appFiber.Group("/api")

return
}

// Run the app
func (a *AppServer) Start() {
addr := config.Config.String("general.address") + ":" + strconv.Itoa(config.Config.Int("general.port"))

logging.Infoln("\u21D2 Server listening on", addr)

go func() {
if err := a.appFiber.Listen(addr); err != nil {
logging.Fatalf("Failed to listen http server: %v", err)
}
}()
a.wgDone.Add(1)
}

// Stop the app
func (a *AppServer) Shutdown() {
close(a.quitHeartbeat)
a.appFiber.Shutdown()
a.wgDone.Wait()
}
Binary file added calaos-container
Binary file not shown.
73 changes: 73 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package config

import (
"log"
"os"
"strings"

"github.com/sirupsen/logrus"

"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/json"
"github.com/knadh/koanf/parsers/toml"
"github.com/knadh/koanf/providers/confmap"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
)

// Config is the main app config
var Config = koanf.New(".")

var (
defaultConfig map[string]interface{} = map[string]interface{}{
"general.port": 8000,
"general.address": "",
"log.default": "info",
}

//a dedicated logger must be used here to avoid conflict
logging *logrus.Logger
)

// InitConfig from config file, must be called early
func InitConfig(conffile *string) error {
logging = logrus.New()
logging.Formatter = &logrus.TextFormatter{
DisableTimestamp: true,
QuoteEmptyFields: true,
}
logging.SetLevel(logrus.TraceLevel)
log.SetOutput(os.Stdout)

// Load default values using the confmap provider.
// We provide a flat map with the "." delimiter.
// A nested map can be loaded by setting the delimiter to an empty string "".
Config.Load(confmap.Provider(defaultConfig, "."), nil)

// Load JSON config.
errJson := Config.Load(file.Provider(*conffile), json.Parser())

// Load TOML config and merge into the previously loaded config (because we can).
errToml := Config.Load(file.Provider(*conffile), toml.Parser())

if errJson != nil && errToml != nil {
logging.WithField("parser", "json").Errorf("error loading config (%s): %v", *conffile, errJson)
logging.WithField("parser", "toml").Errorf("error loading config (%s): %v", *conffile, errToml)
}

// Load environment variables and merge into the loaded config.
// "GH" is the prefix to filter the env vars by.
// "." is the delimiter used to represent the key hierarchy in env vars.
// The (optional, or can be nil) function can be used to transform
// the env var names, for instance, to lowercase them.
//
// For example, env vars: CALAOS_TYPE and CALAOS_PARENT1_CHILD1_NAME
// will be merged into the "type" and the nested "parent1.child1.name"
// keys in the config file here as we lowercase the key,
// replace `_` with `.` and strip the CALAOS_ prefix so that
// only "parent1.child1.name" remains.
return Config.Load(env.Provider("CALAOS_", ".", func(s string) string {
return strings.Replace(strings.ToLower(
strings.TrimPrefix(s, "CALAOS_")), "_", ".", -1)
}), nil)
}
22 changes: 21 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@ go 1.20

require github.com/containers/podman/v4 v4.5.1

require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/gofiber/fiber/v2 v2.47.0 // indirect
github.com/jawher/mow.cli v1.2.0 // indirect
github.com/knadh/koanf v1.5.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/philhofer/fwd v1.1.2 // indirect
github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94 // indirect
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect
github.com/tinylib/msgp v1.1.8 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.47.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
)

require (
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/Microsoft/go-winio v0.6.0 // indirect
Expand Down Expand Up @@ -34,6 +53,7 @@ require (
github.com/docker/docker-credential-helpers v0.7.0 // indirect
github.com/docker/go-connections v0.4.1-0.20210727194412-58542c764a11 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/fatih/color v1.15.0
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-openapi/analysis v0.21.4 // indirect
github.com/go-openapi/errors v0.20.3 // indirect
Expand Down Expand Up @@ -109,7 +129,7 @@ require (
golang.org/x/mod v0.9.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/term v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/tools v0.7.0 // indirect
Expand Down
Loading

0 comments on commit 0e87813

Please sign in to comment.