Refactor app context setup and move API fetch logic to PostService

This commit is contained in:
2025-05-13 17:32:29 +03:30
parent 99e385889a
commit 117fe3dd34
5 changed files with 97 additions and 76 deletions
+44
View File
@@ -0,0 +1,44 @@
package helpers
import (
"CatsOfMastodonBotGo/internal"
"CatsOfMastodonBotGo/internal/services"
"os"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
func SetupAppContext() *internal.AppContext {
// Setup AppContext
instance := os.Getenv("INSTANCE")
if instance == "" {
instance = "https://haminoa.net"
}
tag := os.Getenv("TAG")
if tag == "" {
tag = "catsofmastodon"
}
// Setup database
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{Logger: logger.Default.LogMode(logger.Warn)})
if err != nil {
panic("failed to connect database")
}
AddMigrations(db)
//Setup PostService
var postService = services.NewPostService(db)
// Inititlize AppContext
var appContext = &internal.AppContext{
Db: db,
PostService: postService,
Instance: instance,
Tag: tag,
}
return appContext
}