init
This commit is contained in:
44
local/api/api.go
Normal file
44
local/api/api.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"os"
|
||||
"rockhu-bot/local/controller"
|
||||
"rockhu-bot/local/job"
|
||||
"rockhu-bot/local/utl/common"
|
||||
"rockhu-bot/local/utl/configs"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/basicauth"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
// Routes
|
||||
// Initializes web api controllers and its corresponding routes.
|
||||
//
|
||||
// Args:
|
||||
// *fiber.App: Fiber Application
|
||||
func Init(di *dig.Container, app *fiber.App) {
|
||||
groups := app.Group(configs.Prefix)
|
||||
|
||||
basicAuthConfig := basicauth.New(basicauth.Config{
|
||||
Users: map[string]string{
|
||||
"admin": os.Getenv("PASSWORD"),
|
||||
},
|
||||
})
|
||||
|
||||
routeGroups := &common.RouteGroups{
|
||||
Concert: groups.Group("/concert"),
|
||||
}
|
||||
|
||||
routeGroups.Concert.Use(basicAuthConfig)
|
||||
|
||||
err := di.Provide(func() *common.RouteGroups {
|
||||
return routeGroups
|
||||
})
|
||||
if err != nil {
|
||||
panic("unable to bind routes")
|
||||
}
|
||||
|
||||
controller.InitializeControllers(di)
|
||||
job.InitializeJobs(di)
|
||||
}
|
||||
51
local/controller/concert.go
Normal file
51
local/controller/concert.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"rockhu-bot/local/service"
|
||||
"rockhu-bot/local/utl/common"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type ConcertController struct {
|
||||
service *service.ConcertService
|
||||
}
|
||||
|
||||
// NewConcertController
|
||||
// Initializes ConcertController.
|
||||
//
|
||||
// Args:
|
||||
// *services.ConcertService: Concert service
|
||||
// *Fiber.RouterGroup: Fiber Router Group
|
||||
// Returns:
|
||||
// *ConcertController: Controller for "Concert" interactions
|
||||
func NewConcertController(as *service.ConcertService, routeGroups *common.RouteGroups) *ConcertController {
|
||||
ac := &ConcertController{
|
||||
service: as,
|
||||
}
|
||||
|
||||
routeGroups.Concert.Get("/", ac.getAll)
|
||||
|
||||
return ac
|
||||
}
|
||||
|
||||
// getAll returns Concert
|
||||
//
|
||||
// @Summary Return Concert
|
||||
// @Description Return Concert
|
||||
// @Tags Concert
|
||||
// @Success 200 {array} string
|
||||
// @Router /v1/Concert [get]
|
||||
func (ac *ConcertController) getAll(c *fiber.Ctx) error {
|
||||
ConcertModel := ac.service.GetAll(c.UserContext())
|
||||
model, err := json.Marshal(ConcertModel)
|
||||
if err != nil {
|
||||
return c.SendStatus(400)
|
||||
}
|
||||
return c.Send(model)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
Name string `json:"name" xml:"name" form:"name"`
|
||||
}
|
||||
21
local/controller/controller.go
Normal file
21
local/controller/controller.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"rockhu-bot/local/service"
|
||||
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
// InitializeControllers
|
||||
// Initializes Dependency Injection modules and registers controllers
|
||||
//
|
||||
// Args:
|
||||
// *dig.Container: Dig Container
|
||||
func InitializeControllers(c *dig.Container) {
|
||||
service.InitializeServices(c)
|
||||
|
||||
err := c.Invoke(NewConcertController)
|
||||
if err != nil {
|
||||
panic("unable to initialize Concert controller")
|
||||
}
|
||||
}
|
||||
55
local/job/concert.go
Normal file
55
local/job/concert.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package job
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"rockhu-bot/local/service"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/robfig/cron/v3"
|
||||
)
|
||||
|
||||
type ConcertJob struct {
|
||||
service *service.ConcertService
|
||||
cron *cron.Cron
|
||||
discord *discordgo.Session
|
||||
}
|
||||
|
||||
// NewConcertJob
|
||||
// Initializes ApiController.
|
||||
//
|
||||
// Args:
|
||||
// *services.ConcertService: Concert service
|
||||
// *Fiber.RouterGroup: Fiber Router Group
|
||||
// Returns:
|
||||
// *ConcertJob: Controller for "api" interactions
|
||||
func NewConcertJob(as *service.ConcertService, cron *cron.Cron, dsc *discordgo.Session) *ConcertJob {
|
||||
ac := &ConcertJob{
|
||||
service: as,
|
||||
cron: cron,
|
||||
discord: dsc,
|
||||
}
|
||||
|
||||
cron.AddFunc(os.Getenv("CONCERT_CRON"), ac.checkAndUpdate)
|
||||
return ac
|
||||
}
|
||||
|
||||
// checkAndUpdate returns Concert
|
||||
//
|
||||
// @Summary Return Concert
|
||||
// @Description Return Concert
|
||||
// @Tags Concert
|
||||
// @Success 200 {array} string
|
||||
// @Router /v1/Concert [get]
|
||||
func (ac *ConcertJob) checkAndUpdate() {
|
||||
fmt.Print("Started CheckAndUpdate")
|
||||
ctx := context.Background()
|
||||
newConcerts := ac.service.CheckAndUpdateConcerts(ctx)
|
||||
fmt.Print("Finished CheckAndUpdate")
|
||||
|
||||
if len(newConcerts) > 0 {
|
||||
ac.
|
||||
}
|
||||
|
||||
}
|
||||
17
local/job/job.go
Normal file
17
local/job/job.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package job
|
||||
|
||||
import (
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
// InitializeJobs
|
||||
// Initializes Dependency Injection modules and registers controllers
|
||||
//
|
||||
// Args:
|
||||
// *dig.Container: Dig Container
|
||||
func InitializeJobs(c *dig.Container) {
|
||||
err := c.Invoke(NewConcertJob)
|
||||
if err != nil {
|
||||
panic("unable to initialize concert controller")
|
||||
}
|
||||
}
|
||||
12
local/model/concert.go
Normal file
12
local/model/concert.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ConcertModel struct {
|
||||
gorm.Model
|
||||
Name string `json:"name"`
|
||||
StartDate datatypes.Date `json:"startDate"`
|
||||
}
|
||||
36
local/repository/concert.go
Normal file
36
local/repository/concert.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"rockhu-bot/local/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ConcertRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewConcertRepository(db *gorm.DB) *ConcertRepository {
|
||||
return &ConcertRepository{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
// GetAll
|
||||
// Gets all rows from Concert table.
|
||||
//
|
||||
// Args:
|
||||
// context.Context: Application context
|
||||
// Returns:
|
||||
// model.ConcertModel: Concert object from database.
|
||||
func (as ConcertRepository) GetAll(ctx context.Context) *[]model.ConcertModel {
|
||||
db := as.db.WithContext(ctx)
|
||||
ConcertModel := new([]model.ConcertModel)
|
||||
db.Find(&ConcertModel)
|
||||
return ConcertModel
|
||||
}
|
||||
|
||||
func (as ConcertRepository) CreateTransaction() *gorm.DB {
|
||||
return as.db.Begin()
|
||||
}
|
||||
14
local/repository/repository.go
Normal file
14
local/repository/repository.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
// InitializeRepositories
|
||||
// Initializes Dependency Injection modules for repositories
|
||||
//
|
||||
// Args:
|
||||
// *dig.Container: Dig Container
|
||||
func InitializeRepositories(c *dig.Container) {
|
||||
c.Provide(NewConcertRepository)
|
||||
}
|
||||
138
local/service/concert.go
Normal file
138
local/service/concert.go
Normal file
@@ -0,0 +1,138 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"rockhu-bot/local/model"
|
||||
"rockhu-bot/local/repository"
|
||||
"rockhu-bot/local/utl/common"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gocolly/colly"
|
||||
"gorm.io/datatypes"
|
||||
)
|
||||
|
||||
type ConcertService struct {
|
||||
repository *repository.ConcertRepository
|
||||
}
|
||||
|
||||
func NewConcertService(repository *repository.ConcertRepository) *ConcertService {
|
||||
return &ConcertService{
|
||||
repository: repository,
|
||||
}
|
||||
}
|
||||
|
||||
// GetFirst
|
||||
// Gets first row from Concert table.
|
||||
//
|
||||
// Args:
|
||||
// context.Context: Application context
|
||||
// Returns:
|
||||
// string: Application version
|
||||
func (as ConcertService) GetAll(ctx context.Context) *[]model.ConcertModel {
|
||||
return as.repository.GetAll(ctx)
|
||||
}
|
||||
|
||||
func (as ConcertService) CheckAndUpdateConcerts(ctx context.Context) []model.ConcertModel {
|
||||
cm := as.GetAll(ctx)
|
||||
fmt.Printf("Currently there are %d concerts\n", len(*cm))
|
||||
NewConcerts := scrapeData(ctx)
|
||||
fmt.Printf("There are %d new concerts\n", len(*NewConcerts))
|
||||
forInsert, forDelete, _ := partitionConcerts(*cm, *NewConcerts)
|
||||
|
||||
tx := as.repository.CreateTransaction()
|
||||
defer tx.Rollback()
|
||||
|
||||
if len(forDelete) > 0 {
|
||||
tx.Delete(forDelete)
|
||||
}
|
||||
if len(forInsert) > 0 {
|
||||
tx.Create(forInsert)
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return forInsert
|
||||
}
|
||||
|
||||
func scrapeData(ctx context.Context) *[]model.ConcertModel {
|
||||
c := colly.NewCollector()
|
||||
concerts := new([]model.ConcertModel)
|
||||
|
||||
// Find and visit all links
|
||||
c.OnHTML(".event_box", func(e *colly.HTMLElement) {
|
||||
currentDate := time.Now()
|
||||
currentMonth := currentDate.Month()
|
||||
month, err := common.GetMonthFromLocalized(e.ChildText(".event_date>.month"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
dayText := e.ChildText(".event_date>.day")
|
||||
day, err := strconv.Atoi(dayText)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
year := currentDate.Year()
|
||||
if month < int(currentMonth) {
|
||||
year = year + 1
|
||||
}
|
||||
|
||||
eventDate := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
|
||||
concert := new(model.ConcertModel)
|
||||
concert.Name = e.ChildText(".event_title")
|
||||
concert.StartDate = datatypes.Date(eventDate)
|
||||
*concerts = append(*concerts, *concert)
|
||||
})
|
||||
|
||||
c.OnRequest(func(r *colly.Request) {
|
||||
fmt.Println("Visiting", r.URL)
|
||||
})
|
||||
|
||||
c.Visit("https://jegy.rock1.hu/")
|
||||
|
||||
return concerts
|
||||
}
|
||||
|
||||
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 {
|
||||
currentMap[concertKey(concert)] = concert
|
||||
}
|
||||
|
||||
// Identify new and unchanged concerts
|
||||
for _, concert := range current {
|
||||
if _, exists := previousMap[concertKey(concert)]; exists {
|
||||
unchangedConcerts = append(unchangedConcerts, concert)
|
||||
} else {
|
||||
newConcerts = append(newConcerts, concert)
|
||||
fmt.Printf("New concert %s\n", concert.Name)
|
||||
}
|
||||
}
|
||||
|
||||
// Identify deleted concerts
|
||||
for _, concert := range previous {
|
||||
if _, exists := currentMap[concertKey(concert)]; !exists {
|
||||
deletedConcerts = append(deletedConcerts, concert)
|
||||
fmt.Printf("Deleted concert %s\n", concert.Name)
|
||||
}
|
||||
}
|
||||
|
||||
return newConcerts, deletedConcerts, unchangedConcerts
|
||||
}
|
||||
18
local/service/service.go
Normal file
18
local/service/service.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"rockhu-bot/local/repository"
|
||||
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
// InitializeServices
|
||||
// Initializes Dependency Injection modules for services
|
||||
//
|
||||
// Args:
|
||||
// *dig.Container: Dig Container
|
||||
func InitializeServices(c *dig.Container) {
|
||||
repository.InitializeRepositories(c)
|
||||
|
||||
c.Provide(NewConcertService)
|
||||
}
|
||||
99
local/utl/common/common.go
Normal file
99
local/utl/common/common.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type RouteGroups struct {
|
||||
Concert fiber.Router
|
||||
}
|
||||
|
||||
func CheckError(err error) {
|
||||
if err != nil {
|
||||
log.Printf("Error occured. %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
|
||||
var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
|
||||
|
||||
func GetMonthFromLocalized(month string) (int, error) {
|
||||
switch month {
|
||||
case "január":
|
||||
return 1, nil
|
||||
case "február":
|
||||
return 2, nil
|
||||
case "március":
|
||||
return 3, nil
|
||||
case "április":
|
||||
return 4, nil
|
||||
case "május":
|
||||
return 5, nil
|
||||
case "június":
|
||||
return 6, nil
|
||||
case "július":
|
||||
return 7, nil
|
||||
case "augusztus":
|
||||
return 8, nil
|
||||
case "szeptember":
|
||||
return 9, nil
|
||||
case "október":
|
||||
return 10, nil
|
||||
case "november":
|
||||
return 11, nil
|
||||
case "december":
|
||||
return 12, nil
|
||||
default:
|
||||
return 0, fmt.Errorf("Month not recognized %1", month)
|
||||
}
|
||||
}
|
||||
|
||||
func ToSnakeCase(str string) string {
|
||||
snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
|
||||
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
|
||||
return strings.ToLower(snake)
|
||||
}
|
||||
|
||||
func GetIP() string {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
os.Stderr.WriteString("Oops: " + err.Error() + "\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
for _, a := range addrs {
|
||||
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||
if ipnet.IP.To4() != nil {
|
||||
return ipnet.IP.String()
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func Find[T any](lst *[]T, callback func(item *T) bool) *T {
|
||||
for _, item := range *lst {
|
||||
if callback(&item) {
|
||||
return &item
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
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))
|
||||
return "", fmt.Errorf("error: %v, output: %s", err, string(output))
|
||||
}
|
||||
return string(output), nil
|
||||
}
|
||||
8
local/utl/configs/configs.go
Normal file
8
local/utl/configs/configs.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package configs
|
||||
|
||||
const (
|
||||
Version = "0.0.1"
|
||||
Prefix = "v1"
|
||||
Secret = "Donde4sta"
|
||||
SecretCode = "brasno"
|
||||
)
|
||||
22
local/utl/cron/cron.go
Normal file
22
local/utl/cron/cron.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package cronhu
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/robfig/cron/v3"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
func Init(di *dig.Container) *cron.Cron {
|
||||
|
||||
c := cron.New()
|
||||
err := di.Provide(func() *cron.Cron {
|
||||
return c
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Panic("unable to initialize cron!")
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
39
local/utl/db/db.go
Normal file
39
local/utl/db/db.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"os"
|
||||
"rockhu-bot/local/model"
|
||||
"time"
|
||||
|
||||
"go.uber.org/dig"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func Start(di *dig.Container) {
|
||||
db, err := gorm.Open(postgres.New(postgres.Config{
|
||||
DSN: os.Getenv("CONNECTION_STRINGS"),
|
||||
PreferSimpleProtocol: true,
|
||||
}), &gorm.Config{
|
||||
NowFunc: func() time.Time {
|
||||
utc, _ := time.LoadLocation("")
|
||||
return time.Now().In(utc)
|
||||
}})
|
||||
if err != nil {
|
||||
panic("failed to connect database")
|
||||
}
|
||||
err = di.Provide(func() *gorm.DB {
|
||||
return db
|
||||
})
|
||||
if err != nil {
|
||||
panic("failed to bind database")
|
||||
}
|
||||
Migrate(db)
|
||||
}
|
||||
|
||||
func Migrate(db *gorm.DB) {
|
||||
err := db.AutoMigrate(&model.ConcertModel{})
|
||||
if err != nil {
|
||||
panic("failed to migrate model.ApiModel")
|
||||
}
|
||||
}
|
||||
26
local/utl/discord/discord.go
Normal file
26
local/utl/discord/discord.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package discordrhu
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
func Init(di *dig.Container) {
|
||||
dsc, err := discordgo.New("Bot " + os.Getenv("DISCORD_KEY"))
|
||||
|
||||
if err != nil {
|
||||
log.Panic("unable to start discord session!")
|
||||
}
|
||||
|
||||
err = di.Provide(func() *discordgo.Session {
|
||||
return dsc
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Panic("unable to provide discord session!")
|
||||
}
|
||||
|
||||
}
|
||||
43
local/utl/server/server.go
Normal file
43
local/utl/server/server.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"rockhu-bot/local/api"
|
||||
"rockhu-bot/local/utl/common"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/cors"
|
||||
"github.com/gofiber/swagger"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
func Start(di *dig.Container) *fiber.App {
|
||||
app := fiber.New(fiber.Config{
|
||||
EnablePrintRoutes: true,
|
||||
})
|
||||
|
||||
app.Use(cors.New())
|
||||
|
||||
app.Get("/swagger/*", swagger.HandlerDefault)
|
||||
|
||||
file, err := os.OpenFile("logs.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
|
||||
if err != nil {
|
||||
log.Print("Cannot open file logs.log")
|
||||
}
|
||||
log.SetOutput(file)
|
||||
|
||||
api.Init(di, app)
|
||||
|
||||
app.Get("/ping", func(c *fiber.Ctx) error {
|
||||
return c.SendString("pong")
|
||||
})
|
||||
port := os.Getenv("PORT")
|
||||
err = app.Listen(":" + port)
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("Running on %s:%s", common.GetIP(), port)
|
||||
println(msg)
|
||||
}
|
||||
return app
|
||||
}
|
||||
Reference in New Issue
Block a user