Refactor app context setup and move API fetch logic to PostService
This commit is contained in:
@@ -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
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user