-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
160 lines (145 loc) · 4.49 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"context"
"flag"
"net/http"
"os/exec"
"path/filepath"
"strings"
"github.com/NYTimes/gziphandler"
"github.com/flosch/pongo2"
"github.com/golang/glog"
)
var (
flagListenAddress string
flagSSLListenAddress string
flagSSLCert string
flagSSLKey string
flagDomain string
flagProd bool
flagSitePath string
)
func EASFSHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Frame-Options", "SAMEORIGIN")
w.Header().Set("Server", "easfs")
if r.Host != flagDomain {
RedirectDomain(w, r)
return
}
ext := filepath.Ext(r.URL.Path)
if ext == ".md" || ext == ".html" {
http.Redirect(w, r, strings.TrimSuffix(r.URL.Path, ext), 301)
return
}
// expiration := time.Now().Add(time.Hour)
// cookie := http.Cookie{Name: "hl", Value: language, Expires: expiration}
// http.SetCookie(w, &cookie)
url := r.URL.Path
if url == "/_s/getsuggestions" {
if r.URL.Query().Get("c") == "2" {
if r.URL.Query().Get("p") == "" {
url = "/_suggestions"
} else {
url = filepath.Join("/", r.URL.Query().Get("p"), "/_suggestions")
}
} else {
url = "/_empty_suggestions"
}
} else if strings.Contains(url, "/_") {
ReturnError(w, EASFSError{Code: http.StatusNotFound, Title: "404 Not Found", Description: "The requested URL was not found on this server."})
return
}
var err error
if IsDir(flagSitePath + url) {
// make sure that directory ends with a /
if !strings.HasSuffix(url, "/") {
http.Redirect(w, r, r.URL.Path+"/", 301)
return
}
err = GetPage(w, url)
if err.Error() != "file not found" {
ReturnError(w, EASFSError{Code: http.StatusInternalServerError, Title: "500 Internal Server Error", Description: err.Error()})
return
}
if err != nil {
err = GetIndex(w, url)
}
} else {
err = GetPage(w, url)
}
if err != nil {
if err.Error() != "file not found" {
ReturnError(w, EASFSError{Code: http.StatusInternalServerError, Title: "500 Internal Server Error", Description: err.Error()})
return
}
red, err := GetRedirect(url)
if err == nil {
http.Redirect(w, r, red, 301)
} else {
ReturnError(w, EASFSError{Code: http.StatusNotFound, Title: "404 Not Found", Description: "The requested URL was not found on this server."})
}
}
// fmt.Fprintf(w, "EASFS serving!\n")
}
func RedirectSSL(rsp http.ResponseWriter, req *http.Request) {
rsp.Header().Set("X-Frame-Options", "SAMEORIGIN")
rsp.Header().Set("Server", "easfs")
target := "https://" + req.Host + req.URL.Path
if len(req.URL.RawQuery) > 0 {
target += "?" + req.URL.RawQuery
}
http.Redirect(rsp, req, target,
http.StatusPermanentRedirect)
}
func RedirectDomain(rsp http.ResponseWriter, req *http.Request) {
target := "https://" + flagDomain + req.URL.Path
if len(req.URL.RawQuery) > 0 {
target += "?" + req.URL.RawQuery
}
http.Redirect(rsp, req, target,
http.StatusTemporaryRedirect)
}
var siteStatusHTML = `Site at {{.SitePath}}<br>Site version {{.SiteVersion}}`
var SiteVersion = "Unknown"
func siteStatus(context.Context) interface{} {
data := struct {
SitePath string
SiteVersion string
}{
SitePath: flagSitePath,
SiteVersion: SiteVersion,
}
return data
}
func main() {
AddStatusPart("Site Status", siteStatusHTML, siteStatus)
flag.StringVar(&flagListenAddress, "listen", "0.0.0.0:80", "HTTP listen address")
flag.StringVar(&flagSSLListenAddress, "slisten", "0.0.0.0:443", "HTTPS listen address")
flag.StringVar(&flagSSLCert, "cert", "cert.pem", "HTTPS cert")
flag.StringVar(&flagSSLKey, "key", "key.pem", "HTTPS key")
flag.StringVar(&flagDomain, "domain", "", "Site domain")
flag.StringVar(&flagSitePath, "site", "site/content/", "Path to site content")
flag.BoolVar(&flagProd, "prod", false, "prod env")
flag.Parse()
cmd := exec.Command("git", "describe", "--always", "--match", "v[0-9].*", "--dirty")
cmd.Dir = flagSitePath
out, err := cmd.Output()
if err == nil {
SiteVersion = strings.TrimSpace(string(out))
}
pongo2.RegisterFilter("slugify", Slugify)
go func() {
httperr := http.ListenAndServe(flagListenAddress, http.HandlerFunc(RedirectSSL))
if httperr != nil {
glog.Fatal("ListenAndServe: ", httperr)
}
}()
mux := http.NewServeMux()
fs := http.FileServer(http.Dir("static"))
mux.Handle("/_static/", gziphandler.GzipHandler(http.StripPrefix("/_static/", fs)))
mux.Handle("/", gziphandler.GzipHandler(http.HandlerFunc(EASFSHandler)))
err = http.ListenAndServeTLS(flagSSLListenAddress, flagSSLCert, flagSSLKey, mux)
if err != nil {
glog.Fatal("ListenAndServeTLS: ", err)
}
}