44 lines
839 B
Go
44 lines
839 B
Go
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
|
|
|
|
} |