refactor: migrate env-var reads to config package
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"mal/integrations/playback/allanime"
|
||||
"mal/internal/anime"
|
||||
"mal/internal/auth"
|
||||
"mal/internal/config"
|
||||
"mal/internal/database"
|
||||
"mal/internal/episodes"
|
||||
"mal/internal/playback"
|
||||
@@ -19,6 +20,7 @@ import (
|
||||
|
||||
func NewApp() *fx.App {
|
||||
return fx.New(
|
||||
config.Module,
|
||||
database.Module,
|
||||
jikan.Module,
|
||||
allanime.Module,
|
||||
|
||||
@@ -3,7 +3,6 @@ package db
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
// sqlite3 driver.
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
@@ -17,11 +16,3 @@ func Open(dbFile string) (*sql.DB, error) {
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// GetDBFile returns the database file path, checking DATABASE_FILE env var first
|
||||
func GetDBFile() string {
|
||||
if f := os.Getenv("DATABASE_FILE"); f != "" {
|
||||
return f
|
||||
}
|
||||
return "mal.db"
|
||||
}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package episodes
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"mal/integrations/jikan"
|
||||
"mal/integrations/playback/allanime"
|
||||
"mal/internal/config"
|
||||
"mal/internal/db"
|
||||
"mal/internal/domain"
|
||||
episodeService "mal/internal/episodes/service"
|
||||
@@ -14,9 +12,8 @@ import (
|
||||
"go.uber.org/fx"
|
||||
)
|
||||
|
||||
func episodeAvailabilityEnabled() bool {
|
||||
value := strings.ToLower(strings.TrimSpace(os.Getenv("EPISODE_AVAILABILITY_MODE")))
|
||||
return value != "legacy" && value != "jikan"
|
||||
func episodeAvailabilityEnabled(cfg config.Config) bool {
|
||||
return cfg.EpisodeAvailabilityMode != config.EpisodeAvailabilityModeLegacy && cfg.EpisodeAvailabilityMode != config.EpisodeAvailabilityModeJikan
|
||||
}
|
||||
|
||||
var Module = fx.Options(
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package playback
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"mal/integrations/jikan"
|
||||
"mal/integrations/playback/allanime"
|
||||
"mal/internal/config"
|
||||
"mal/internal/domain"
|
||||
"mal/internal/playback/handler"
|
||||
"mal/internal/playback/repository"
|
||||
@@ -14,8 +13,8 @@ import (
|
||||
"go.uber.org/fx"
|
||||
)
|
||||
|
||||
func provideProxyTokenKey() string {
|
||||
return os.Getenv("PLAYBACK_PROXY_SECRET")
|
||||
func provideProxyTokenKey(cfg config.Config) string {
|
||||
return cfg.PlaybackProxySecret
|
||||
}
|
||||
|
||||
var Module = fx.Options(
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"mal/internal/config"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func CORSMiddleware() gin.HandlerFunc {
|
||||
allowAll := os.Getenv("MAL_CORS_ALLOW_ALL") == "1"
|
||||
return CORSMiddlewareWithConfig(config.Config{})
|
||||
}
|
||||
|
||||
func CORSMiddlewareWithConfig(cfg config.Config) gin.HandlerFunc {
|
||||
allowAll := cfg.CORSAllowAll
|
||||
return func(c *gin.Context) {
|
||||
origin := c.GetHeader("Origin")
|
||||
if origin != "" && (allowAll || isAllowedOrigin(origin)) {
|
||||
|
||||
@@ -3,9 +3,9 @@ package server
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"mal/internal/config"
|
||||
"mal/internal/observability"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -19,12 +19,14 @@ var Module = fx.Options(
|
||||
fx.Invoke(RunServer),
|
||||
)
|
||||
|
||||
func ProvideRouter(htmlRender render.HTMLRender, metrics *observability.Metrics) *gin.Engine {
|
||||
if os.Getenv("GIN_MODE") == "" {
|
||||
func ProvideRouter(cfg config.Config, htmlRender render.HTMLRender, metrics *observability.Metrics) *gin.Engine {
|
||||
if cfg.GinMode == "" {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
} else {
|
||||
gin.SetMode(cfg.GinMode)
|
||||
}
|
||||
r := gin.New()
|
||||
r.Use(CORSMiddleware(), RequestLogger(metrics), gin.Recovery())
|
||||
r.Use(CORSMiddlewareWithConfig(cfg), RequestLogger(metrics), gin.Recovery())
|
||||
r.Static("/static", "./static")
|
||||
r.Static("/dist", "./dist")
|
||||
r.GET("/metrics", gin.WrapH(metrics.Handler()))
|
||||
@@ -32,11 +34,8 @@ func ProvideRouter(htmlRender render.HTMLRender, metrics *observability.Metrics)
|
||||
return r
|
||||
}
|
||||
|
||||
func RunServer(lifecycle fx.Lifecycle, r *gin.Engine) {
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "3000"
|
||||
}
|
||||
func RunServer(cfg config.Config, lifecycle fx.Lifecycle, r *gin.Engine) {
|
||||
port := cfg.Port
|
||||
|
||||
srv := newHTTPServer(":"+port, r)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user