Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better config #50

Merged
merged 6 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Config via env, dump config on admin panel
  • Loading branch information
thomiceli committed Jun 2, 2023
commit 57da3540f8527c9e6ce9ef2ab9847793120dd366
149 changes: 96 additions & 53 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"gopkg.in/yaml.v3"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
)
Expand All @@ -18,30 +19,30 @@ var C *config
// Not using nested structs because the library
// doesn't support dot notation in this case sadly
type config struct {
LogLevel string `yaml:"log-level"`
ExternalUrl string `yaml:"external-url"`
OpengistHome string `yaml:"opengist-home"`
DBFilename string `yaml:"db-filename"`

HttpHost string `yaml:"http.host"`
HttpPort string `yaml:"http.port"`
HttpGit bool `yaml:"http.git-enabled"`
HttpTLSEnabled bool `yaml:"http.tls-enabled"`
HttpCertFile string `yaml:"http.cert-file"`
HttpKeyFile string `yaml:"http.key-file"`

SshGit bool `yaml:"ssh.git-enabled"`
SshHost string `yaml:"ssh.host"`
SshPort string `yaml:"ssh.port"`
SshExternalDomain string `yaml:"ssh.external-domain"`
SshKeygen string `yaml:"ssh.keygen-executable"`

GithubClientKey string `yaml:"github.client-key"`
GithubSecret string `yaml:"github.secret"`

GiteaClientKey string `yaml:"gitea.client-key"`
GiteaSecret string `yaml:"gitea.secret"`
GiteaUrl string `yaml:"gitea.url"`
LogLevel string `yaml:"log-level" env:"OG_LOG_LEVEL"`
ExternalUrl string `yaml:"external-url" env:"OG_EXTERNAL_URL"`
OpengistHome string `yaml:"opengist-home" env:"OG_OPENGIST_HOME"`
DBFilename string `yaml:"db-filename" env:"OG_DB_FILENAME"`

HttpHost string `yaml:"http.host" env:"OG_HTTP_HOST"`
HttpPort string `yaml:"http.port" env:"OG_HTTP_PORT"`
HttpGit bool `yaml:"http.git-enabled" env:"OG_HTTP_GIT_ENABLED"`
HttpTLSEnabled bool `yaml:"http.tls-enabled" env:"OG_HTTP_TLS_ENABLED"`
HttpCertFile string `yaml:"http.cert-file" env:"OG_HTTP_CERT_FILE"`
HttpKeyFile string `yaml:"http.key-file" env:"OG_HTTP_KEY_FILE"`

SshGit bool `yaml:"ssh.git-enabled" env:"OG_SSH_GIT_ENABLED"`
SshHost string `yaml:"ssh.host" env:"OG_SSH_HOST"`
SshPort string `yaml:"ssh.port" env:"OG_SSH_PORT"`
SshExternalDomain string `yaml:"ssh.external-domain" env:"OG_SSH_EXTERNAL_DOMAIN"`
SshKeygen string `yaml:"ssh.keygen-executable" env:"OG_SSH_KEYGEN_EXECUTABLE"`

GithubClientKey string `yaml:"github.client-key" env:"OG_GITHUB_CLIENT_KEY"`
GithubSecret string `yaml:"github.secret" env:"OG_GITHUB_SECRET"`

GiteaClientKey string `yaml:"gitea.client-key" env:"OG_GITEA_CLIENT_KEY"`
GiteaSecret string `yaml:"gitea.secret" env:"OG_GITEA_SECRET"`
GiteaUrl string `yaml:"gitea.url" env:"OG_GITEA_URL"`
}

func configWithDefaults() (*config, error) {
Expand Down Expand Up @@ -77,37 +78,12 @@ func InitConfig(configPath string) error {
return err
}

if configPath != "" {
absolutePath, _ := filepath.Abs(configPath)
absolutePath = filepath.Clean(absolutePath)
file, err := os.Open(absolutePath)
if err != nil {
if !os.IsNotExist(err) {
return err
}
fmt.Println("No YML config file found at " + absolutePath)
} else {
fmt.Println("Using config file: " + absolutePath)

// Override default values with values from config.yml
d := yaml.NewDecoder(file)
if err = d.Decode(&c); err != nil {
return err
}
defer file.Close()
}
} else {
fmt.Println("No config file specified. Using default values.")
if err = loadConfigFromYaml(c, configPath); err != nil {
return err
}

// Override default values with environment variables (as yaml)
configEnv := os.Getenv("CONFIG")
if configEnv != "" {
fmt.Println("Using config from environment variable: CONFIG")
d := yaml.NewDecoder(strings.NewReader(configEnv))
if err = d.Decode(&c); err != nil {
return err
}
if err = loadConfigFromEnv(c); err != nil {
return err
}

C = c
Expand Down Expand Up @@ -159,3 +135,70 @@ func GetHomeDir() string {
absolutePath, _ := filepath.Abs(C.OpengistHome)
return filepath.Clean(absolutePath)
}

func loadConfigFromYaml(c *config, configPath string) error {
if configPath != "" {
absolutePath, _ := filepath.Abs(configPath)
absolutePath = filepath.Clean(absolutePath)
file, err := os.Open(absolutePath)
if err != nil {
if !os.IsNotExist(err) {
return err
}
fmt.Println("No YML config file found at " + absolutePath)
} else {
fmt.Println("Using config file: " + absolutePath)

// Override default values with values from config.yml
d := yaml.NewDecoder(file)
if err = d.Decode(&c); err != nil {
return err
}
defer file.Close()
}
} else {
fmt.Println("No config file specified. Using default values.")
}

// Override default values with environment variables (as yaml)
configEnv := os.Getenv("CONFIG")
if configEnv != "" {
fmt.Println("Using config from environment variable: CONFIG")
d := yaml.NewDecoder(strings.NewReader(configEnv))
if err := d.Decode(&c); err != nil {
return err
}
}

return nil
}

func loadConfigFromEnv(c *config) error {
v := reflect.ValueOf(c).Elem()

for i := 0; i < v.NumField(); i++ {
tag := v.Type().Field(i).Tag.Get("env")

if tag == "" {
continue
}

envValue := os.Getenv(strings.ToUpper(tag))
if envValue == "" {
continue
}

switch v.Field(i).Kind() {
case reflect.String:
v.Field(i).SetString(envValue)
case reflect.Bool:
boolVal, err := strconv.ParseBool(envValue)
if err != nil {
return err
}
v.Field(i).SetBool(boolVal)
}
}

return nil
}
13 changes: 7 additions & 6 deletions internal/web/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,16 @@ func adminSyncReposFromDB(ctx echo.Context) error {
return redirect(ctx, "/admin-panel")
}

func adminSettings(ctx echo.Context) error {
setData(ctx, "title", "Admin Settings")
setData(ctx, "htmlTitle", "Admin Settings - Admin panel")
setData(ctx, "adminHeaderPage", "settings")
func adminConfig(ctx echo.Context) error {
setData(ctx, "title", "Configuration")
setData(ctx, "htmlTitle", "Configuration - Admin panel")
setData(ctx, "adminHeaderPage", "config")
setData(ctx, "c", config.C)

return html(ctx, "admin_settings.html")
return html(ctx, "admin_config.html")
}

func adminSetSetting(ctx echo.Context) error {
func adminSetConfig(ctx echo.Context) error {
key := ctx.FormValue("key")
value := ctx.FormValue("value")

Expand Down
4 changes: 2 additions & 2 deletions internal/web/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ func Start() {
g2.POST("/gists/:gist/delete", adminGistDelete)
g2.POST("/sync-fs", adminSyncReposFromFS)
g2.POST("/sync-db", adminSyncReposFromDB)
g2.GET("/settings", adminSettings)
g2.PUT("/set-setting", adminSetSetting)
g2.GET("/configuration", adminConfig)
g2.PUT("/set-config", adminSetConfig)
}

g1.GET("/all", allGists, checkRequireLogin)
Expand Down
2 changes: 1 addition & 1 deletion public/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const setSetting = (key: string, value: string) => {
data.append('key', key);
data.append('value', value);
data.append('_csrf', ((document.getElementsByName('_csrf')[0] as HTMLInputElement).value));
return fetch('/admin-panel/set-setting', {
return fetch('/admin-panel/set-config', {
method: 'PUT',
credentials: 'same-origin',
body: data,
Expand Down
12 changes: 12 additions & 0 deletions public/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,15 @@ table.csv-table thead tr th {
table.csv-table tbody td {
@apply border py-1.5 px-1 border-slate-200 dark:border-slate-800;
}

dl.dl-config {
@apply grid grid-cols-3 text-sm;
}

dl.dl-config dt {
@apply col-span-1 text-gray-700 dark:text-slate-300 font-bold;
}

dl.dl-config dd {
@apply ml-1 col-span-2 break-words;
}
4 changes: 2 additions & 2 deletions templates/base/admin_header.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ <h1 class="text-2xl font-bold leading-tight">Admin panel</h1>
{{ else }} text-gray-600 dark:text-gray-400 hover:text-gray-400 dark:hover:text-slate-300 px-3 py-2 font-medium text-sm rounded-md {{ end }}" aria-current="page">Users</a>
<a href="/admin-panel/gists" class="{{ if eq .adminHeaderPage "gists" }}bg-gray-100 dark:bg-gray-700 text-slate-700 dark:text-slate-300 px-3 py-2 font-medium text-sm rounded-md
{{ else }} text-gray-600 dark:text-gray-400 hover:text-gray-400 dark:hover:text-slate-300 px-3 py-2 font-medium text-sm rounded-md {{ end }}" aria-current="page">Gists</a>
<a href="/admin-panel/settings" class="{{ if eq .adminHeaderPage "settings" }}bg-gray-100 dark:bg-gray-700 text-slate-700 dark:text-slate-300 px-3 py-2 font-medium text-sm rounded-md
{{ else }} text-gray-600 dark:text-gray-400 hover:text-gray-400 dark:hover:text-slate-300 px-3 py-2 font-medium text-sm rounded-md {{ end }}" aria-current="page">Admin settings</a>
<a href="/admin-panel/configuration" class="{{ if eq .adminHeaderPage "config" }}bg-gray-100 dark:bg-gray-700 text-slate-700 dark:text-slate-300 px-3 py-2 font-medium text-sm rounded-md
{{ else }} text-gray-600 dark:text-gray-400 hover:text-gray-400 dark:hover:text-slate-300 px-3 py-2 font-medium text-sm rounded-md {{ end }}" aria-current="page">Configuration</a>
</nav>
</div>
</div>
Expand Down
115 changes: 115 additions & 0 deletions templates/pages/admin_config.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{{ template "header" .}}
{{ template "admin_header" .}}

<div class="grid gap-4 grid-cols-1 md:grid-cols-2">
<div class="p-6 bg-gray-50 dark:bg-gray-800 rounded-md border border-gray-200 dark:border-gray-700">
<dl class="dl-config">
<div class="relative col-span-3">
<div class="absolute inset-0 flex items-center" aria-hidden="true">
<div class="w-full border-t border-gray-300"></div>
</div>
<div class="relative flex justify-center">
<span class="bg-gray-50 dark:bg-gray-800 px-2 text-sm text-slate-700 dark:text-slate-300 font-bold">General</span>
</div>
</div>
<dt>Log level</dt><dd>{{ .c.LogLevel }}</dd>
<dt>External URL</dt><dd>{{ .c.ExternalUrl }}</dd>
<dt>Opengist home</dt><dd>{{ .c.OpengistHome }}</dd>
<dt>DB filename</dt><dd>{{ .c.DBFilename }}</dd>
<div class="relative col-span-3 mt-4">
<div class="absolute inset-0 flex items-center" aria-hidden="true">
<div class="w-full border-t border-gray-300"></div>
</div>
<div class="relative flex justify-center">
<span class="bg-gray-50 dark:bg-gray-800 px-2 text-sm text-slate-700 dark:text-slate-300 font-bold">HTTP</span>
</div>
</div>
<dt>HTTP host</dt><dd>{{ .c.HttpHost }}</dd>
<dt>HTTP port</dt><dd>{{ .c.HttpPort }}</dd>
<dt>HTTP Git enabled</dt><dd>{{ .c.HttpGit }}</dd>
<dt>HTTP TLS enabled</dt><dd>{{ .c.HttpTLSEnabled }}</dd>
<dt>HTTP Cert file</dt><dd>{{ .c.HttpCertFile }}</dd>
<dt>HTTP Key file</dt><dd>{{ .c.HttpKeyFile }}</dd>
<div class="relative col-span-3 mt-4">
<div class="absolute inset-0 flex items-center" aria-hidden="true">
<div class="w-full border-t border-gray-300"></div>
</div>
<div class="relative flex justify-center">
<span class="bg-gray-50 dark:bg-gray-800 px-2 text-sm text-slate-700 dark:text-slate-300 font-bold">SSH</span>
</div>
</div>
<dt>SSH Git enabled</dt><dd>{{ .c.SshGit }}</dd>
<dt>SSH host</dt><dd>{{ .c.SshHost }}</dd>
<dt>SSH port</dt><dd>{{ .c.SshPort }}</dd>
<dt>SSH external domain</dt><dd>{{ .c.SshExternalDomain }}</dd>
<dt>SSH Keygen</dt><dd>{{ .c.SshKeygen }}</dd>
<div class="relative col-span-3 mt-4">
<div class="absolute inset-0 flex items-center" aria-hidden="true">
<div class="w-full border-t border-gray-300"></div>
</div>
<div class="relative flex justify-center">
<span class="bg-gray-50 dark:bg-gray-800 px-2 text-sm text-slate-700 dark:text-slate-300 font-bold">OAuth</span>
</div>
</div>
<dt>Github Client key</dt><dd>{{ .c.GithubClientKey }}</dd>
<dt>Github Secret</dt><dd>{{ .c.GithubSecret }}</dd>
<dt>Gitea client Key</dt><dd>{{ .c.GiteaClientKey }}</dd>
<dt>Gitea Secret</dt><dd>{{ .c.GiteaSecret }}</dd>
<dt>Gitea URL</dt><dd>{{ .c.GiteaUrl }}</dd>
</dl>
</div>
<div>
<ul role="list" class="divide-y divide-slate-300 dark:divide-gray-200 px-4 py-2 sm:px-6 bg-gray-50 dark:bg-gray-800 rounded-md border border-gray-200 dark:border-gray-700">
<li class="list-none gap-x-4 py-5">
<div class="flex items-center justify-between">
<span class="flex flex-grow flex-col">
<span class="text-sm font-medium leading-6 text-slate-700 dark:text-slate-300">Disable signup</span>
<span class="text-sm text-gray-400 dark:text-gray-400">Forbid the creation of new accounts.</span>
</span>
<button type="button" id="disable-signup" data-bool="{{ .DisableSignup }}" class="toggle-button {{ if .DisableSignup }}bg-primary-600{{else}}bg-gray-300 dark:bg-gray-400{{end}} relative inline-flex h-6 w-11 ml-4 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none focus:ring-2 focus:ring-primary-600 focus:ring-offset-2" role="switch" aria-checked="false" aria-labelledby="availability-label" aria-describedby="availability-description">
<span aria-hidden="true" class="{{ if .DisableSignup }}translate-x-5{{else}}translate-x-0{{end}} pointer-events-none inline-block h-5 w-5 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out"></span>
</button>
</div>
</li>
<li class="list-none gap-x-4 py-5">
<div class="flex items-center justify-between">
<span class="flex flex-grow flex-col">
<span class="text-sm font-medium leading-6 text-slate-700 dark:text-slate-300">Require login</span>
<span class="text-sm text-gray-400 dark:text-gray-400">Enforce users to be logged in to see gists.</span>
</span>
<button type="button" id="require-login" data-bool="{{ .RequireLogin }}" class="toggle-button {{ if .RequireLogin }}bg-primary-600{{else}}bg-gray-300 dark:bg-gray-400{{end}} relative inline-flex h-6 w-11 ml-4 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none focus:ring-2 focus:ring-primary-600 focus:ring-offset-2" role="switch" aria-checked="false" aria-labelledby="availability-label" aria-describedby="availability-description">
<span aria-hidden="true" class="{{ if .RequireLogin }}translate-x-5{{else}}translate-x-0{{end}} pointer-events-none inline-block h-5 w-5 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out"></span>
</button>
</div>
</li>
<li class="list-none gap-x-4 py-5">
<div class="flex items-center justify-between">
<span class="flex flex-grow flex-col">
<span class="text-sm font-medium leading-6 text-slate-700 dark:text-slate-300">Disable login form</span>
<span class="text-sm text-gray-400 dark:text-gray-400">Forbid logging in via the login form to force using OAuth providers instead.</span>
</span>
<button type="button" id="disable-login-form" data-bool="{{ .DisableLoginForm }}" class="toggle-button {{ if .DisableLoginForm }}bg-primary-600{{else}}bg-gray-300 dark:bg-gray-400{{end}} relative inline-flex h-6 w-11 ml-4 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none focus:ring-2 focus:ring-primary-600 focus:ring-offset-2" role="switch" aria-checked="false" aria-labelledby="availability-label" aria-describedby="availability-description">
<span aria-hidden="true" class="{{ if .DisableLoginForm }}translate-x-5{{else}}translate-x-0{{end}} pointer-events-none inline-block h-5 w-5 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out"></span>
</button>
</div>
</li>
<li class="list-none gap-x-4 py-5">
<div class="flex items-center justify-between">
<span class="flex flex-grow flex-col">
<span class="text-sm font-medium leading-6 text-slate-700 dark:text-slate-300">Disable Gravatar</span>
<span class="text-sm text-gray-400 dark:text-gray-400">Disable the usage of Gravatar as an avatar provider.</span>
</span>
<button type="button" id="disable-gravatar" data-bool="{{ .DisableGravatar }}" class="toggle-button {{ if .DisableGravatar }}bg-primary-600{{else}}bg-gray-300 dark:bg-gray-400{{end}} relative inline-flex h-6 w-11 ml-4 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none focus:ring-2 focus:ring-primary-600 focus:ring-offset-2" role="switch" aria-checked="false" aria-labelledby="availability-label" aria-describedby="availability-description">
<span aria-hidden="true" class="{{ if .DisableGravatar }}translate-x-5{{else}}translate-x-0{{end}} pointer-events-none inline-block h-5 w-5 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out"></span>
</button>
</div>
</li>
</ul>
{{ .csrfHtml }}
</div>
</div>

<script type="module" src="{{ asset "admin.ts" }}"></script>

{{ template "admin_footer" .}}
{{ template "footer" .}}
Loading