Browse Source

added systemd socket support, moved stuff around

main
Diane 8 months ago
parent
commit
a50386127b
No known key found for this signature in database GPG Key ID: 2D0649E76A7C8D25
  1. 95
      config.go
  2. 66
      gitmgr.go
  3. 5
      go.mod
  4. 2
      go.sum

95
config.go

@ -0,0 +1,95 @@
package main
import (
"fmt"
"github.com/coreos/go-systemd/activation"
"net"
"os"
"strconv"
"strings"
"time"
)
type ListenMode uint8
const (
ListenModeHttp = iota
ListenModeUnix
httpTimeout = 5 * time.Second
maxDescLengthErr = "max description length invalid format: %s is not a number\n%e\n"
)
type Config struct {
Listen net.Listener
ListenMode ListenMode
GitRoot string
IsExported string
MaxDescriptionLength int
CloneUrlTemplate string
}
func getEnv(name, defaultValue string) string {
val := os.Getenv(name)
if len(val) == 0 {
return defaultValue
}
return val
}
func getMaxDescLength(defaultVal string) (int, error) {
maxDescLengthStr := getEnv("MAX_DESCRIPTION_LENGTH", defaultVal)
maxDescLength, err := strconv.Atoi(maxDescLengthStr)
if err != nil {
return 0, fmt.Errorf(maxDescLengthErr, maxDescLengthStr, err)
}
return maxDescLength, nil
}
func getListenData(defaultAddr string) (net.Listener, error) {
systemdListeners, err := activation.Listeners()
if err != nil {
return nil, err
}
var listener net.Listener
if len(systemdListeners) == 0 {
listen := getEnv("LISTEN", defaultAddr)
var listenMode ListenMode
if strings.Contains(listen, ":") &&
net.ParseIP(strings.SplitN(listen, ":", 2)[0]) != nil {
listenMode = ListenModeHttp
} else {
listenMode = ListenModeUnix
}
listener, err = net.Listen(
[]string{"tcp", "unix"}[listenMode],
listen,
)
} else {
listener = systemdListeners[0] // Doesn't support more than a listener
}
return listener, err
}
func GetConfig() (*Config, error) {
maxDescLength, err := getMaxDescLength("120")
if err != nil {
return nil, err
}
listen, err := getListenData("/run/gitmgr.sock")
if err != nil {
return nil, err
}
return &Config{
Listen: listen,
GitRoot: getEnv("GIT_ROOT", "/srv/git"),
IsExported: getEnv("EXPORTED_FILE", "git-daemon-export-ok"),
MaxDescriptionLength: maxDescLength,
CloneUrlTemplate: getEnv("CLONE_URL_TEMPLATE", ""),
}, nil
}

66
gitmgr.go

@ -3,67 +3,10 @@ package main
import (
"fmt"
"github.com/gorilla/mux"
"net"
"net/http"
"os"
"strconv"
"strings"
"text/template"
"time"
)
type ListenMode uint8
const (
ListenModeHttp = iota
ListenModeUnix
httpTimeout = 5 * time.Second
maxDescLengthErr = "max description length invalid format: %s is not a number\n%e\n"
)
type Config struct {
Listen string
ListenMode ListenMode
GitRoot string
IsExported string
MaxDescriptionLength int
CloneUrlTemplate string
}
func getEnv(name, defaultValue string) string {
val := os.Getenv(name)
if len(val) == 0 {
return defaultValue
}
return val
}
func GetConfig() (*Config, error) {
maxDescLengthStr := getEnv("MAX_DESCRIPTION_LENGTH", "120")
maxDescLength, err := strconv.Atoi(maxDescLengthStr)
if err != nil {
return nil, fmt.Errorf(maxDescLengthErr, maxDescLengthStr, err)
}
listen := getEnv("LISTEN", "/run/gitmgr.sock")
var listenMode ListenMode
if strings.Contains(listen, ":") &&
net.ParseIP(strings.SplitN(listen, ":", 2)[0]) != nil {
listenMode = ListenModeHttp
} else {
listenMode = ListenModeUnix
}
return &Config{
ListenMode: listenMode,
Listen: listen,
GitRoot: getEnv("GIT_ROOT", "/srv/git"),
IsExported: getEnv("EXPORTED_FILE", "git-daemon-export-ok"),
MaxDescriptionLength: maxDescLength,
CloneUrlTemplate: getEnv("CLONE_URL_TEMPLATE", ""),
}, nil
}
func main() {
config, err := GetConfig()
if err != nil {
@ -95,15 +38,8 @@ func main() {
ReadTimeout: httpTimeout,
WriteTimeout: httpTimeout,
}
listener, err := net.Listen(
[]string{"tcp", "unix"}[config.ListenMode],
config.Listen,
)
if err != nil {
panic(err)
}
err = server.Serve(listener)
err = server.Serve(config.Listen)
if err != nil {
panic(err)
}

5
go.mod

@ -2,4 +2,7 @@ module gitmgr
go 1.16
require github.com/gorilla/mux v1.8.0 // indirect
require (
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf
github.com/gorilla/mux v1.8.0
)

2
go.sum

@ -1,2 +1,4 @@
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf h1:iW4rZ826su+pqaw19uhpSCzhj44qo35pNgKFGqzDKkU=
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
Loading…
Cancel
Save