74 lines
1.9 KiB
Go
74 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"CatsOfMastodonBotGo/internal/config"
|
|
"CatsOfMastodonBotGo/internal/database"
|
|
"CatsOfMastodonBotGo/internal/domain"
|
|
"CatsOfMastodonBotGo/internal/server"
|
|
"CatsOfMastodonBotGo/internal/services"
|
|
"context"
|
|
"log/slog"
|
|
"time"
|
|
)
|
|
|
|
func main() {
|
|
// Setup config
|
|
config.Init()
|
|
|
|
|
|
// Initialize database
|
|
database.Init()
|
|
|
|
services.InitPostService()
|
|
|
|
ticker := time.NewTicker(10 * time.Minute)
|
|
|
|
runFetchPosts := func() {
|
|
// Get posts
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
var posts []domain.Post = nil
|
|
err, posts := services.PostServiceInstance.GetPostsFromApi(ctx, config.Config.Tag, config.Config.Instance)
|
|
if err != nil {
|
|
slog.Error(err.Error())
|
|
return
|
|
}
|
|
|
|
var existingPostIds = services.PostServiceInstance.GetExistingPostIds()
|
|
var existingAccountIds = services.PostServiceInstance.GetExistingAccountIds()
|
|
var newPosts = services.PostServiceInstance.GetNewPosts(existingPostIds, posts)
|
|
var newAccounts = services.PostServiceInstance.GetNewAccounts(existingAccountIds, newPosts)
|
|
|
|
// Save to database
|
|
slog.Info("Fetched %d posts; %d existing posts; %d new posts and %d new accounts\n", len(posts), len(existingPostIds), len(newPosts), len(newAccounts))
|
|
|
|
// Additional logging
|
|
if newAccounts != nil {
|
|
slog.Info("Inserted %d accounts\n", services.PostServiceInstance.InsertNewAccounts(newAccounts))
|
|
}
|
|
if newPosts != nil {
|
|
slog.Info("Inserted %d posts\n", services.PostServiceInstance.InsertNewPosts(newPosts))
|
|
}
|
|
}
|
|
|
|
go func() {
|
|
for range ticker.C {
|
|
runFetchPosts()
|
|
}
|
|
}()
|
|
|
|
// Run initial fetch on startup
|
|
go func() {
|
|
runFetchPosts()
|
|
}()
|
|
|
|
// https://seefnasrul.medium.com/create-your-first-go-rest-api-with-jwt-authentication-in-gin-framework-dbe5bda72817
|
|
|
|
r := server.SetupRouter()
|
|
err := r.Run(":8080")
|
|
if err != nil {
|
|
slog.Error(err.Error())
|
|
}
|
|
|
|
}
|