package helpers import ( "CatsOfMastodonBotGo/internal" "CatsOfMastodonBotGo/internal/auth" "CatsOfMastodonBotGo/internal/services" "log" "os" "gorm.io/driver/sqlite" "gorm.io/gorm" "gorm.io/gorm/logger" ) func SetupAppContext() *internal.AppContext { // Setup AppContext instance := os.Getenv("CAOM_INSTANCE") if instance == "" { instance = "https://mstdn.party" } tag := os.Getenv("CAOM_TAG") if tag == "" { tag = "catsofmastodon" } adminPassword := os.Getenv("CAOM_ADMIN_PASSWORD") if adminPassword == "" { log.Println("No admin password provided, using default password 'catsaregood'") adminPassword = "catsaregood" } // Jwt params secret := os.Getenv("CAOM_JWT_SECRET") if secret == "" { log.Fatal("No jwt secret provided, using default secret 'secret'") } issuer := os.Getenv("CAOM_JWT_ISSUER") if issuer == "" { log.Println("No jwt issuer provided, using default issuer 'CatsOfMastodonBotGo'") issuer = "CatsOfMastodonBotGo" } audience := os.Getenv("CAOM_JWT_AUDIENCE") if audience == "" { log.Println("No jwt audience provided, using default audience 'CatsOfMastodonBotGo'") audience = "CatsOfMastodonBotGo" } // 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) // Setup Jwt var jwt = auth.NewJwtTokenGenerator(secret, issuer, audience) // Inititlize AppContext var appContext = &internal.AppContext{ Db: db, PostService: postService, Jwt: jwt, AdminPassword: adminPassword, Instance: instance, Tag: tag, } return appContext }