Skip to content

Commit

Permalink
feat(targetflag): target flags added to support different application…
Browse files Browse the repository at this point in the history
… and platforms
  • Loading branch information
mj committed Jun 28, 2024
1 parent 7eb001a commit e933222
Show file tree
Hide file tree
Showing 23 changed files with 291 additions and 64 deletions.
19 changes: 11 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,25 @@ check:
golangci-lint run --timeout=20m0s

### building
build: build-cli build-discord build-grpc build-telegram build-http
build: build-cli build-discord-mainnet build-discord-testnet build-grpc build-telegram build-http

build-cli:
go build -o build/pagu-cli ./cmd/cli

build-discord:
go build -o build/pagu-discord ./cmd/discord

build-grpc:
go build -o build/pagu-grpc ./cmd/grpc

go build -o build/pagu-grpc ./cmd/mainnet/grpc

build-discord-mainnet:
go build -o build/pagu-discord-mainnet ./cmd/mainnet/discord

build-discord-testnet:
go build -o build/pagu-discord-testnet ./cmd/testnet/discord

build-telegram:
go build -o build/pagu-telegram ./cmd/telegram
go build -o build/pagu-telegram ./cmd/mainnet/telegram

build-http:
go build -o build/pagu-http ./cmd/http
go build -o build/pagu-http ./cmd/mainnet/http

### pre commit
pre-commit: mock proto fmt check unit_test
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func run(cmd *cobra.Command, args []string) {
botEngine, err := engine.NewBotEngine(configs)
pCmd.ExitOnError(cmd, err)

botEngine.RegisterAllCommands()
botEngine.RegisterCommands()
botEngine.Start()

reader := bufio.NewReader(os.Stdin)
Expand Down
File renamed without changes.
54 changes: 54 additions & 0 deletions cmd/mainnet/discord/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"os"
"os/signal"
"syscall"

"github.com/pagu-project/Pagu/internal/engine"

"github.com/pagu-project/Pagu/internal/platforms/discord"
"github.com/pagu-project/Pagu/pkg/log"

pCmd "github.com/pagu-project/Pagu/cmd"
"github.com/pagu-project/Pagu/config"
"github.com/spf13/cobra"
)

func runCommand(parentCmd *cobra.Command) {
run := &cobra.Command{
Use: "run",
Short: "Runs a mainnet instance of Pagu for Discord",
}

parentCmd.AddCommand(run)

run.Run = func(cmd *cobra.Command, _ []string) {
// load configuration.
configs, err := config.Load(configPath)
pCmd.ExitOnError(cmd, err)

// Initialize global logger.
log.InitGlobalLogger(configs.Logger)

// starting botEngine.
botEngine, err := engine.NewBotEngine(configs)
pCmd.ExitOnError(cmd, err)

discordMainBot, err := discord.NewDiscordBot(botEngine, configs.DiscordMainBot, config.TargetMaskMain)
pCmd.ExitOnError(cmd, err)

err = discordMainBot.Start()
pCmd.ExitOnError(cmd, err)

sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sigChan

if err := discordMainBot.Stop(); err != nil {
pCmd.ExitOnError(cmd, err)
}

botEngine.Stop()
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion cmd/grpc/run.go → cmd/mainnet/grpc/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func runCommand(parentCmd *cobra.Command) {
botEngine, err := engine.NewBotEngine(config)
pCmd.ExitOnError(cmd, err)

botEngine.RegisterAllCommands()
botEngine.RegisterCommands()
botEngine.Start()

grpcServer := grpc.NewServer(botEngine, config.GRPC)
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion cmd/http/run.go → cmd/mainnet/http/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func runCommand(parentCmd *cobra.Command) {
botEngine, err := engine.NewBotEngine(configs)
pCmd.ExitOnError(cmd, err)

botEngine.RegisterAllCommands()
botEngine.RegisterCommands()
botEngine.Start()

httpServer := http.NewHTTPServer(botEngine, configs.HTTP)
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion cmd/telegram/run.go → cmd/mainnet/telegram/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func runCommand(parentCmd *cobra.Command) {

log.InitGlobalLogger(configs.Logger)

botEngine.RegisterAllCommands()
botEngine.RegisterCommands()
botEngine.Start()

chatID := configs.Telegram.ChatID
Expand Down
21 changes: 21 additions & 0 deletions cmd/moderation/discord/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
pagu "github.com/pagu-project/Pagu"
"github.com/pagu-project/Pagu/cmd"
"github.com/spf13/cobra"
)

var configPath string

func main() {
rootCmd := &cobra.Command{
Use: "pagu-discord",
Version: pagu.StringVersion(),
}

rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "./config.yml", "config path ./config.yml")
runCommand(rootCmd)
err := rootCmd.Execute()
cmd.ExitOnError(rootCmd, err)
}
10 changes: 4 additions & 6 deletions cmd/discord/run.go → cmd/moderation/discord/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"syscall"

"github.com/pagu-project/Pagu/internal/engine"

"github.com/pagu-project/Pagu/internal/platforms/discord"
"github.com/pagu-project/Pagu/pkg/log"

Expand Down Expand Up @@ -34,20 +35,17 @@ func runCommand(parentCmd *cobra.Command) {
botEngine, err := engine.NewBotEngine(configs)
pCmd.ExitOnError(cmd, err)

botEngine.RegisterAllCommands()
botEngine.Start()

discordBot, err := discord.NewDiscordBot(botEngine, configs.DiscordBot.Token, configs.DiscordBot)
discordModerationBot, err := discord.NewDiscordBot(botEngine, configs.DiscordModerationBot, config.TargetMaskModerator)
pCmd.ExitOnError(cmd, err)

err = discordBot.Start()
err = discordModerationBot.Start()
pCmd.ExitOnError(cmd, err)

sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sigChan

if err := discordBot.Stop(); err != nil {
if err := discordModerationBot.Stop(); err != nil {
pCmd.ExitOnError(cmd, err)
}

Expand Down
21 changes: 21 additions & 0 deletions cmd/testnet/discord/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
pagu "github.com/pagu-project/Pagu"
"github.com/pagu-project/Pagu/cmd"
"github.com/spf13/cobra"
)

var configPath string

func main() {
rootCmd := &cobra.Command{
Use: "pagu-discord",
Version: pagu.StringVersion(),
}

rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "./config.yml", "config path ./config.yml")
runCommand(rootCmd)
err := rootCmd.Execute()
cmd.ExitOnError(rootCmd, err)
}
54 changes: 54 additions & 0 deletions cmd/testnet/discord/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"os"
"os/signal"
"syscall"

"github.com/pagu-project/Pagu/internal/engine"

"github.com/pagu-project/Pagu/internal/platforms/discord"
"github.com/pagu-project/Pagu/pkg/log"

pCmd "github.com/pagu-project/Pagu/cmd"
"github.com/pagu-project/Pagu/config"
"github.com/spf13/cobra"
)

func runCommand(parentCmd *cobra.Command) {
run := &cobra.Command{
Use: "run",
Short: "Runs a testnet instance of Pagu for Discord",
}

parentCmd.AddCommand(run)

run.Run = func(cmd *cobra.Command, _ []string) {
// load configuration.
configs, err := config.Load(configPath)
pCmd.ExitOnError(cmd, err)

// Initialize global logger.
log.InitGlobalLogger(configs.Logger)

// starting botEngine.
botEngine, err := engine.NewBotEngine(configs)
pCmd.ExitOnError(cmd, err)

discordTestBot, err := discord.NewDiscordBot(botEngine, configs.DiscordTestBot, config.TargetMaskTest)
pCmd.ExitOnError(cmd, err)

err = discordTestBot.Start()
pCmd.ExitOnError(cmd, err)

sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sigChan

if err := discordTestBot.Stop(); err != nil {
pCmd.ExitOnError(cmd, err)
}

botEngine.Stop()
}
}
28 changes: 15 additions & 13 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ import (
)

type Config struct {
Network string `yaml:"network"`
NetworkNodes []string `yaml:"network_nodes"`
LocalNode string `yaml:"local_node"`
Database Database `yaml:"database"`
AuthIDs []string `yaml:"auth_ids"`
DiscordBot DiscordBot `yaml:"discord"`
GRPC GRPC `yaml:"grpc"`
Wallet Wallet `yaml:"main_net_wallet"`
TestNetWallet Wallet `yaml:"test_net_wallet"`
Logger Logger `yaml:"logger"`
HTTP HTTP `yaml:"http"`
Phoenix PhoenixNetwork `yaml:"phoenix"`
Telegram Telegram `yaml:"telegram"`
Network string `yaml:"network"`
NetworkNodes []string `yaml:"network_nodes"`
LocalNode string `yaml:"local_node"`
Database Database `yaml:"database"`
AuthIDs []string `yaml:"auth_ids"`
DiscordMainBot DiscordBot `yaml:"discord_main"`
DiscordTestBot DiscordBot `yaml:"discord_test"`
DiscordModerationBot DiscordBot `yaml:"discord_mod"`
GRPC GRPC `yaml:"grpc"`
Wallet Wallet `yaml:"main_net_wallet"`
TestNetWallet Wallet `yaml:"test_net_wallet"`
Logger Logger `yaml:"logger"`
HTTP HTTP `yaml:"http"`
Phoenix PhoenixNetwork `yaml:"phoenix"`
Telegram Telegram `yaml:"telegram"`
}

type Database struct {
Expand Down
12 changes: 10 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ func TestBasicCheck(t *testing.T) {
Password: "test_password",
},
NetworkNodes: []string{"http://127.0.0.1:8545"},
DiscordBot: DiscordBot{
DiscordMainBot: DiscordBot{
Token: "MTEabc123",
GuildID: "123456789",
},
DiscordTestBot: DiscordBot{
Token: "MTEabc123",
GuildID: "123456789",
},
Expand All @@ -45,7 +49,11 @@ func TestBasicCheck(t *testing.T) {
Password: "test_password",
},
NetworkNodes: []string{},
DiscordBot: DiscordBot{
DiscordMainBot: DiscordBot{
Token: "MTEabc123",
GuildID: "123456789",
},
DiscordTestBot: DiscordBot{
Token: "MTEabc123",
GuildID: "123456789",
},
Expand Down
10 changes: 8 additions & 2 deletions config/global.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package config

var (
TargetMaskMain = 1
TargetMaskTest = 2
TargetMaskModerator = 4

TargetMaskAll = TargetMaskMain | TargetMaskTest | TargetMaskModerator
)

const (
PriceCacheKey = "PriceCacheKey"
)

var HelpCommandTemplate string = `<table>{{range .}}<tr><td>{{ .Name }}</td><td>{{ .Desc }}</td></tr>{{end}}</table>`
21 changes: 0 additions & 21 deletions deployment/discord/Dockerfile

This file was deleted.

20 changes: 16 additions & 4 deletions deployment/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,31 @@ services:
- ../config/config.yml:/config/config.yml
- ../config/wallets/main_wallet:/config/wallets/main_wallet
- ../config/wallets/test_wallet:/config/wallets/test_wallet
pagu-discord:
pagu-discord-mainnet:
build:
context: ../.
dockerfile: deployment/discord/Dockerfile
hostname: pagu-discord
container_name: pagu-discord
dockerfile: deployment/mainnet/discord/Dockerfile
hostname: pagu-discord-mainnet
container_name: pagu-discord-mainnet
networks:
pagu-network:
depends_on:
- pagu-db
volumes:
- ../config/config.yml:/config/config.yml
- ../config/wallets/main_wallet:/config/wallets/main_wallet
pagu-discord-testnet:
build:
context: ../.
dockerfile: deployment/testnet/discord/Dockerfile
hostname: pagu-discord-testnet
container_name: pagu-discord-testnet
networks:
pagu-network:
depends_on:
- pagu-db
volumes:
- ../config/config.yml:/config/config.yml
- ../config/wallets/test_wallet:/config/wallets/test_wallet
pagu-node:
hostname: pagu-node
Expand Down
Loading

0 comments on commit e933222

Please sign in to comment.