Add initial support for unix sockets (#2852)

This commit is contained in:
Odin Ugedal 2016-08-11 23:46:33 +02:00 committed by 无闻
parent 70fbcd2f27
commit 1dd003bd4c
3 changed files with 44 additions and 13 deletions

View File

@ -8,6 +8,7 @@ import (
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net"
"net/http" "net/http"
"net/http/fcgi" "net/http/fcgi"
"os" "os"
@ -582,6 +583,9 @@ func runWeb(ctx *cli.Context) error {
var err error var err error
listenAddr := fmt.Sprintf("%s:%s", setting.HttpAddr, setting.HttpPort) listenAddr := fmt.Sprintf("%s:%s", setting.HttpAddr, setting.HttpPort)
if setting.Protocol == setting.UNIX_SOCKET {
listenAddr = fmt.Sprintf("%s", setting.HttpAddr)
}
log.Info("Listen: %v://%s%s", setting.Protocol, listenAddr, setting.AppSubUrl) log.Info("Listen: %v://%s%s", setting.Protocol, listenAddr, setting.AppSubUrl)
switch setting.Protocol { switch setting.Protocol {
case setting.HTTP: case setting.HTTP:
@ -591,6 +595,20 @@ func runWeb(ctx *cli.Context) error {
err = server.ListenAndServeTLS(setting.CertFile, setting.KeyFile) err = server.ListenAndServeTLS(setting.CertFile, setting.KeyFile)
case setting.FCGI: case setting.FCGI:
err = fcgi.Serve(nil, m) err = fcgi.Serve(nil, m)
case setting.UNIX_SOCKET:
os.Remove(listenAddr)
listener, err := net.ListenUnix("unix", &net.UnixAddr{listenAddr, "unix"})
if err != nil {
break
}
// FIXME add proper implementation of signal capture on all protocols
// execute this on SIGTERM or SIGINT: listener.Close()
err = os.Chmod(listenAddr, os.FileMode(setting.UnixSocketPermission))
if err != nil {
log.Fatal(4, "Failed to set permission of unix socket: %v", err)
}
err = http.Serve(listener, m)
default: default:
log.Fatal(4, "Invalid protocol: %s", setting.Protocol) log.Fatal(4, "Invalid protocol: %s", setting.Protocol)
} }

View File

@ -61,6 +61,8 @@ DOMAIN = localhost
ROOT_URL = %(PROTOCOL)s://%(DOMAIN)s:%(HTTP_PORT)s/ ROOT_URL = %(PROTOCOL)s://%(DOMAIN)s:%(HTTP_PORT)s/
HTTP_ADDR = 0.0.0.0 HTTP_ADDR = 0.0.0.0
HTTP_PORT = 3000 HTTP_PORT = 3000
; Permission for unix socket
UNIX_SOCKET_PERMISSION = 666
; Local (DMZ) URL for Gogs workers (such as SSH update) accessing web service. ; Local (DMZ) URL for Gogs workers (such as SSH update) accessing web service.
; In most cases you do not need to change the default value. ; In most cases you do not need to change the default value.
; Alter it only if your SSH server node is not the same as HTTP node. ; Alter it only if your SSH server node is not the same as HTTP node.

View File

@ -12,6 +12,7 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strconv"
"strings" "strings"
"time" "time"
@ -31,9 +32,10 @@ import (
type Scheme string type Scheme string
const ( const (
HTTP Scheme = "http" HTTP Scheme = "http"
HTTPS Scheme = "https" HTTPS Scheme = "https"
FCGI Scheme = "fcgi" FCGI Scheme = "fcgi"
UNIX_SOCKET Scheme = "unix"
) )
type LandingPage string type LandingPage string
@ -58,16 +60,17 @@ var (
AppDataPath string AppDataPath string
// Server settings // Server settings
Protocol Scheme Protocol Scheme
Domain string Domain string
HttpAddr, HttpPort string HttpAddr, HttpPort string
LocalURL string LocalURL string
OfflineMode bool OfflineMode bool
DisableRouterLog bool DisableRouterLog bool
CertFile, KeyFile string CertFile, KeyFile string
StaticRootPath string StaticRootPath string
EnableGzip bool EnableGzip bool
LandingPageUrl LandingPage LandingPageUrl LandingPage
UnixSocketPermission uint32
SSH struct { SSH struct {
Disabled bool `ini:"DISABLE_SSH"` Disabled bool `ini:"DISABLE_SSH"`
@ -367,11 +370,19 @@ func NewContext() {
KeyFile = sec.Key("KEY_FILE").String() KeyFile = sec.Key("KEY_FILE").String()
} else if sec.Key("PROTOCOL").String() == "fcgi" { } else if sec.Key("PROTOCOL").String() == "fcgi" {
Protocol = FCGI Protocol = FCGI
} else if sec.Key("PROTOCOL").String() == "unix" {
Protocol = UNIX_SOCKET
} }
Domain = sec.Key("DOMAIN").MustString("localhost") Domain = sec.Key("DOMAIN").MustString("localhost")
HttpAddr = sec.Key("HTTP_ADDR").MustString("0.0.0.0") HttpAddr = sec.Key("HTTP_ADDR").MustString("0.0.0.0")
HttpPort = sec.Key("HTTP_PORT").MustString("3000") HttpPort = sec.Key("HTTP_PORT").MustString("3000")
LocalURL = sec.Key("LOCAL_ROOT_URL").MustString(string(Protocol) + "://localhost:" + HttpPort + "/") LocalURL = sec.Key("LOCAL_ROOT_URL").MustString(string(Protocol) + "://localhost:" + HttpPort + "/")
UnixSocketPermissionRaw := sec.Key("UNIX_SOCKET_PERMISSION").MustString("666")
UnixSocketPermissionParsed, err := strconv.ParseUint(UnixSocketPermissionRaw, 8, 32)
if err != nil || UnixSocketPermissionParsed > 0777 {
log.Fatal(4, "Fail to parse unixSocketPermission: %s", UnixSocketPermissionRaw)
}
UnixSocketPermission = uint32(UnixSocketPermissionParsed)
OfflineMode = sec.Key("OFFLINE_MODE").MustBool() OfflineMode = sec.Key("OFFLINE_MODE").MustBool()
DisableRouterLog = sec.Key("DISABLE_ROUTER_LOG").MustBool() DisableRouterLog = sec.Key("DISABLE_ROUTER_LOG").MustBool()
StaticRootPath = sec.Key("STATIC_ROOT_PATH").MustString(workDir) StaticRootPath = sec.Key("STATIC_ROOT_PATH").MustString(workDir)