Added TLS support

This commit is contained in:
Thomas Miceli 2023-03-15 10:37:17 +01:00
parent 2c40060a60
commit e95a1b6840
No known key found for this signature in database
GPG key ID: D86C6F6390AF050F
3 changed files with 33 additions and 12 deletions

View file

@ -25,6 +25,15 @@ http:
# Enable or disable git operations (clone, pull, push) via HTTP (either `true` or `false`). Default: true
git-enabled: true
# Enable or disable TLS (either `true` or `false`). Default: false
tls-enabled: false
# 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
# Note: it is not using the SSH daemon from your machine (yet)
ssh:

View file

@ -23,6 +23,9 @@ type config struct {
Port string `yaml:"port"`
Domain string `yaml:"domain"`
Git bool `yaml:"git-enabled"`
TLSEnabled bool `yaml:"tls-enabled"`
CertFile string `yaml:"cert-file"`
KeyFile string `yaml:"key-file"`
} `yaml:"http"`
SSH struct {
@ -51,6 +54,8 @@ func configWithDefaults() (*config, error) {
c.HTTP.Domain = "localhost"
c.HTTP.Git = true
c.HTTP.TLSEnabled = false
c.SSH.Enabled = true
c.SSH.Host = "0.0.0.0"
c.SSH.Port = "2222"

View file

@ -102,7 +102,7 @@ func Start() {
}
}
e.Use(basicInit)
e.Use(sessionInit)
e.Validator = NewValidator()
@ -168,32 +168,39 @@ func Start() {
// Git HTTP routes
if config.C.HTTP.Git {
e.Any("/:user/:gistname/*", gitHttp, gistInit)
debugStr = " (with Git HTTP support)"
debugStr = " (with Git over HTTP)"
}
e.Any("/*", noRouteFound)
addr := config.C.HTTP.Host + ":" + config.C.HTTP.Port
log.Info().Msg("Starting HTTP server on http://" + addr + debugStr)
if config.C.HTTP.TLSEnabled {
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 {
log.Fatal().Err(err).Msg("Failed to start HTTPS server")
}
} else {
log.Info().Msg("Starting HTTP server on http://" + addr + debugStr)
if err := e.Start(addr); err != nil {
log.Fatal().Err(err).Msg("Failed to start HTTP server")
}
}
}
func dataInit(next echo.HandlerFunc) echo.HandlerFunc {
return func(ctx echo.Context) error {
ctxValue := context.WithValue(ctx.Request().Context(), "data", echo.Map{})
ctx.SetRequest(ctx.Request().WithContext(ctxValue))
setData(ctx, "loadStartTime", time.Now())
setData(ctx, "signupDisabled", config.C.DisableSignup)
return next(ctx)
}
}
func basicInit(next echo.HandlerFunc) echo.HandlerFunc {
func sessionInit(next echo.HandlerFunc) echo.HandlerFunc {
return func(ctx echo.Context) error {
setData(ctx, "signupDisabled", config.C.DisableSignup)
sess := getSession(ctx)
if sess.Values["user"] != nil {
user := &models.User{ID: sess.Values["user"].(uint)}