-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
611 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
# Go workspace file | ||
go.work | ||
|
||
./calaos-container | ||
bin | ||
debian/calaos-container | ||
debian/.debhelper | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.