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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"rockhu-bot/local/api"
|
"rockhu-bot/local/api"
|
||||||
|
cronhu "rockhu-bot/local/utl/cron"
|
||||||
"rockhu-bot/local/utl/db"
|
"rockhu-bot/local/utl/db"
|
||||||
discordrhu "rockhu-bot/local/utl/discord"
|
discordrhu "rockhu-bot/local/utl/discord"
|
||||||
"rockhu-bot/local/utl/server"
|
"rockhu-bot/local/utl/server"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
"github.com/robfig/cron/v3"
|
|
||||||
|
|
||||||
"go.uber.org/dig"
|
"go.uber.org/dig"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
godotenv.Load()
|
err := godotenv.Load()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("error loading .env file")
|
||||||
|
}
|
||||||
di := dig.New()
|
di := dig.New()
|
||||||
|
|
||||||
c := cron.New()
|
cronhu.Init(di)
|
||||||
di.Provide(func() *cron.Cron {
|
|
||||||
return c
|
|
||||||
})
|
|
||||||
|
|
||||||
discordrhu.Init(di)
|
discordrhu.Init(di)
|
||||||
db.Start(di)
|
db.Start(di)
|
||||||
server := server.Start(di)
|
server := server.Start(di)
|
||||||
|
|||||||
@@ -3,8 +3,11 @@ package job
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"rockhu-bot/local/model"
|
||||||
"rockhu-bot/local/service"
|
"rockhu-bot/local/service"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
"github.com/robfig/cron/v3"
|
"github.com/robfig/cron/v3"
|
||||||
@@ -30,7 +33,6 @@ func NewConcertJob(as *service.ConcertService, cron *cron.Cron, dsc *discordgo.S
|
|||||||
cron: cron,
|
cron: cron,
|
||||||
discord: dsc,
|
discord: dsc,
|
||||||
}
|
}
|
||||||
|
|
||||||
cron.AddFunc(os.Getenv("CONCERT_CRON"), ac.checkAndUpdate)
|
cron.AddFunc(os.Getenv("CONCERT_CRON"), ac.checkAndUpdate)
|
||||||
return ac
|
return ac
|
||||||
}
|
}
|
||||||
@@ -43,13 +45,44 @@ func NewConcertJob(as *service.ConcertService, cron *cron.Cron, dsc *discordgo.S
|
|||||||
// @Success 200 {array} string
|
// @Success 200 {array} string
|
||||||
// @Router /v1/Concert [get]
|
// @Router /v1/Concert [get]
|
||||||
func (ac *ConcertJob) checkAndUpdate() {
|
func (ac *ConcertJob) checkAndUpdate() {
|
||||||
fmt.Print("Started CheckAndUpdate")
|
fmt.Println("Started CheckAndUpdate")
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
newConcerts := ac.service.CheckAndUpdateConcerts(ctx)
|
newConcerts := ac.service.CheckAndUpdateConcerts(ctx)
|
||||||
fmt.Print("Finished CheckAndUpdate")
|
fmt.Println("Finished CheckAndUpdate")
|
||||||
|
|
||||||
if len(newConcerts) > 0 {
|
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
|
package job
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/robfig/cron/v3"
|
||||||
"go.uber.org/dig"
|
"go.uber.org/dig"
|
||||||
)
|
)
|
||||||
|
|
||||||
// InitializeJobs
|
// InitializeJobs
|
||||||
// Initializes Dependency Injection modules and registers controllers
|
// Initializes Dependency Injection modules and registers jobs
|
||||||
//
|
//
|
||||||
// Args:
|
// Args:
|
||||||
// *dig.Container: Dig Container
|
// *dig.Container: Dig Container
|
||||||
func InitializeJobs(c *dig.Container) {
|
func InitializeJobs(c *dig.Container) {
|
||||||
err := c.Invoke(NewConcertJob)
|
err := c.Invoke(NewConcertJob)
|
||||||
if err != nil {
|
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
|
return forInsert
|
||||||
}
|
}
|
||||||
|
|
||||||
func scrapeData(ctx context.Context) *[]model.ConcertModel {
|
func scrapeData(_ context.Context) *[]model.ConcertModel {
|
||||||
c := colly.NewCollector()
|
c := colly.NewCollector()
|
||||||
concerts := new([]model.ConcertModel)
|
concerts := new([]model.ConcertModel)
|
||||||
|
|
||||||
@@ -97,19 +97,16 @@ func scrapeData(ctx context.Context) *[]model.ConcertModel {
|
|||||||
|
|
||||||
func concertKey(concert model.ConcertModel) string {
|
func concertKey(concert model.ConcertModel) string {
|
||||||
cc := concert.Name + (time.Time)(concert.StartDate).Format("2006-01-02")
|
cc := concert.Name + (time.Time)(concert.StartDate).Format("2006-01-02")
|
||||||
fmt.Println(cc)
|
|
||||||
return cc
|
return cc
|
||||||
}
|
}
|
||||||
|
|
||||||
func partitionConcerts(previous, current []model.ConcertModel) (newConcerts, deletedConcerts, unchangedConcerts []model.ConcertModel) {
|
func partitionConcerts(previous, current []model.ConcertModel) (newConcerts, deletedConcerts, unchangedConcerts []model.ConcertModel) {
|
||||||
// Create a map of previous concerts for quick lookup
|
// Create a map of previous concerts for quick lookup
|
||||||
fmt.Println("Previous concerts\n")
|
|
||||||
previousMap := make(map[string]model.ConcertModel)
|
previousMap := make(map[string]model.ConcertModel)
|
||||||
for _, concert := range previous {
|
for _, concert := range previous {
|
||||||
previousMap[concertKey(concert)] = concert
|
previousMap[concertKey(concert)] = concert
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("New concerts\n")
|
|
||||||
// Create a map of current concerts for quick lookup
|
// Create a map of current concerts for quick lookup
|
||||||
currentMap := make(map[string]model.ConcertModel)
|
currentMap := make(map[string]model.ConcertModel)
|
||||||
for _, concert := range current {
|
for _, concert := range current {
|
||||||
@@ -122,7 +119,6 @@ func partitionConcerts(previous, current []model.ConcertModel) (newConcerts, del
|
|||||||
unchangedConcerts = append(unchangedConcerts, concert)
|
unchangedConcerts = append(unchangedConcerts, concert)
|
||||||
} else {
|
} else {
|
||||||
newConcerts = append(newConcerts, concert)
|
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 {
|
for _, concert := range previous {
|
||||||
if _, exists := currentMap[concertKey(concert)]; !exists {
|
if _, exists := currentMap[concertKey(concert)]; !exists {
|
||||||
deletedConcerts = append(deletedConcerts, concert)
|
deletedConcerts = append(deletedConcerts, concert)
|
||||||
fmt.Printf("Deleted concert %s\n", concert.Name)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ func GetMonthFromLocalized(month string) (int, error) {
|
|||||||
case "december":
|
case "december":
|
||||||
return 12, nil
|
return 12, nil
|
||||||
default:
|
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)
|
cmd := exec.Command("powershell", "-nologo", "-noprofile", "-File", "run_sc.ps1", command, service)
|
||||||
output, err := cmd.CombinedOutput()
|
output, err := cmd.CombinedOutput()
|
||||||
if err != nil {
|
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 "", fmt.Errorf("error: %v, output: %s", err, string(output))
|
||||||
}
|
}
|
||||||
return string(output), nil
|
return string(output), nil
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"rockhu-bot/local/model"
|
"rockhu-bot/local/model"
|
||||||
"time"
|
"time"
|
||||||
@@ -11,8 +12,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Start(di *dig.Container) {
|
func Start(di *dig.Container) {
|
||||||
|
dbConn := os.Getenv("DB_CONNECTION")
|
||||||
db, err := gorm.Open(postgres.New(postgres.Config{
|
db, err := gorm.Open(postgres.New(postgres.Config{
|
||||||
DSN: os.Getenv("CONNECTION_STRINGS"),
|
DSN: dbConn,
|
||||||
PreferSimpleProtocol: true,
|
PreferSimpleProtocol: true,
|
||||||
}), &gorm.Config{
|
}), &gorm.Config{
|
||||||
NowFunc: func() time.Time {
|
NowFunc: func() time.Time {
|
||||||
@@ -20,13 +22,13 @@ func Start(di *dig.Container) {
|
|||||||
return time.Now().In(utc)
|
return time.Now().In(utc)
|
||||||
}})
|
}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("failed to connect database")
|
log.Panic("failed to connect database")
|
||||||
}
|
}
|
||||||
err = di.Provide(func() *gorm.DB {
|
err = di.Provide(func() *gorm.DB {
|
||||||
return db
|
return db
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("failed to bind database")
|
log.Panic("failed to bind database")
|
||||||
}
|
}
|
||||||
Migrate(db)
|
Migrate(db)
|
||||||
}
|
}
|
||||||
@@ -34,6 +36,6 @@ func Start(di *dig.Container) {
|
|||||||
func Migrate(db *gorm.DB) {
|
func Migrate(db *gorm.DB) {
|
||||||
err := db.AutoMigrate(&model.ConcertModel{})
|
err := db.AutoMigrate(&model.ConcertModel{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("failed to migrate model.ApiModel")
|
log.Panic("failed to migrate model.ApiModel")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package discordrhu
|
package discordrhu
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
@@ -10,6 +11,9 @@ import (
|
|||||||
|
|
||||||
func Init(di *dig.Container) {
|
func Init(di *dig.Container) {
|
||||||
dsc, err := discordgo.New("Bot " + os.Getenv("DISCORD_KEY"))
|
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 {
|
if err != nil {
|
||||||
log.Panic("unable to start discord session!")
|
log.Panic("unable to start discord session!")
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"rockhu-bot/local/api"
|
|
||||||
"rockhu-bot/local/utl/common"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"rockhu-bot/local/api"
|
||||||
|
"rockhu-bot/local/utl/common"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/fiber/v2/middleware/cors"
|
"github.com/gofiber/fiber/v2/middleware/cors"
|
||||||
|
|||||||
Reference in New Issue
Block a user