Commit a827a033 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Move routes into route.go

parent 70f191d2
package main
import (
"./internal/errorpage"
"./internal/git"
"./internal/lfs"
"net/http"
"regexp"
)
type route struct {
method string
regex *regexp.Regexp
handler http.Handler
}
const projectPattern = `^/[^/]+/[^/]+/`
const gitProjectPattern = `^/[^/]+/[^/]+\.git/`
const apiPattern = `^/api/`
const projectsAPIPattern = `^/api/v3/projects/[^/]+/`
const ciAPIPattern = `^/ci/api/`
// Routing table
// We match against URI not containing the relativeUrlRoot:
// see upstream.ServeHTTP
var routes []route
func (u *upstream) compileRoutes() {
u.routes = []route{
// Git Clone
route{"GET", regexp.MustCompile(gitProjectPattern + `info/refs\z`), git.GetInfoRefs(u.API)},
route{"POST", regexp.MustCompile(gitProjectPattern + `git-upload-pack\z`), contentEncodingHandler(git.PostRPC(u.API))},
route{"POST", regexp.MustCompile(gitProjectPattern + `git-receive-pack\z`), contentEncodingHandler(git.PostRPC(u.API))},
route{"PUT", regexp.MustCompile(gitProjectPattern + `gitlab-lfs/objects/([0-9a-f]{64})/([0-9]+)\z`), lfs.PutStore(u.API, u.Proxy)},
// Repository Archive
route{"GET", regexp.MustCompile(projectPattern + `repository/archive\z`), git.GetArchive(u.API)},
route{"GET", regexp.MustCompile(projectPattern + `repository/archive.zip\z`), git.GetArchive(u.API)},
route{"GET", regexp.MustCompile(projectPattern + `repository/archive.tar\z`), git.GetArchive(u.API)},
route{"GET", regexp.MustCompile(projectPattern + `repository/archive.tar.gz\z`), git.GetArchive(u.API)},
route{"GET", regexp.MustCompile(projectPattern + `repository/archive.tar.bz2\z`), git.GetArchive(u.API)},
// Repository Archive API
route{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive\z`), git.GetArchive(u.API)},
route{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.zip\z`), git.GetArchive(u.API)},
route{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.tar\z`), git.GetArchive(u.API)},
route{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.tar.gz\z`), git.GetArchive(u.API)},
route{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.tar.bz2\z`), git.GetArchive(u.API)},
// CI Artifacts API
route{"POST", regexp.MustCompile(ciAPIPattern + `v1/builds/[0-9]+/artifacts\z`), contentEncodingHandler(artifactsAuthorizeHandler(u.API, handleFileUploads(u.Proxy)))},
// Explicitly u.Proxy API requests
route{"", regexp.MustCompile(apiPattern), u.Proxy},
route{"", regexp.MustCompile(ciAPIPattern), u.Proxy},
// Serve assets
route{"", regexp.MustCompile(`^/assets/`),
handleServeFile(u.DocumentRoot, u.urlPrefix, CacheExpireMax,
handleDevelopmentMode(developmentMode,
handleDeployPage(u.DocumentRoot,
errorpage.Inject(u.DocumentRoot,
u.Proxy,
),
),
),
),
},
// Serve static files or forward the requests
route{"", nil,
handleServeFile(u.DocumentRoot, u.urlPrefix, CacheDisabled,
handleDeployPage(u.DocumentRoot,
errorpage.Inject(u.DocumentRoot,
u.Proxy,
),
),
),
},
}
}
...@@ -8,16 +8,12 @@ package main ...@@ -8,16 +8,12 @@ package main
import ( import (
"./internal/api" "./internal/api"
"./internal/errorpage"
"./internal/git"
"./internal/lfs"
"./internal/proxy" "./internal/proxy"
"fmt" "fmt"
"log" "log"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
"regexp"
"strings" "strings"
"time" "time"
) )
...@@ -30,80 +26,6 @@ type upstream struct { ...@@ -30,80 +26,6 @@ type upstream struct {
routes []route routes []route
} }
type route struct {
method string
regex *regexp.Regexp
handler http.Handler
}
const projectPattern = `^/[^/]+/[^/]+/`
const gitProjectPattern = `^/[^/]+/[^/]+\.git/`
const apiPattern = `^/api/`
const projectsAPIPattern = `^/api/v3/projects/[^/]+/`
const ciAPIPattern = `^/ci/api/`
// Routing table
// We match against URI not containing the relativeUrlRoot:
// see upstream.ServeHTTP
var routes []route
func (u *upstream) compileRoutes() {
u.routes = []route{
// Git Clone
route{"GET", regexp.MustCompile(gitProjectPattern + `info/refs\z`), git.GetInfoRefs(u.API)},
route{"POST", regexp.MustCompile(gitProjectPattern + `git-upload-pack\z`), contentEncodingHandler(git.PostRPC(u.API))},
route{"POST", regexp.MustCompile(gitProjectPattern + `git-receive-pack\z`), contentEncodingHandler(git.PostRPC(u.API))},
route{"PUT", regexp.MustCompile(gitProjectPattern + `gitlab-lfs/objects/([0-9a-f]{64})/([0-9]+)\z`), lfs.PutStore(u.API, u.Proxy)},
// Repository Archive
route{"GET", regexp.MustCompile(projectPattern + `repository/archive\z`), git.GetArchive(u.API)},
route{"GET", regexp.MustCompile(projectPattern + `repository/archive.zip\z`), git.GetArchive(u.API)},
route{"GET", regexp.MustCompile(projectPattern + `repository/archive.tar\z`), git.GetArchive(u.API)},
route{"GET", regexp.MustCompile(projectPattern + `repository/archive.tar.gz\z`), git.GetArchive(u.API)},
route{"GET", regexp.MustCompile(projectPattern + `repository/archive.tar.bz2\z`), git.GetArchive(u.API)},
// Repository Archive API
route{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive\z`), git.GetArchive(u.API)},
route{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.zip\z`), git.GetArchive(u.API)},
route{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.tar\z`), git.GetArchive(u.API)},
route{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.tar.gz\z`), git.GetArchive(u.API)},
route{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.tar.bz2\z`), git.GetArchive(u.API)},
// CI Artifacts API
route{"POST", regexp.MustCompile(ciAPIPattern + `v1/builds/[0-9]+/artifacts\z`), contentEncodingHandler(artifactsAuthorizeHandler(u.API, handleFileUploads(u.Proxy)))},
// Explicitly u.Proxy API requests
route{"", regexp.MustCompile(apiPattern), u.Proxy},
route{"", regexp.MustCompile(ciAPIPattern), u.Proxy},
// Serve assets
route{"", regexp.MustCompile(`^/assets/`),
handleServeFile(u.DocumentRoot, u.urlPrefix, CacheExpireMax,
handleDevelopmentMode(developmentMode,
handleDeployPage(u.DocumentRoot,
errorpage.Inject(u.DocumentRoot,
u.Proxy,
),
),
),
),
},
// Serve static files or forward the requests
route{"", nil,
handleServeFile(u.DocumentRoot, u.urlPrefix, CacheDisabled,
handleDeployPage(u.DocumentRoot,
errorpage.Inject(u.DocumentRoot,
u.Proxy,
),
),
),
},
}
}
func newUpstream(authBackend string, authSocket string) *upstream { func newUpstream(authBackend string, authSocket string) *upstream {
parsedURL, err := url.Parse(authBackend) parsedURL, err := url.Parse(authBackend)
if err != nil { if err != nil {
...@@ -146,8 +68,6 @@ func newUpstream(authBackend string, authSocket string) *upstream { ...@@ -146,8 +68,6 @@ func newUpstream(authBackend string, authSocket string) *upstream {
} }
func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) { func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
var g route
w := newLoggingResponseWriter(ow) w := newLoggingResponseWriter(ow)
defer w.Log(r) defer w.Log(r)
...@@ -172,13 +92,14 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) { ...@@ -172,13 +92,14 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
} }
// Look for a matching Git service // Look for a matching Git service
var ro route
foundService := false foundService := false
for _, g = range u.routes { for _, ro = range u.routes {
if g.method != "" && r.Method != g.method { if ro.method != "" && r.Method != ro.method {
continue continue
} }
if g.regex == nil || g.regex.MatchString(prefix.strip(URIPath)) { if ro.regex == nil || ro.regex.MatchString(prefix.strip(URIPath)) {
foundService = true foundService = true
break break
} }
...@@ -190,5 +111,5 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) { ...@@ -190,5 +111,5 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
return return
} }
g.handler.ServeHTTP(&w, r) ro.handler.ServeHTTP(&w, r)
} }
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