Initial commit

This commit contains skeleton of the application with
uber/fx DI library.
This commit is contained in:
Alex Vanin 2019-05-10 11:21:59 +03:00
commit 0507cdb3d1
6 changed files with 115 additions and 0 deletions

10
go.mod Normal file
View file

@ -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
)

17
go.sum Normal file
View file

@ -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=

42
main.go Normal file
View file

@ -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)
}
}

26
modules/grace/graceful.go Normal file
View file

@ -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
}

View file

@ -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,
}
}

0
modules/tokens/.gitkeep Normal file
View file