Tweak config and git urls

This commit is contained in:
Thomas Miceli 2023-04-07 01:52:56 +02:00
parent b606b67d43
commit eeebc7c789
No known key found for this signature in database
GPG key ID: D86C6F6390AF050F
7 changed files with 117 additions and 103 deletions

View file

@ -33,8 +33,8 @@ COPY . .
# Build the application # Build the application
RUN make RUN make
# Expose the port for the webserver # Expose the ports
EXPOSE 6157 EXPOSE 6157 2222
# Mount the .opengist volume # Mount the .opengist volume
VOLUME /root/.opengist VOLUME /root/.opengist

View file

@ -1,58 +1,58 @@
# Set the log level to one of the following: trace, debug, info, warn, error, fatal, panic. Default: warn
log-level: warn
# Public URL for the Git HTTP/SSH connection.
# If not set, uses the URL from the request
external-url:
# Prevents the creation of new accounts (either `true` or `false`). Default: false
disable-signup: false
# Directory where Opengist will store its data. Default: ~/.opengist/ # Directory where Opengist will store its data. Default: ~/.opengist/
opengist-home: opengist-home:
# Name of the SQLite database file. Default: opengist.db # Name of the SQLite database file. Default: opengist.db
db-filename: opengist.db db-filename: opengist.db
# Prevents the creation of new accounts (either `true` or `false`). Default: false
disable-signup: false
# Set the log level to one of the following: trace, debug, info, warn, error, fatal, panic. Default: warn
log-level: warn
# HTTP server configuration # HTTP server configuration
http: # Host to bind to. Default: 0.0.0.0
http.host: 0.0.0.0
# Host to bind to. Default: 0.0.0.0 # Port to bind to. Default: 6157
host: 0.0.0.0 http.port: 6157
# Port to bind to. Default: 6157 # Enable or disable git operations (clone, pull, push) via HTTP (either `true` or `false`). Default: true
port: 6157 http.git-enabled: true
# Domain to use in links. Default: localhost # Enable or disable TLS (either `true` or `false`). Default: false
domain: localhost http.tls-enabled: false
# Enable or disable git operations (clone, pull, push) via HTTP (either `true` or `false`). Default: true # Path to the TLS certificate file if TLS is enabled
git-enabled: true http.cert-file:
# Enable or disable TLS (either `true` or `false`). Default: false # Path to the TLS key file if TLS is enabled
tls-enabled: false http.key-file:
# Path to the TLS certificate file if TLS is enabled
cert-file:
# Path to the TLS key file if TLS is enabled
key-file:
# SSH built-in server configuration # SSH built-in server configuration
# Note: it is not using the SSH daemon from your machine (yet) # Note: it is not using the SSH daemon from your machine (yet)
ssh:
# Enable or disable SSH built-in server # Enable or disable SSH built-in server
# for git operations (clone, pull, push) via SSH (either `true` or `false`). Default: true # for git operations (clone, pull, push) via SSH (either `true` or `false`). Default: true
enabled: true ssh.git-enabled: true
# Host to bind to. Default: 0.0.0.0 # Host to bind to. Default: 0.0.0.0
host: 0.0.0.0 ssh.host: 0.0.0.0
# Port to bind to. Default: 2222 # Port to bind to. Default: 2222
# Note: it cannot be the same port as the SSH daemon if it's currently running # Note: it cannot be the same port as the SSH daemon if it's currently running
# If you want to use the port 22 for the built-in SSH server, # If you want to use the port 22 for the built-in SSH server,
# you can either change the port of the SSH daemon or stop it # you can either change the port of the SSH daemon or stop it
port: 2222 ssh.port: 2222
# Domain to use in links. Default: localhost # Public domain for the Git SSH connection, if it has to be different from the HTTP one.
domain: localhost # If not set, uses the URL from the request
ssh.external-domain:
# Path or alias to ssh-keygen executable. Default: ssh-keygen # Path or alias to ssh-keygen executable. Default: ssh-keygen
keygen-executable: ssh-keygen ssh.keygen-executable: ssh-keygen

View file

@ -15,29 +15,27 @@ var OpengistVersion = "0.0.1"
var C *config var C *config
// Not using nested structs because the library
// doesn't support dot notation in this case sadly
type config struct { type config struct {
LogLevel string `yaml:"log-level"`
ExternalUrl string `yaml:"external-url"`
DisableSignup bool `yaml:"disable-signup"`
OpengistHome string `yaml:"opengist-home"` OpengistHome string `yaml:"opengist-home"`
DBFilename string `yaml:"db-filename"` DBFilename string `yaml:"db-filename"`
DisableSignup bool `yaml:"disable-signup"`
LogLevel string `yaml:"log-level"`
HTTP struct { HttpHost string `yaml:"http.host"`
Host string `yaml:"host"` HttpPort string `yaml:"http.port"`
Port string `yaml:"port"` HttpGit bool `yaml:"http.git-enabled"`
Domain string `yaml:"domain"` HttpTLSEnabled bool `yaml:"http.tls-enabled"`
Git bool `yaml:"git-enabled"` HttpCertFile string `yaml:"http.cert-file"`
TLSEnabled bool `yaml:"tls-enabled"` HttpKeyFile string `yaml:"http.key-file"`
CertFile string `yaml:"cert-file"`
KeyFile string `yaml:"key-file"`
} `yaml:"http"`
SSH struct { SshGit bool `yaml:"ssh.git-enabled"`
Enabled bool `yaml:"enabled"` SshHost string `yaml:"ssh.host"`
Host string `yaml:"host"` SshPort string `yaml:"ssh.port"`
Port string `yaml:"port"` SshExternalDomain string `yaml:"ssh.external-domain"`
Domain string `yaml:"domain"` SshKeygen string `yaml:"ssh.keygen-executable"`
Keygen string `yaml:"keygen-executable"`
} `yaml:"ssh"`
} }
func configWithDefaults() (*config, error) { func configWithDefaults() (*config, error) {
@ -47,43 +45,53 @@ func configWithDefaults() (*config, error) {
return c, err return c, err
} }
c.LogLevel = "warn"
c.DisableSignup = false
c.OpengistHome = filepath.Join(homeDir, ".opengist") c.OpengistHome = filepath.Join(homeDir, ".opengist")
c.DBFilename = "opengist.db" c.DBFilename = "opengist.db"
c.DisableSignup = false
c.LogLevel = "warn"
c.HTTP.Host = "0.0.0.0" c.HttpHost = "0.0.0.0"
c.HTTP.Port = "6157" c.HttpPort = "6157"
c.HTTP.Domain = "localhost" c.HttpGit = true
c.HTTP.Git = true c.HttpTLSEnabled = false
c.HTTP.TLSEnabled = false c.SshGit = true
c.SshHost = "0.0.0.0"
c.SSH.Enabled = true c.SshPort = "2222"
c.SSH.Host = "0.0.0.0" c.SshKeygen = "ssh-keygen"
c.SSH.Port = "2222"
c.SSH.Domain = "localhost"
c.SSH.Keygen = "ssh-keygen"
return c, nil return c, nil
} }
func InitConfig(configPath string) error { func InitConfig(configPath string) error {
// Default values
c, err := configWithDefaults() c, err := configWithDefaults()
if err != nil { if err != nil {
return err return err
} }
file, err := os.Open(configPath) file, err := os.Open(configPath)
if err != nil { if err == nil {
return err fmt.Println("Using config file: " + configPath)
}
defer file.Close()
d := yaml.NewDecoder(file) // Override default values with values from config.yml
if err = d.Decode(&c); err != nil { d := yaml.NewDecoder(file)
return err if err = d.Decode(&c); err != nil {
return err
}
defer file.Close()
} }
// 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
}
}
C = c C = c
return nil return nil

View file

@ -18,7 +18,7 @@ import (
) )
func Start() { func Start() {
if !config.C.SSH.Enabled { if !config.C.SshGit {
return return
} }
@ -47,8 +47,8 @@ func Start() {
} }
func listen(serverConfig *ssh.ServerConfig) { func listen(serverConfig *ssh.ServerConfig) {
log.Info().Msg("Starting SSH server on ssh://" + config.C.SSH.Host + ":" + config.C.SSH.Port) log.Info().Msg("Starting SSH server on ssh://" + config.C.SshHost + ":" + config.C.SshPort)
listener, err := net.Listen("tcp", config.C.SSH.Host+":"+config.C.SSH.Port) listener, err := net.Listen("tcp", config.C.SshHost+":"+config.C.SshPort)
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("SSH: Failed to start SSH server") log.Fatal().Err(err).Msg("SSH: Failed to start SSH server")
} }
@ -129,7 +129,7 @@ func setupHostKey() (ssh.Signer, error) {
keyPath := filepath.Join(dir, "opengist-ed25519") keyPath := filepath.Join(dir, "opengist-ed25519")
if _, err := os.Stat(keyPath); err != nil && !os.IsExist(err) { if _, err := os.Stat(keyPath); err != nil && !os.IsExist(err) {
cmd := exec.Command(config.C.SSH.Keygen, cmd := exec.Command(config.C.SshKeygen,
"-t", "ssh-ed25519", "-t", "ssh-ed25519",
"-f", keyPath, "-f", keyPath,
"-m", "PEM", "-m", "PEM",

View file

@ -30,11 +30,19 @@ func gistInit(next echo.HandlerFunc) echo.HandlerFunc {
} }
setData(ctx, "gist", gist) setData(ctx, "gist", gist)
if config.C.SSH.Enabled { if config.C.SshGit {
if config.C.SSH.Port == "22" { var sshDomain string
setData(ctx, "sshCloneUrl", config.C.SSH.Domain+":"+userName+"/"+gistName+".git")
if config.C.SshExternalDomain != "" {
sshDomain = config.C.SshExternalDomain
} else { } else {
setData(ctx, "sshCloneUrl", "ssh://"+config.C.SSH.Domain+":"+config.C.SSH.Port+"/"+userName+"/"+gistName+".git") sshDomain = strings.Split(ctx.Request().Host, ":")[0]
}
if config.C.SshPort == "22" {
setData(ctx, "sshCloneUrl", sshDomain+":"+userName+"/"+gistName+".git")
} else {
setData(ctx, "sshCloneUrl", "ssh://"+sshDomain+":"+config.C.SshPort+"/"+userName+"/"+gistName+".git")
} }
} }
@ -44,20 +52,19 @@ func gistInit(next echo.HandlerFunc) echo.HandlerFunc {
} }
setData(ctx, "httpProtocol", strings.ToUpper(httpProtocol)) setData(ctx, "httpProtocol", strings.ToUpper(httpProtocol))
if config.C.HTTP.Git { var baseHttpUrl string
if config.C.HTTP.Port == "80" || config.C.HTTP.Port == "443" { // if a custom external url is set, use it
setData(ctx, "httpCloneUrl", httpProtocol+"://"+config.C.HTTP.Domain+"/"+userName+"/"+gistName+".git") if config.C.ExternalUrl != "" {
} else { baseHttpUrl = config.C.ExternalUrl
setData(ctx, "httpCloneUrl", httpProtocol+"://"+config.C.HTTP.Domain+":"+config.C.HTTP.Port+"/"+userName+"/"+gistName+".git")
}
}
if config.C.HTTP.Port == "80" || config.C.HTTP.Port == "443" {
setData(ctx, "httpCopyUrl", httpProtocol+"://"+config.C.HTTP.Domain+"/"+userName+"/"+gistName)
} else { } else {
setData(ctx, "httpCopyUrl", httpProtocol+"://"+config.C.HTTP.Domain+":"+config.C.HTTP.Port+"/"+userName+"/"+gistName) baseHttpUrl = httpProtocol + "://" + ctx.Request().Host
} }
if config.C.HttpGit {
setData(ctx, "httpCloneUrl", baseHttpUrl+"/"+userName+"/"+gistName+".git")
}
setData(ctx, "httpCopyUrl", baseHttpUrl+"/"+userName+"/"+gistName)
setData(ctx, "currentUrl", template.URL(ctx.Request().URL.Path)) setData(ctx, "currentUrl", template.URL(ctx.Request().URL.Path))
nbCommits, err := gist.NbCommits() nbCommits, err := gist.NbCommits()

View file

@ -209,18 +209,18 @@ func Start() {
debugStr := "" debugStr := ""
// Git HTTP routes // Git HTTP routes
if config.C.HTTP.Git { if config.C.HttpGit {
e.Any("/:user/:gistname/*", gitHttp, gistInit) e.Any("/:user/:gistname/*", gitHttp, gistInit)
debugStr = " (with Git over HTTP)" debugStr = " (with Git over HTTP)"
} }
e.Any("/*", noRouteFound) e.Any("/*", noRouteFound)
addr := config.C.HTTP.Host + ":" + config.C.HTTP.Port addr := config.C.HttpHost + ":" + config.C.HttpPort
if config.C.HTTP.TLSEnabled { if config.C.HttpTLSEnabled {
log.Info().Msg("Starting HTTPS server on https://" + addr + debugStr) log.Info().Msg("Starting HTTPS server on https://" + addr + debugStr)
if err := e.StartTLS(addr, config.C.HTTP.CertFile, config.C.HTTP.KeyFile); err != nil { if err := e.StartTLS(addr, config.C.HttpCertFile, config.C.HttpKeyFile); err != nil {
log.Fatal().Err(err).Msg("Failed to start HTTPS server") log.Fatal().Err(err).Msg("Failed to start HTTPS server")
} }
} else { } else {

View file

@ -14,6 +14,8 @@ import (
) )
func initialize() { func initialize() {
fmt.Println("Opengist v" + config.OpengistVersion)
configPath := flag.String("config", "config.yml", "Path to a config file in YML format") configPath := flag.String("config", "config.yml", "Path to a config file in YML format")
flag.Parse() flag.Parse()
absolutePath, _ := filepath.Abs(*configPath) absolutePath, _ := filepath.Abs(*configPath)
@ -27,9 +29,6 @@ func initialize() {
config.InitLog() config.InitLog()
fmt.Println("Opengist v" + config.OpengistVersion)
fmt.Println("Using config file: " + absolutePath)
gitVersion, err := git.GetGitVersion() gitVersion, err := git.GetGitVersion()
if err != nil { if err != nil {
log.Fatal().Err(err).Send() log.Fatal().Err(err).Send()