Compare commits
	
		
			3 Commits
		
	
	
		
			f136ae58b3
			...
			228d1bffc1
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 228d1bffc1 | |||
| 6d15ce2df9 | |||
| 5680f9471f | 
@@ -3,7 +3,7 @@
 | 
			
		||||
# syntax=docker/dockerfile:1
 | 
			
		||||
 | 
			
		||||
# Build the application from source
 | 
			
		||||
FROM golang:1.24.3 AS build-stage
 | 
			
		||||
FROM golang:1.25.1-alpine3.22 AS build-stage
 | 
			
		||||
 | 
			
		||||
WORKDIR /app
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -18,6 +18,9 @@ import (
 | 
			
		||||
func main() {
 | 
			
		||||
	fx.New(
 | 
			
		||||
		fx.Provide(
 | 
			
		||||
			// Logger
 | 
			
		||||
			NewLogger,
 | 
			
		||||
 | 
			
		||||
			// Configuration
 | 
			
		||||
			config.Load,
 | 
			
		||||
			
 | 
			
		||||
@@ -37,9 +40,6 @@ func main() {
 | 
			
		||||
			handlers.NewApiEndpointHandler,
 | 
			
		||||
			handlers.NewEmbedCardHandler,
 | 
			
		||||
			handlers.NewOauthLoginHandler,
 | 
			
		||||
			
 | 
			
		||||
			// Logger
 | 
			
		||||
			NewLogger,
 | 
			
		||||
		),
 | 
			
		||||
		fx.Invoke(
 | 
			
		||||
			// Start background tasks
 | 
			
		||||
 
 | 
			
		||||
@@ -5,13 +5,15 @@ import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"io"
 | 
			
		||||
	"log/slog"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"net/url"
 | 
			
		||||
 | 
			
		||||
	"go.uber.org/zap"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type GiteaOAuth2Handler struct {
 | 
			
		||||
	cfg *config.Config
 | 
			
		||||
	logger *zap.Logger
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewGiteaOauth2Token(cfg *config.Config) *GiteaOAuth2Handler {
 | 
			
		||||
@@ -24,7 +26,7 @@ func (g *GiteaOAuth2Handler) GetGiteaLoginURL(redirectHost string) (string, erro
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	if redirectHost == "" {
 | 
			
		||||
		slog.Error("Redirect host not provided")
 | 
			
		||||
		g.logger.Error("Redirect host not provided")
 | 
			
		||||
		return "", nil
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
@@ -35,7 +37,7 @@ func (g *GiteaOAuth2Handler) GetGiteaLoginURL(redirectHost string) (string, erro
 | 
			
		||||
func (g *GiteaOAuth2Handler) GetGiteaUserEmailByCode(code string) (string, error) {
 | 
			
		||||
 | 
			
		||||
	if g.cfg.GiteaOauthInstance == "" {
 | 
			
		||||
        slog.Error("Instance URL not provided")
 | 
			
		||||
        g.logger.Error("Instance URL not provided")
 | 
			
		||||
		return "", nil
 | 
			
		||||
	}
 | 
			
		||||
	// No need to verify since we are accesing the gitea once and only for the email
 | 
			
		||||
@@ -45,7 +47,7 @@ func (g *GiteaOAuth2Handler) GetGiteaUserEmailByCode(code string) (string, error
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	userInfoUrl := g.cfg.GiteaOauthInstance + "/login/oauth/userinfo"
 | 
			
		||||
	slog.Info(userInfoUrl)
 | 
			
		||||
	g.logger.Info(userInfoUrl)
 | 
			
		||||
	req, err := http.NewRequest("POST", userInfoUrl, nil)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return "", err
 | 
			
		||||
 
 | 
			
		||||
@@ -1,11 +1,11 @@
 | 
			
		||||
package config
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"log/slog"
 | 
			
		||||
	"os"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/joho/godotenv"
 | 
			
		||||
	"go.uber.org/zap"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Config struct {
 | 
			
		||||
@@ -32,10 +32,10 @@ type Config struct {
 | 
			
		||||
	GiteaOauthAllowedEmails []string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Load() *Config {
 | 
			
		||||
func Load(logger *zap.Logger) *Config {
 | 
			
		||||
	err := godotenv.Load()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		slog.Warn("Error loading .env file - Using environment variables instead")
 | 
			
		||||
		logger.Warn("Error loading .env file - Using environment variables instead")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Get mastodon instance
 | 
			
		||||
@@ -51,7 +51,7 @@ func Load() *Config {
 | 
			
		||||
	// Get admin password (Its a single user/admin app so its just fine)
 | 
			
		||||
	adminPassword := os.Getenv("CAOM_ADMIN_PASSWORD")
 | 
			
		||||
	if adminPassword == "" {
 | 
			
		||||
		slog.Warn("No admin password provided, using default password 'catsaregood'")
 | 
			
		||||
		logger.Warn("No admin password provided, using default password 'catsaregood'")
 | 
			
		||||
		adminPassword = "catsaregood"
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -62,12 +62,12 @@ func Load() *Config {
 | 
			
		||||
	}
 | 
			
		||||
	issuer := os.Getenv("CAOM_JWT_ISSUER")
 | 
			
		||||
	if issuer == "" {
 | 
			
		||||
		slog.Info("No jwt issuer provided, using default issuer 'CatsOfMastodonBotGo'")
 | 
			
		||||
		logger.Info("No jwt issuer provided, using default issuer 'CatsOfMastodonBotGo'")
 | 
			
		||||
		issuer = "CatsOfMastodonBotGo"
 | 
			
		||||
	}
 | 
			
		||||
	audience := os.Getenv("CAOM_JWT_AUDIENCE")
 | 
			
		||||
	if audience == "" {
 | 
			
		||||
		slog.Info("No jwt audience provided, using default audience 'CatsOfMastodonBotGo'")
 | 
			
		||||
		logger.Info("No jwt audience provided, using default audience 'CatsOfMastodonBotGo'")
 | 
			
		||||
		audience = "CatsOfMastodonBotGo"
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -89,7 +89,7 @@ func Load() *Config {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if dbEngine == "" || dbHost == "" || dbPort == "" || dbUser == "" || dbPassword == "" || dbName == "" {
 | 
			
		||||
		slog.Info("No database connection provided, using sqlite")
 | 
			
		||||
		logger.Info("No database connection provided, using sqlite")
 | 
			
		||||
		dbEngine = "sqlite"
 | 
			
		||||
		dbHost = ""
 | 
			
		||||
		dbPort = ""
 | 
			
		||||
@@ -100,7 +100,7 @@ func Load() *Config {
 | 
			
		||||
 | 
			
		||||
	imageKitId := os.Getenv("CAOM_IMAGEKIT_ID")
 | 
			
		||||
	if imageKitId == "" {
 | 
			
		||||
		slog.Info("No imagekit id provided, not using imagekit.io")
 | 
			
		||||
		logger.Info("No imagekit id provided, not using imagekit.io")
 | 
			
		||||
	}
 | 
			
		||||
	// Initialize AppContext
 | 
			
		||||
	return &Config{
 | 
			
		||||
@@ -126,4 +126,4 @@ func Load() *Config {
 | 
			
		||||
		GiteaOauthClientSecret:  giteaOauthClientSecret,
 | 
			
		||||
		GiteaOauthAllowedEmails: giteaOauthAllowedEmailsParsed,
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -13,6 +13,9 @@ import (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// fx.In allows fx to inject multiple dependencies
 | 
			
		||||
 | 
			
		||||
// I think we could just put all thses in SetupRouter() but in this way we are first defining the dependencies we need
 | 
			
		||||
// and by using fx.In we say that whenever RouterParams was needed, inject dependensies into it and give it to SetupRouter()
 | 
			
		||||
type RouterParams struct {
 | 
			
		||||
	fx.In
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user