Refactor app structure: move models to domain, centralize config and database init - TODO: add jwt

This commit is contained in:
2025-05-17 20:07:29 +03:30
parent ab9254fcad
commit 7b601e75ba
12 changed files with 142 additions and 181 deletions

View File

@@ -1,18 +1,26 @@
package main
import (
"CatsOfMastodonBotGo/internal/helpers"
"CatsOfMastodonBotGo/internal/models"
"CatsOfMastodonBotGo/internal/config"
"CatsOfMastodonBotGo/internal/database"
"CatsOfMastodonBotGo/internal/domain"
"CatsOfMastodonBotGo/internal/server"
"CatsOfMastodonBotGo/internal/services"
"context"
"log"
"time"
)
func main() {
// Setup AppContext
var appContext = helpers.SetupAppContext()
// Setup config
config.Init()
var config = config.Load()
// Initialize database
database.Init()
services.InitPostService()
ticker := time.NewTicker(10 * time.Minute)
@@ -20,27 +28,27 @@ func main() {
// Get posts
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
var posts []models.Post = nil
err, posts := appContext.PostService.GetPostsFromApi(ctx, appContext.Tag, appContext.Instance)
var posts []domain.Post = nil
err, posts := services.PostServiceInstance.GetPostsFromApi(ctx, config.Tag, config.Instance)
if err != nil {
log.Println(err)
return
}
var existingPostIds = appContext.PostService.GetExistingPostIds()
var existingAccountIds = appContext.PostService.GetExistingAccountIds()
var newPosts = appContext.PostService.GetNewPosts(existingPostIds, posts)
var newAccounts = appContext.PostService.GetNewAccounts(existingAccountIds, newPosts)
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
log.Printf("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 {
log.Printf("Inserted %d accounts\n", appContext.PostService.InsertNewAccounts(newAccounts))
log.Printf("Inserted %d accounts\n", services.PostServiceInstance.InsertNewAccounts(newAccounts))
}
if newPosts != nil {
log.Printf("Inserted %d posts\n", appContext.PostService.InsertNewPosts(newPosts))
log.Printf("Inserted %d posts\n", services.PostServiceInstance.InsertNewPosts(newPosts))
}
}
@@ -57,7 +65,7 @@ func main() {
// https://seefnasrul.medium.com/create-your-first-go-rest-api-with-jwt-authentication-in-gin-framework-dbe5bda72817
r := server.SetupRouter(appContext)
r := server.SetupRouter()
err := r.Run(":8080")
if err != nil {
log.Fatal(err)