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
import (
"fmt"
"io/ioutil"
"net/http"
"log"
"net/http"
)
type errorPageResponseWriter struct {
......
......@@ -22,6 +22,7 @@ import (
_ "net/http/pprof"
"os"
"regexp"
"strings"
"syscall"
"time"
)
......@@ -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 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 relativeUrlRoot = flag.String("relativeUrlRoot", "/", "GitLab relative URL root")
type httpRoute struct {
method string
......@@ -84,6 +86,10 @@ func main() {
os.Exit(0)
}
if !strings.HasSuffix(*relativeUrlRoot, "/") {
*relativeUrlRoot += "/"
}
log.Printf("Starting %s", version)
// Good housekeeping for Unix sockets: unlink before binding
......@@ -128,6 +134,6 @@ func main() {
// Because net/http/pprof installs itself in the DefaultServeMux
// we create a fresh one for the Git server.
serveMux := http.NewServeMux()
serveMux.Handle("/", newUpstream(*authBackend, authTransport))
serveMux.Handle(*relativeUrlRoot, newUpstream(*authBackend, authTransport))
log.Fatal(http.Serve(listener, serveMux))
}
......@@ -22,8 +22,8 @@ type sendFileResponseWriter struct {
func newSendFileResponseWriter(rw http.ResponseWriter, req *http.Request) *sendFileResponseWriter {
s := &sendFileResponseWriter{
rw: rw,
req: req,
rw: rw,
req: req,
}
req.Header.Set("X-Sendfile-Type", "X-Sendfile")
return s
......
......@@ -16,7 +16,7 @@ func handleServeFile(rootDir string, notFoundHandler serviceHandleFunc) serviceH
}
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)
if err != nil {
fail500(w, fmt.Errorf("invalid path:"+file, err))
......
......@@ -11,6 +11,7 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"strings"
)
type serviceHandleFunc func(w http.ResponseWriter, r *gitRequest)
......@@ -53,6 +54,7 @@ type authorizationResponse struct {
// GitLab Rails application.
type gitRequest struct {
*http.Request
relativeUriPath string
authorizationResponse
u *upstream
}
......@@ -78,6 +80,9 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
w := newLoggingResponseWriter(ow)
defer w.Log(r)
// Strip prefix and add "/"
relativeUriPath := "/" + strings.TrimPrefix(r.RequestURI, *relativeUrlRoot)
// Look for a matching Git service
foundService := false
for _, g = range httpRoutes {
......@@ -85,7 +90,7 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
continue
}
if g.regex == nil || g.regex.MatchString(r.URL.Path) {
if g.regex == nil || g.regex.MatchString(relativeUriPath) {
foundService = true
break
}
......@@ -98,8 +103,9 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
}
request := gitRequest{
Request: r,
u: u,
Request: r,
relativeUriPath: relativeUriPath,
u: u,
}
g.handleFunc(w, &request)
......
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