refactor: remove root url

This commit is contained in:
Stavros
2025-01-20 18:22:17 +02:00
parent 4f4645f32b
commit e2f97d1fbe
5 changed files with 22 additions and 5 deletions

View File

@@ -66,13 +66,11 @@ func init() {
rootCmd.Flags().IntP("port", "p", 3000, "Port to run the server on.")
rootCmd.Flags().String("address", "0.0.0.0", "Address to bind the server to.")
rootCmd.Flags().String("secret", "", "Secret to use for the cookie.")
rootCmd.Flags().String("root-url", "", "Root URL of traefik.")
rootCmd.Flags().String("app-url", "", "The tinyauth URL.")
rootCmd.Flags().String("users", "", "Comma separated list of users in the format username:bcrypt-hashed-password.")
viper.BindEnv("port", "PORT")
viper.BindEnv("address", "ADDRESS")
viper.BindEnv("secret", "SECRET")
viper.BindEnv("root-url", "ROOT_URL")
viper.BindEnv("app-url", "APP_URL")
viper.BindEnv("users", "USERS")
viper.BindPFlags(rootCmd.Flags())

View File

@@ -24,7 +24,6 @@ services:
image: ghcr.io/steveiliop56/tinyauth:latest
environment:
- SECRET=some-random-32-chars-string
- ROOT_URL=https://example.com
- APP_URL=https://tinyauth.example.com
- USERS=user:$$2a$$10$$UdLYoJ5lgPsC0RKqYH/jMua7zIn0g9kPqWmhYayJYLaZQ/FTmH2/u
labels:

View File

@@ -11,6 +11,7 @@ import (
"tinyauth/internal/auth"
"tinyauth/internal/hooks"
"tinyauth/internal/types"
"tinyauth/internal/utils"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
@@ -33,7 +34,12 @@ func Run(config types.Config, users types.UserList) {
fileServer := http.FileServer(http.FS(dist))
store := cookie.NewStore([]byte(config.Secret))
domain := strings.Split(config.RootURL, "://")[1]
domain, domainErr := utils.GetRootURL(config.AppURL)
if domainErr != nil {
log.Fatal().Err(domainErr).Msg("Failed to get domain")
os.Exit(1)
}
store.Options(sessions.Options{
Domain: fmt.Sprintf(".%s", domain),

View File

@@ -22,7 +22,6 @@ type Config struct {
Port int `validate:"number" mapstructure:"port"`
Address string `mapstructure:"address, ip4_addr"`
Secret string `validate:"required,len=32" mapstructure:"secret"`
RootURL string `validate:"required,url" mapstructure:"root-url"`
AppURL string `validate:"required,url" mapstructure:"app-url"`
Users string `validate:"required" mapstructure:"users"`
}

View File

@@ -2,6 +2,7 @@ package utils
import (
"errors"
"net/url"
"strings"
"tinyauth/internal/types"
)
@@ -27,3 +28,17 @@ func CreateUsersList(users string) (types.UserList, error) {
return userList, nil
}
func GetRootURL(urlSrc string) (string, error) {
urlParsed, parseErr := url.Parse(urlSrc)
if parseErr != nil {
return "", parseErr
}
urlSplitted := strings.Split(urlParsed.Host, ".")
urlFinal := urlSplitted[len(urlSplitted)-2] + "." + urlSplitted[len(urlSplitted)-1]
return urlFinal, nil
}