Commit cff33472 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Support relativeUrlRoot allowing to serve the app from subpath

parent 3920a26e
...@@ -3,8 +3,8 @@ package main ...@@ -3,8 +3,8 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http"
"log" "log"
"net/http"
) )
type errorPageResponseWriter struct { type errorPageResponseWriter struct {
......
...@@ -22,6 +22,7 @@ import ( ...@@ -22,6 +22,7 @@ import (
_ "net/http/pprof" _ "net/http/pprof"
"os" "os"
"regexp" "regexp"
"strings"
"syscall" "syscall"
"time" "time"
) )
...@@ -35,6 +36,7 @@ var listenUmask = flag.Int("listenUmask", 022, "Umask for Unix socket, default: ...@@ -35,6 +36,7 @@ var listenUmask = flag.Int("listenUmask", 022, "Umask for Unix socket, default:
var authBackend = flag.String("authBackend", "http://localhost:8080", "Authentication/authorization backend") var authBackend = flag.String("authBackend", "http://localhost:8080", "Authentication/authorization backend")
var authSocket = flag.String("authSocket", "", "Optional: Unix domain socket to dial authBackend at") var authSocket = flag.String("authSocket", "", "Optional: Unix domain socket to dial authBackend at")
var pprofListenAddr = flag.String("pprofListenAddr", "", "pprof listening address, e.g. 'localhost:6060'") var pprofListenAddr = flag.String("pprofListenAddr", "", "pprof listening address, e.g. 'localhost:6060'")
var relativeUrlRoot = flag.String("relativeUrlRoot", "/", "GitLab relative URL root")
type httpRoute struct { type httpRoute struct {
method string method string
...@@ -84,6 +86,10 @@ func main() { ...@@ -84,6 +86,10 @@ func main() {
os.Exit(0) os.Exit(0)
} }
if !strings.HasSuffix(*relativeUrlRoot, "/") {
*relativeUrlRoot += "/"
}
log.Printf("Starting %s", version) log.Printf("Starting %s", version)
// Good housekeeping for Unix sockets: unlink before binding // Good housekeeping for Unix sockets: unlink before binding
...@@ -128,6 +134,6 @@ func main() { ...@@ -128,6 +134,6 @@ func main() {
// Because net/http/pprof installs itself in the DefaultServeMux // Because net/http/pprof installs itself in the DefaultServeMux
// we create a fresh one for the Git server. // we create a fresh one for the Git server.
serveMux := http.NewServeMux() serveMux := http.NewServeMux()
serveMux.Handle("/", newUpstream(*authBackend, authTransport)) serveMux.Handle(*relativeUrlRoot, newUpstream(*authBackend, authTransport))
log.Fatal(http.Serve(listener, serveMux)) log.Fatal(http.Serve(listener, serveMux))
} }
...@@ -16,7 +16,7 @@ func handleServeFile(rootDir string, notFoundHandler serviceHandleFunc) serviceH ...@@ -16,7 +16,7 @@ func handleServeFile(rootDir string, notFoundHandler serviceHandleFunc) serviceH
} }
return func(w http.ResponseWriter, r *gitRequest) { return func(w http.ResponseWriter, r *gitRequest) {
file := filepath.Join(rootDir, r.URL.Path) file := filepath.Join(rootDir, r.relativeUriPath)
file, err := filepath.Abs(file) file, err := filepath.Abs(file)
if err != nil { if err != nil {
fail500(w, fmt.Errorf("invalid path:"+file, err)) fail500(w, fmt.Errorf("invalid path:"+file, err))
......
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"net/url" "net/url"
"strings"
) )
type serviceHandleFunc func(w http.ResponseWriter, r *gitRequest) type serviceHandleFunc func(w http.ResponseWriter, r *gitRequest)
...@@ -53,6 +54,7 @@ type authorizationResponse struct { ...@@ -53,6 +54,7 @@ type authorizationResponse struct {
// GitLab Rails application. // GitLab Rails application.
type gitRequest struct { type gitRequest struct {
*http.Request *http.Request
relativeUriPath string
authorizationResponse authorizationResponse
u *upstream u *upstream
} }
...@@ -78,6 +80,9 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) { ...@@ -78,6 +80,9 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
w := newLoggingResponseWriter(ow) w := newLoggingResponseWriter(ow)
defer w.Log(r) defer w.Log(r)
// Strip prefix and add "/"
relativeUriPath := "/" + strings.TrimPrefix(r.RequestURI, *relativeUrlRoot)
// Look for a matching Git service // Look for a matching Git service
foundService := false foundService := false
for _, g = range httpRoutes { for _, g = range httpRoutes {
...@@ -85,7 +90,7 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) { ...@@ -85,7 +90,7 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
continue continue
} }
if g.regex == nil || g.regex.MatchString(r.URL.Path) { if g.regex == nil || g.regex.MatchString(relativeUriPath) {
foundService = true foundService = true
break break
} }
...@@ -99,6 +104,7 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) { ...@@ -99,6 +104,7 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
request := gitRequest{ request := gitRequest{
Request: r, Request: r,
relativeUriPath: relativeUriPath,
u: u, u: u,
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment