From 0507cdb3d11c1a4e85d577764191b45081fe21db Mon Sep 17 00:00:00 2001 From: alexvanin Date: Fri, 10 May 2019 11:21:59 +0300 Subject: [PATCH] Initial commit This commit contains skeleton of the application with uber/fx DI library. --- go.mod | 10 +++++++++ go.sum | 17 +++++++++++++++ main.go | 42 ++++++++++++++++++++++++++++++++++++ modules/grace/graceful.go | 26 ++++++++++++++++++++++ modules/settings/settings.go | 20 +++++++++++++++++ modules/tokens/.gitkeep | 0 6 files changed, 115 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 modules/grace/graceful.go create mode 100644 modules/settings/settings.go create mode 100644 modules/tokens/.gitkeep diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a279677 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module galched-bot + +require ( + github.com/stretchr/testify v1.3.0 // indirect + go.uber.org/atomic v1.3.2 // indirect + go.uber.org/dig v1.7.0 // indirect + go.uber.org/fx v1.9.0 + go.uber.org/goleak v0.10.0 // indirect + go.uber.org/multierr v1.1.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..f41b6c3 --- /dev/null +++ b/go.sum @@ -0,0 +1,17 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +go.uber.org/atomic v1.3.2 h1:2Oa65PReHzfn29GpvgsYwloV9AVFHPDk8tYxt2c2tr4= +go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= +go.uber.org/dig v1.7.0 h1:E5/L92iQTNJTjfgJF2KgU+/JpMaiuvK2DHLBj0+kSZk= +go.uber.org/dig v1.7.0/go.mod h1:z+dSd2TP9Usi48jL8M3v63iSBVkiwtVyMKxMZYYauPg= +go.uber.org/fx v1.9.0 h1:7OAz8ucp35AU8eydejpYG7QrbE8rLKzGhHbZlJi5LYY= +go.uber.org/fx v1.9.0/go.mod h1:mFdUyAUuJ3w4jAckiKSKbldsxy1ojpAMJ+dVZg5Y0Aw= +go.uber.org/goleak v0.10.0 h1:G3eWbSNIskeRqtsN/1uI5B+eP73y3JUuBsv9AZjehb4= +go.uber.org/goleak v0.10.0/go.mod h1:VCZuO8V8mFPlL0F5J5GK1rtHV3DrFcQ1R8ryq7FK0aI= +go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI= +go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= diff --git a/main.go b/main.go new file mode 100644 index 0000000..8ac8bd3 --- /dev/null +++ b/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "context" + "log" + + "galched-bot/modules/grace" + "galched-bot/modules/settings" + + "go.uber.org/fx" +) + +type ( + silentPrinter struct{} + + appParam struct { + fx.In + + Context context.Context + Settings settings.Settings + } +) + +func (s *silentPrinter) Printf(str string, i ...interface{}) {} + +func start(p appParam) { + log.Print("main: starting galched-bot v", p.Settings.Version) + <-p.Context.Done() +} + +func main() { + var err error + app := fx.New( + fx.Logger(new(silentPrinter)), + fx.Provide(settings.NewSettings, grace.NewGracefulContext), + fx.Invoke(start)) + + err = app.Start(context.Background()) + if err != nil { + log.Fatal(err) + } +} diff --git a/modules/grace/graceful.go b/modules/grace/graceful.go new file mode 100644 index 0000000..1570c85 --- /dev/null +++ b/modules/grace/graceful.go @@ -0,0 +1,26 @@ +package grace + +import ( + "context" + "log" + "os" + "os/signal" + "syscall" +) + +func NewGracefulContext() context.Context { + ctx, cancel := context.WithCancel(context.Background()) + + go func() { + ch := make(chan os.Signal, 1) + signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) + select { + case <-ch: + log.Print("ctx: caught interrupt signal") + cancel() + case <-ctx.Done(): + } + }() + + return ctx +} diff --git a/modules/settings/settings.go b/modules/settings/settings.go new file mode 100644 index 0000000..6e6f4ba --- /dev/null +++ b/modules/settings/settings.go @@ -0,0 +1,20 @@ +package settings + +const ( + version = "3.0.0" + discordTokenPath = "./tokens/.discordtoken" +) + +type ( + Settings struct { + Version string + DiscordTokenPath string + } +) + +func NewSettings() Settings { + return Settings{ + Version: version, + DiscordTokenPath: discordTokenPath, + } +} diff --git a/modules/tokens/.gitkeep b/modules/tokens/.gitkeep new file mode 100644 index 0000000..e69de29