Skip to content

Commit

Permalink
fix(cmd): add sqlite database file path flag
Browse files Browse the repository at this point in the history
  • Loading branch information
bastean committed Jul 24, 2024
1 parent c99e8f6 commit 8519b93
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 71 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@ Usage: dsgo [OPTIONS]
Example of interoperability between a Web App and a Discord Bot using a layered architecture.
-app string
Discord App Id Token (Required)
Discord App Id Token (required)
-database string
SQLite database file path (default "In-Memory")
-guild string
Discord Test Guild Id (Optional)
Discord Test Guild Id (optional)
-port string
Fiber Server Port (Optional)
Fiber Server Port (optional)
-token string
Discord Bot Token (Required)
Discord Bot Token (required)
```

## Docker (Demo)
Expand Down
18 changes: 8 additions & 10 deletions cmd/dsgo/ds.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,22 @@ var (
Apps = "apps"
)

var Port string

var AppId, BotToken, TestGuildId string

func usage() {
fmt.Printf("Usage: %s [OPTIONS]\n\n", cli)
fmt.Print("Example of interoperability between a Web App and a Discord Bot using a layered architecture.\n\n")
flag.PrintDefaults()
}

func main() {
flag.StringVar(&Port, "port", env.Server.Fiber.Port, "Fiber Server Port (Optional)")
flag.StringVar(&env.DatabaseSQLiteName, "database", env.DatabaseSQLiteName, "SQLite database file path (default \"In-Memory\")")

flag.StringVar(&env.ServerFiberPort, "port", env.ServerFiberPort, "Fiber Server Port (optional)")

flag.StringVar(&AppId, "app", env.Bot.Discord.AppId, "Discord App Id Token (Required)")
flag.StringVar(&env.BotDiscordAppId, "app", env.BotDiscordAppId, "Discord App Id Token (required)")

flag.StringVar(&BotToken, "token", env.Bot.Discord.BotToken, "Discord Bot Token (Required)")
flag.StringVar(&env.BotDiscordToken, "token", env.BotDiscordToken, "Discord Bot Token (required)")

flag.StringVar(&TestGuildId, "guild", env.Bot.Discord.TestGuildId, "Discord Test Guild Id (Optional)")
flag.StringVar(&env.BotDiscordTestGuildId, "guild", env.BotDiscordTestGuildId, "Discord Test Guild Id (optional)")

flag.Usage = usage

Expand All @@ -60,13 +58,13 @@ func main() {
log.Starting(Apps)

go func() {
if err := server.Up(Port); err != nil {
if err := server.Up(); err != nil {
log.Fatal(err.Error())
}
}()

go func() {
if err := bot.Up(AppId, BotToken, TestGuildId); err != nil {
if err := bot.Up(); err != nil {
log.Fatal(err.Error())
}
}()
Expand Down
7 changes: 4 additions & 3 deletions internal/app/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bot
import (
"github.com/bastean/dsgo/internal/app/bot/command"
"github.com/bastean/dsgo/internal/app/bot/handler"
"github.com/bastean/dsgo/internal/pkg/service/env"
"github.com/bastean/dsgo/internal/pkg/service/errors"
"github.com/bastean/dsgo/internal/pkg/service/logger/log"
"github.com/bwmarrin/discordgo"
Expand All @@ -21,17 +22,17 @@ var (
Session *discordgo.Session
)

func Up(app, token, guild string) error {
func Up() error {
log.Starting(Bot.Discord)

Session, err = discordgo.New("Bot " + token)
Session, err = discordgo.New("Bot " + env.BotDiscordToken)

if err != nil {
log.CannotBeStarted(Bot.Discord)
return errors.BubbleUp(err, "Up")
}

_, err = Session.ApplicationCommandBulkOverwrite(app, guild, command.Commands)
_, err = Session.ApplicationCommandBulkOverwrite(env.BotDiscordAppId, env.BotDiscordTestGuildId, command.Commands)

if err != nil {
log.CannotBeStarted(Bot.Discord)
Expand Down
8 changes: 4 additions & 4 deletions internal/app/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var Files embed.FS

var App *fiber.App

func Up(port string) error {
func Up() error {
log.Starting(Server.Fiber)

App = fiber.New(fiber.Config{
Expand All @@ -36,16 +36,16 @@ func Up(port string) error {

router.Routing(App, &Files)

if err := App.Listen(":" + port); err != nil {
if err := App.Listen(":" + env.ServerFiberPort); err != nil {
log.CannotBeStarted(Server.Fiber)
return errors.BubbleUp(err, "Up")
}

log.Started(Server.Fiber)

log.Info(fmt.Sprintf("%s listening on :%s", Server.Fiber, port))
log.Info(fmt.Sprintf("%s listening on :%s", Server.Fiber, env.ServerFiberPort))

if proxy, ok := env.Server.Fiber.HasProxy(); ok {
if proxy, ok := env.HasServerFiberProxy(); ok {
log.Info(fmt.Sprintf("%s proxy listening on :%s", Server.Fiber, proxy))
}

Expand Down
20 changes: 6 additions & 14 deletions internal/pkg/service/env/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,9 @@ import (
"os"
)

type Discord struct {
AppId, PublicKey, BotToken, TestGuildId string
}

var Bot = &struct {
*Discord
}{
Discord: &Discord{
AppId: os.Getenv("DSGO_BOT_DISCORD_APP_ID"),
PublicKey: os.Getenv("DSGO_BOT_DISCORD_APP_PUBLIC_KEY"),
BotToken: os.Getenv("DSGO_BOT_DISCORD_APP_TOKEN"),
TestGuildId: os.Getenv("DSGO_BOT_DISCORD_TEST_GUILD_ID"),
},
}
var (
BotDiscordAppId = os.Getenv("DSGO_BOT_DISCORD_APP_ID")
BotDiscordPublicKey = os.Getenv("DSGO_BOT_DISCORD_APP_PUBLIC_KEY")
BotDiscordToken = os.Getenv("DSGO_BOT_DISCORD_APP_TOKEN")
BotDiscordTestGuildId = os.Getenv("DSGO_BOT_DISCORD_TEST_GUILD_ID")
)
26 changes: 7 additions & 19 deletions internal/pkg/service/env/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,11 @@ import (
"os"
)

type MySQL struct {
DSN, Name string
}

type SQLite struct {
Name string
}
var (
DatabaseMySQLDSN = os.Getenv("DATABASE_MYSQL_DSN")
DatabaseMySQLName = os.Getenv("DATABASE_MYSQL_NAME")
)

var Database = &struct {
*MySQL
*SQLite
}{
MySQL: &MySQL{
DSN: os.Getenv("DATABASE_MYSQL_DSN"),
Name: os.Getenv("DATABASE_MYSQL_NAME"),
},
SQLite: &SQLite{
Name: os.Getenv("DATABASE_SQLITE_NAME"),
},
}
var (
DatabaseSQLiteName = os.Getenv("DATABASE_SQLITE_NAME")
)
20 changes: 6 additions & 14 deletions internal/pkg/service/env/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,17 @@ import (
"os"
)

type Fiber struct {
URL, Port string
}
var (
ServerFiberURL = os.Getenv("DSGO_SERVER_FIBER_URL")
ServerFiberPort = os.Getenv("DSGO_SERVER_FIBER_PORT")
)

func (fiber *Fiber) HasProxy() (string, bool) {
func HasServerFiberProxy() (string, bool) {
proxy := os.Getenv("DSGO_DEV_AIR_PROXY_PORT")

if proxy != "" && proxy != fiber.Port {
if proxy != "" && proxy != ServerFiberPort {
return proxy, true
}

return "", false
}

var Server = &struct {
*Fiber
}{
Fiber: &Fiber{
URL: os.Getenv("DSGO_SERVER_FIBER_URL"),
Port: os.Getenv("DSGO_SERVER_FIBER_PORT"),
},
}
6 changes: 3 additions & 3 deletions internal/pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ var (

func OpenMySQL() error {
Database, err = sql.OpenMySQL(
env.Database.MySQL.DSN,
env.Database.MySQL.Name,
env.DatabaseMySQLDSN,
env.DatabaseMySQLName,
)

if err != nil {
Expand All @@ -42,7 +42,7 @@ func OpenMySQL() error {

func OpenSQLite() error {
Database, err = sql.OpenSQLite(
env.Database.SQLite.Name,
env.DatabaseSQLiteName,
)

if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/context/infrastructure/persistence/sql/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func OpenMySQL(dsn, name string) (*Database, error) {
}

func OpenSQLite(filename string) (*Database, error) {
if filename == "" {
filename = "file::memory:?cache=shared"
}

session, err := gorm.Open(sqlite.Open(filename), Config)

if err != nil {
Expand Down

0 comments on commit 8519b93

Please sign in to comment.