add config
This commit is contained in:
14
Dockerfile
Normal file
14
Dockerfile
Normal file
@@ -0,0 +1,14 @@
|
||||
FROM golang:alpine
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
COPY . ./
|
||||
|
||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /api ./cmd/api/main.go
|
||||
|
||||
EXPOSE 4000
|
||||
|
||||
CMD ["/api"]
|
||||
@@ -1,26 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"rockhu-bot/local/api"
|
||||
cronhu "rockhu-bot/local/utl/cron"
|
||||
"rockhu-bot/local/utl/db"
|
||||
discordrhu "rockhu-bot/local/utl/discord"
|
||||
"rockhu-bot/local/utl/server"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/robfig/cron/v3"
|
||||
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
func main() {
|
||||
godotenv.Load()
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Fatal("error loading .env file")
|
||||
}
|
||||
di := dig.New()
|
||||
|
||||
c := cron.New()
|
||||
di.Provide(func() *cron.Cron {
|
||||
return c
|
||||
})
|
||||
|
||||
cronhu.Init(di)
|
||||
discordrhu.Init(di)
|
||||
db.Start(di)
|
||||
server := server.Start(di)
|
||||
|
||||
@@ -3,8 +3,11 @@ package job
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"rockhu-bot/local/model"
|
||||
"rockhu-bot/local/service"
|
||||
"time"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/robfig/cron/v3"
|
||||
@@ -30,7 +33,6 @@ func NewConcertJob(as *service.ConcertService, cron *cron.Cron, dsc *discordgo.S
|
||||
cron: cron,
|
||||
discord: dsc,
|
||||
}
|
||||
|
||||
cron.AddFunc(os.Getenv("CONCERT_CRON"), ac.checkAndUpdate)
|
||||
return ac
|
||||
}
|
||||
@@ -43,13 +45,44 @@ func NewConcertJob(as *service.ConcertService, cron *cron.Cron, dsc *discordgo.S
|
||||
// @Success 200 {array} string
|
||||
// @Router /v1/Concert [get]
|
||||
func (ac *ConcertJob) checkAndUpdate() {
|
||||
fmt.Print("Started CheckAndUpdate")
|
||||
fmt.Println("Started CheckAndUpdate")
|
||||
ctx := context.Background()
|
||||
newConcerts := ac.service.CheckAndUpdateConcerts(ctx)
|
||||
fmt.Print("Finished CheckAndUpdate")
|
||||
fmt.Println("Finished CheckAndUpdate")
|
||||
|
||||
if len(newConcerts) > 0 {
|
||||
ac.
|
||||
for _, concert := range newConcerts {
|
||||
sendMessageToAllChannels(ac.discord, concert)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func sendMessageToAllChannels(s *discordgo.Session, concert model.ConcertModel) {
|
||||
// Get all guilds (servers) the bot is part of
|
||||
guilds, err := s.UserGuilds(100, "", "", false)
|
||||
if err != nil {
|
||||
log.Printf("Error getting guilds: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Iterate over all guilds
|
||||
for _, guild := range guilds {
|
||||
// Get all channels in the guild
|
||||
channels, err := s.GuildChannels(guild.ID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting channels for guild %s: %v", guild.Name, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Iterate over all channels and send a message to text channels
|
||||
for _, channel := range channels {
|
||||
if channel.Type == discordgo.ChannelTypeGuildText {
|
||||
_, err := s.ChannelMessageSendEmbed(channel.ID, &discordgo.MessageEmbed{Title: "New concert", Description: concert.Name, Timestamp: (time.Time)(concert.StartDate).Format("2006-01-02")})
|
||||
if err != nil {
|
||||
log.Printf("Error sending message to channel %s: %v", channel.Name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,21 @@
|
||||
package job
|
||||
|
||||
import (
|
||||
"github.com/robfig/cron/v3"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
// InitializeJobs
|
||||
// Initializes Dependency Injection modules and registers controllers
|
||||
// Initializes Dependency Injection modules and registers jobs
|
||||
//
|
||||
// Args:
|
||||
// *dig.Container: Dig Container
|
||||
func InitializeJobs(c *dig.Container) {
|
||||
err := c.Invoke(NewConcertJob)
|
||||
if err != nil {
|
||||
panic("unable to initialize concert controller")
|
||||
panic("unable to initialize concert job")
|
||||
}
|
||||
c.Invoke(func(cr *cron.Cron) {
|
||||
cr.Start()
|
||||
})
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ func (as ConcertService) CheckAndUpdateConcerts(ctx context.Context) []model.Con
|
||||
return forInsert
|
||||
}
|
||||
|
||||
func scrapeData(ctx context.Context) *[]model.ConcertModel {
|
||||
func scrapeData(_ context.Context) *[]model.ConcertModel {
|
||||
c := colly.NewCollector()
|
||||
concerts := new([]model.ConcertModel)
|
||||
|
||||
@@ -97,19 +97,16 @@ func scrapeData(ctx context.Context) *[]model.ConcertModel {
|
||||
|
||||
func concertKey(concert model.ConcertModel) string {
|
||||
cc := concert.Name + (time.Time)(concert.StartDate).Format("2006-01-02")
|
||||
fmt.Println(cc)
|
||||
return cc
|
||||
}
|
||||
|
||||
func partitionConcerts(previous, current []model.ConcertModel) (newConcerts, deletedConcerts, unchangedConcerts []model.ConcertModel) {
|
||||
// Create a map of previous concerts for quick lookup
|
||||
fmt.Println("Previous concerts\n")
|
||||
previousMap := make(map[string]model.ConcertModel)
|
||||
for _, concert := range previous {
|
||||
previousMap[concertKey(concert)] = concert
|
||||
}
|
||||
|
||||
fmt.Println("New concerts\n")
|
||||
// Create a map of current concerts for quick lookup
|
||||
currentMap := make(map[string]model.ConcertModel)
|
||||
for _, concert := range current {
|
||||
@@ -122,7 +119,6 @@ func partitionConcerts(previous, current []model.ConcertModel) (newConcerts, del
|
||||
unchangedConcerts = append(unchangedConcerts, concert)
|
||||
} else {
|
||||
newConcerts = append(newConcerts, concert)
|
||||
fmt.Printf("New concert %s\n", concert.Name)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,7 +126,6 @@ func partitionConcerts(previous, current []model.ConcertModel) (newConcerts, del
|
||||
for _, concert := range previous {
|
||||
if _, exists := currentMap[concertKey(concert)]; !exists {
|
||||
deletedConcerts = append(deletedConcerts, concert)
|
||||
fmt.Printf("Deleted concert %s\n", concert.Name)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ func GetMonthFromLocalized(month string) (int, error) {
|
||||
case "december":
|
||||
return 12, nil
|
||||
default:
|
||||
return 0, fmt.Errorf("Month not recognized %1", month)
|
||||
return 0, fmt.Errorf("month not recognized %s", month)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,7 +92,7 @@ func RunElevatedCommand(command string, service string) (string, error) {
|
||||
cmd := exec.Command("powershell", "-nologo", "-noprofile", "-File", "run_sc.ps1", command, service)
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Panic("error: %v, output: %s", err, string(output))
|
||||
log.Panicf("error: %v, output: %s", err, string(output))
|
||||
return "", fmt.Errorf("error: %v, output: %s", err, string(output))
|
||||
}
|
||||
return string(output), nil
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"rockhu-bot/local/model"
|
||||
"time"
|
||||
@@ -11,8 +12,9 @@ import (
|
||||
)
|
||||
|
||||
func Start(di *dig.Container) {
|
||||
dbConn := os.Getenv("DB_CONNECTION")
|
||||
db, err := gorm.Open(postgres.New(postgres.Config{
|
||||
DSN: os.Getenv("CONNECTION_STRINGS"),
|
||||
DSN: dbConn,
|
||||
PreferSimpleProtocol: true,
|
||||
}), &gorm.Config{
|
||||
NowFunc: func() time.Time {
|
||||
@@ -20,13 +22,13 @@ func Start(di *dig.Container) {
|
||||
return time.Now().In(utc)
|
||||
}})
|
||||
if err != nil {
|
||||
panic("failed to connect database")
|
||||
log.Panic("failed to connect database")
|
||||
}
|
||||
err = di.Provide(func() *gorm.DB {
|
||||
return db
|
||||
})
|
||||
if err != nil {
|
||||
panic("failed to bind database")
|
||||
log.Panic("failed to bind database")
|
||||
}
|
||||
Migrate(db)
|
||||
}
|
||||
@@ -34,6 +36,6 @@ func Start(di *dig.Container) {
|
||||
func Migrate(db *gorm.DB) {
|
||||
err := db.AutoMigrate(&model.ConcertModel{})
|
||||
if err != nil {
|
||||
panic("failed to migrate model.ApiModel")
|
||||
log.Panic("failed to migrate model.ApiModel")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package discordrhu
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
@@ -10,6 +11,9 @@ import (
|
||||
|
||||
func Init(di *dig.Container) {
|
||||
dsc, err := discordgo.New("Bot " + os.Getenv("DISCORD_KEY"))
|
||||
dsc.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
|
||||
fmt.Println("Bot is ready")
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Panic("unable to start discord session!")
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"rockhu-bot/local/api"
|
||||
"rockhu-bot/local/utl/common"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"rockhu-bot/local/api"
|
||||
"rockhu-bot/local/utl/common"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/cors"
|
||||
|
||||
Reference in New Issue
Block a user