From 649ee06c63e97055797868a1f16722c0f57a4b29 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Tue, 7 Sep 2021 22:55:52 +0100 Subject: [PATCH] checkpoint: update config refactor --- internal/config/config.go | 120 ++++++++++++++++----------------- internal/config/config_test.go | 28 ++++---- internal/config/defaults.go | 52 -------------- internal/config/types.go | 68 +++++++++++++++++++ 4 files changed, 140 insertions(+), 128 deletions(-) delete mode 100644 internal/config/defaults.go create mode 100644 internal/config/types.go diff --git a/internal/config/config.go b/internal/config/config.go index e2b885d..cab63d2 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -6,68 +6,6 @@ import ( "io/ioutil" ) -// Config is the whole configuration for the forge platform deployment. -type Config struct { - ProjectName string `json:"project"` - Domain string `json:"domain"` - Docker DockerConfig `json:"docker"` - Services ServicesConfig `json:"services"` -} - -// DockerConfig contains the configuration for docker specific components. -type DockerConfig struct { - Network DockerNetworkConfig `json:"network"` - SharedVolume DockerSharedVolumeConfig `json:"sharedVolume"` - SharedGroupId int `json:"sharedGroupId"` -} - -// DockerNetworkConfig contains configuration for creating the docker network. -type DockerNetworkConfig struct { - Name string `json:"name"` - Subnet string `json:"subnet"` - Driver string `json:"driver"` -} - -// DockerSharedVolumeConfig contains configuration for creating the shared volume. -type DockerSharedVolumeConfig struct { - Name string `json:"name"` - MountPath string `json:"mountPath"` -} - -// Services contains a list of services and their configuration. -type ServicesConfig struct { - Traefik TraefikConfig `json:"traefik"` - Gitea GiteaConfig `json:"gitea"` -} - -// TraefikConfig contains configuration for the Traefik container. -type TraefikConfig struct { - CheckNewVersion bool `json:"checkNewVersion"` - ContainerIp string `json:"containerIp"` - LogLevel string `json:"logLevel"` - SendAnonymousUsage bool `json:"sendAnonymousUsage"` - Version string `json:"version"` -} - -// GiteaConfig contains configuration for the Gitea container. -type GiteaConfig struct { - AppName string `json:"appName"` - BaseUri string `json:"baseUri"` - ContainerIp string `json:"containerIp"` - ContainerDataDirectory string `json:"containerDataDirectory"` - ContainerTemporaryDirectory string `json:"containerTemporaryDirectory"` - HostDataDirectory string `json:"hostDataDirectory"` - HttpPort int `json:"httpPort"` - InternalToken string `json:"internalToken"` - LogLevel string `json:"logLevel"` - RunMode string `json:"runMode"` - SecretKey string `json:"secretKey"` - SshDomain string `json:"sshDomain"` - SshPort int `json:"sshPort"` - UserId int `json:"userId"` - Version string `json:"version"` -} - // NewConfig creates a new Config value from a given JSON file. func NewConfig(file string) (Config, error) { var err error @@ -83,5 +21,63 @@ func NewConfig(file string) (Config, error) { return c, fmt.Errorf("unable to decode the JSON configuration from %s...\n%v", file, err) } + // Propagate the domain to the services as appropriate + c.Services.Gitea.Domain = c.Domain + c.Services.Traefik.Domain = c.Domain + c.Services.Gitea.RootUrl = fmt.Sprintf("https://%s/%s", c.Domain, c.Services.Gitea.BaseUri) + c.Services.Gitea.SshDomain = c.Domain + + // Propagate the shared Group ID to the serivces as appropriate + c.Services.Traefik.GroupId = c.SharedGroupId + c.Services.Gitea.GroupId = c.SharedGroupId + return c, nil } + +func defaultConfig() Config { + c := Config{ + ProjectName: "", + Domain: "localhost", + SharedGroupId: 2239, + + Docker: DockerConfig{ + Network: DockerNetworkConfig{ + Name: "forge-platform-network", + Subnet: "172.20.0.0/24", + Driver: "default", + }, + SharedVolume: DockerSharedVolumeConfig{ + Name: "forge-platform-shared-volume", + MountPath: "/forge-platform/shared", + }, + }, + + Services: ServicesConfig{ + Traefik: TraefikConfig{ + CheckNewVersion: false, + ContainerIp: "172.20.0.2", + LogLevel: "info", + SendAnonymousUsage: false, + Version: "v2.5.2", + }, + Gitea: GiteaConfig{ + AppName: "Gitea", + BaseUri: "git", + ContainerIp: "172.20.0.3", + ContainerDataDirectory: "/forge-platform/data", + ContainerTemporaryDirectory: "/forge-platform/tmp", + HostDataDirectory: "/mnt/forge-platform/gitea", + HttpPort: 3000, + InternalToken: "", + LogLevel: "info", + RunMode: "prod", + SecretKey: "", + SshPort: 2222, + UserId: 2000, + Version: "1.50.0", + }, + }, + } + + return c +} diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 33febf8..426f6d2 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -37,22 +37,22 @@ func TestValidConfig(t *testing.T) { Version: "v2.4.9", }, Gitea: GiteaConfig{ - AppName: "A git hosting platform", - BaseUri: "git", - ContainerIp: "172.17.1.3", + AppName: "A git hosting platform", + BaseUri: "git", + ContainerIp: "172.17.1.3", DataDirectory: "/helix/data/gitea", - Domain: "forge.test.local", - GroupId: 0, - HttpPort: 3000, + Domain: "forge.test.local", + GroupId: 0, + HttpPort: 3000, InternalToken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE2MjY0ODQxNjV9.Lp2v7vluALZtAng1jte5-SvF69iAUoh9pFBxf-IJ1a0", - LogLevel: "info", - RootUrl: "https://forge.test.local/git", - RunMode: "test", - SecretKey: "gBFbTiV4GTwzonAyyHNKghc9lmWvaTmFqZs5u0h14Qgx5yp1OKlrZKgw1e5LfCiE", - SshDomain: "forge.test.local", - SshPort: 2222, - UserId: 0, - Version: "1.14.4", + LogLevel: "info", + RootUrl: "https://forge.test.local/git", + RunMode: "test", + SecretKey: "gBFbTiV4GTwzonAyyHNKghc9lmWvaTmFqZs5u0h14Qgx5yp1OKlrZKgw1e5LfCiE", + SshDomain: "forge.test.local", + SshPort: 2222, + UserId: 0, + Version: "1.14.4", }, }, }, diff --git a/internal/config/defaults.go b/internal/config/defaults.go deleted file mode 100644 index 465520d..0000000 --- a/internal/config/defaults.go +++ /dev/null @@ -1,52 +0,0 @@ -package config - -func defaultConfig() Config { - domain := "localhost" - - c := Config{ - ProjectName: "", - Domain: domain, - - Docker: DockerConfig{ - Network: DockerNetworkConfig{ - Name: "forge-platform-network", - Subnet: "172.20.0.0/24", - Driver: "default", - }, - SharedVolume: DockerSharedVolumeConfig{ - Name: "forge-platform-shared-volume", - MountPath: "/forge-platform/shared", - }, - SharedGroupId: 12239, - }, - - Services: ServicesConfig{ - Traefik: TraefikConfig{ - CheckNewVersion: false, - ContainerIp: "172.20.0.2", - LogLevel: "info", - SendAnonymousUsage: false, - Version: "v2.5.2", - }, - Gitea: GiteaConfig{ - AppName: "Gitea", - BaseUri: "git", - ContainerIp: "172.20.0.3", - ContainerDataDirectory: "/forge-platform/data", - ContainerTemporaryDirectory: "/forge-platform/tmp", - HostDataDirectory: "/mnt/forge-platform/gitea", - HttpPort: 3000, - InternalToken: "", - LogLevel: "info", - RunMode: "prod", - SecretKey: "", - SshDomain: domain, - SshPort: 2222, - UserId: 12000, - Version: "1.50.0", - }, - }, - } - - return c -} diff --git a/internal/config/types.go b/internal/config/types.go new file mode 100644 index 0000000..9f12bbe --- /dev/null +++ b/internal/config/types.go @@ -0,0 +1,68 @@ +package config + +// Config is the whole configuration for the forge platform deployment. +type Config struct { + ProjectName string `json:"project"` + Domain string `json:"domain"` + SharedGroupId int `json:"sharedGroupId"` + Docker DockerConfig `json:"docker"` + Services ServicesConfig `json:"services"` +} + +// DockerConfig contains the configuration for docker specific components. +type DockerConfig struct { + Network DockerNetworkConfig `json:"network"` + SharedVolume DockerSharedVolumeConfig `json:"sharedVolume"` +} + +// DockerNetworkConfig contains configuration for creating the docker network. +type DockerNetworkConfig struct { + Name string `json:"name"` + Subnet string `json:"subnet"` + Driver string `json:"driver"` +} + +// DockerSharedVolumeConfig contains configuration for creating the shared volume. +type DockerSharedVolumeConfig struct { + Name string `json:"name"` + MountPath string `json:"mountPath"` +} + +// Services contains a list of services and their configuration. +type ServicesConfig struct { + Traefik TraefikConfig `json:"traefik"` + Gitea GiteaConfig `json:"gitea"` +} + +// TraefikConfig contains configuration for the Traefik container. +type TraefikConfig struct { + CheckNewVersion bool `json:"checkNewVersion"` + SendAnonymousUsage bool `json:"sendAnonymousUsage"` + GroupId int + ContainerIp string `json:"containerIp"` + Domain string + LogLevel string `json:"logLevel"` + Version string `json:"version"` +} + +// GiteaConfig contains configuration for the Gitea container. +type GiteaConfig struct { + AppName string `json:"appName"` + BaseUri string `json:"baseUri"` + ContainerIp string `json:"containerIp"` + ContainerDataDirectory string `json:"containerDataDirectory"` + ContainerTemporaryDirectory string `json:"containerTemporaryDirectory"` + Domain string + HostDataDirectory string `json:"hostDataDirectory"` + HttpPort int `json:"httpPort"` + InternalToken string `json:"internalToken"` + LogLevel string `json:"logLevel"` + RootUrl string `json:"rootUrl"` + RunMode string `json:"runMode"` + SecretKey string `json:"secretKey"` + SshDomain string + SshPort int `json:"sshPort"` + UserId int `json:"userId"` + GroupId int + Version string `json:"version"` +}