Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app.go80
-rw-r--r--domains.go8
-rw-r--r--main.go77
3 files changed, 85 insertions, 80 deletions
diff --git a/app.go b/app.go
new file mode 100644
index 00000000..08e82042
--- /dev/null
+++ b/app.go
@@ -0,0 +1,80 @@
+package main
+
+import (
+ "crypto/tls"
+ "net/http"
+ "strings"
+ "sync"
+)
+
+const xForwardedProto = "X-Forwarded-Proto"
+const xForwardedProtoHTTPS = "https"
+
+type theApp struct {
+ domains domains
+ lock sync.RWMutex
+}
+
+func (a *theApp) domain(host string) *domain {
+ host = strings.ToLower(host)
+ a.lock.RLock()
+ defer a.lock.RUnlock()
+ domain, _ := a.domains[host]
+ return domain
+}
+
+func (a *theApp) ServeTLS(ch *tls.ClientHelloInfo) (*tls.Certificate, error) {
+ if ch.ServerName == "" {
+ return nil, nil
+ }
+
+ if domain := a.domain(ch.ServerName); domain != nil {
+ tls, _ := domain.ensureCertificate()
+ return tls, nil
+ }
+
+ return nil, nil
+}
+
+func (a *theApp) serveContent(ww http.ResponseWriter, r *http.Request, https bool) {
+ w := newLoggingResponseWriter(ww)
+ defer w.Log(r)
+
+ // Add auto redirect
+ if https && !*serverHTTP {
+ u := *r.URL
+ u.Scheme = "https"
+ u.Host = r.Host
+ u.User = nil
+
+ http.Redirect(&w, r, u.String(), 307)
+ return
+ }
+
+ domain := a.domain(r.Host)
+ if domain == nil {
+ http.NotFound(&w, r)
+ return
+ }
+
+ // Serve static file
+ domain.ServeHTTP(&w, r)
+}
+
+func (a *theApp) ServeHTTP(ww http.ResponseWriter, r *http.Request) {
+ https := r.TLS != nil
+ a.serveContent(ww, r, https)
+}
+
+func (a *theApp) ServeProxy(ww http.ResponseWriter, r *http.Request) {
+ forwardedProto := r.Header.Get(xForwardedProto)
+ https := forwardedProto == xForwardedProtoHTTPS
+
+ a.serveContent(ww, r, https)
+}
+
+func (a *theApp) UpdateDomains(domains domains) {
+ a.lock.Lock()
+ defer a.lock.Unlock()
+ a.domains = domains
+}
diff --git a/domains.go b/domains.go
index 9d8bdcc3..b9e4b9c8 100644
--- a/domains.go
+++ b/domains.go
@@ -22,12 +22,14 @@ func (d domains) addDomain(group, project string, config *domainConfig) error {
Config: config,
}
+ var domainName string
if config != nil {
- d[config.Domain] = newDomain
+ domainName = config.Domain
} else {
- domainName := group + "." + *pagesDomain
- d[domainName] = newDomain
+ domainName = group + "." + *pagesDomain
}
+ domainName = strings.ToLower(domainName)
+ d[domainName] = newDomain
return nil
}
diff --git a/main.go b/main.go
index 3a6cdd9c..30cbb5de 100644
--- a/main.go
+++ b/main.go
@@ -1,13 +1,10 @@
package main
import (
- "crypto/tls"
"flag"
"fmt"
"log"
- "net/http"
"path/filepath"
- "strings"
"sync"
"time"
)
@@ -28,80 +25,6 @@ var serverHTTP = flag.Bool("serve-http", true, "Serve the pages under HTTP")
var http2proto = flag.Bool("http2", true, "Enable HTTP2 support")
var pagesRoot = flag.String("pages-root", "shared/pages", "The directory where pages are stored")
-const xForwardedProto = "X-Forwarded-Proto"
-const xForwardedProtoHTTPS = "https"
-
-type theApp struct {
- domains domains
- lock sync.RWMutex
-}
-
-func (a *theApp) domain(host string) *domain {
- a.lock.RLock()
- defer a.lock.RUnlock()
- domain, _ := a.domains[host]
- return domain
-}
-
-func (a *theApp) ServeTLS(ch *tls.ClientHelloInfo) (*tls.Certificate, error) {
- if ch.ServerName == "" {
- return nil, nil
- }
-
- host := strings.ToLower(ch.ServerName)
- if domain := a.domain(host); domain != nil {
- tls, _ := domain.ensureCertificate()
- return tls, nil
- }
-
- return nil, nil
-}
-
-func (a *theApp) serveContent(ww http.ResponseWriter, r *http.Request, https bool) {
- w := newLoggingResponseWriter(ww)
- defer w.Log(r)
-
- // Add auto redirect
- if https && !*serverHTTP {
- u := *r.URL
- u.Scheme = "https"
- u.Host = r.Host
- u.User = nil
-
- http.Redirect(&w, r, u.String(), 307)
- return
- }
-
- host := strings.ToLower(r.Host)
- domain := a.domain(host)
-
- if domain == nil {
- http.NotFound(&w, r)
- return
- }
-
- // Serve static file
- domain.ServeHTTP(&w, r)
-}
-
-func (a *theApp) ServeHTTP(ww http.ResponseWriter, r *http.Request) {
- https := r.TLS != nil
- a.serveContent(ww, r, https)
-}
-
-func (a *theApp) ServeProxy(ww http.ResponseWriter, r *http.Request) {
- forwardedProto := r.Header.Get(xForwardedProto)
- https := forwardedProto == xForwardedProtoHTTPS
- a.serveContent(ww, r, https)
-}
-
-func (a *theApp) UpdateDomains(domains domains) {
- fmt.Printf("Domains: %v", domains)
- a.lock.Lock()
- a.domains = domains
- a.lock.Unlock()
-}
-
func resolve() {
fullPath, err := filepath.EvalSymlinks(*pagesRoot)
if err != nil {