Files
CatsOfMastodonGo/internal/config/config.go

86 lines
1.8 KiB
Go

package config
import (
//"CatsOfMastodonBotGo/internal/auth"
"CatsOfMastodonBotGo/internal/database"
//"CatsOfMastodonBotGo/internal/services"
"log"
"os"
"gorm.io/gorm"
)
type config struct {
Db *gorm.DB
// PostService *services.PostService
// Jwt *auth.JwtTokenGenerator
AdminPassword string
Instance string
Tag string
}
var Config *config
func Load() *config {
// Get mastodon instance
instance := os.Getenv("CAOM_INSTANCE")
if instance == "" {
instance = "https://mstdn.party"
}
// Get mastodon tag
tag := os.Getenv("CAOM_TAG")
if tag == "" {
tag = "catsofmastodon"
}
// Get admin password (Its a single user/admin app so its just fine)
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")
}
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 := database.Connect()
if err != nil {
log.Fatal(err)
}
//Setup PostService
// var postService = services.NewPostService(db)
// // Setup Jwt
// var jwt = auth.NewJwtTokenGenerator(secret, issuer, audience)
// Inititlize AppContext
var appContext = &config{
Db: db,
// PostService: postService,
// Jwt: jwt,
AdminPassword: adminPassword,
Instance: instance,
Tag: tag,
}
return appContext
}
func Init() {
Config = Load()
}