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
path: root/app.go
diff options
context:
space:
mode:
authorKamil Trzcinski <ayufan@ayufan.eu>2016-02-11 20:43:26 +0300
committerKamil Trzcinski <ayufan@ayufan.eu>2016-02-11 20:43:26 +0300
commit8b6077ea12539ec80d531c8f2a1a8b0188766d2e (patch)
tree8086ac80019f2a8540641e48b69604c1f643fb90 /app.go
parent61c3aed8fc1105f9c85d636fe8de0197ddffbc63 (diff)
Split main.go to app.go
Diffstat (limited to 'app.go')
-rw-r--r--app.go80
1 files changed, 80 insertions, 0 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
+}