Rename --host => --root-url
This commit is contained in:
parent
3d07acb222
commit
ffeb9a3362
@ -28,10 +28,10 @@ the command line.
|
||||
<h2>Command line API</h2>
|
||||
<pre>
|
||||
# Upload a file
|
||||
curl -F'file=@yourfile.png' <a href="{{.Host}}">{{.Host}}</a>
|
||||
curl -F'file=@yourfile.png' <a href="{{.RootURL}}">{{.RootURL}}</a>
|
||||
|
||||
# Shorten a URL
|
||||
curl -F'shorten=http://example.com/some/long/url' <a href="{{.Host}}">{{.Host}}</a>
|
||||
curl -F'shorten=http://example.com/some/long/url' <a href="{{.RootURL}}">{{.RootURL}}</a>
|
||||
|
||||
# The first line of the result will contain the shortened URL.
|
||||
#
|
||||
@ -39,6 +39,6 @@ the command line.
|
||||
# information on how to delete the shortened object.
|
||||
|
||||
# To upload a file and only extract the shortened URL (i.e. throw away the rest)
|
||||
curl -F'file=@yourfile.png' <a href="{{.Host}}">{{.Host}}</a> | head -n 1
|
||||
curl -F'file=@yourfile.png' <a href="{{.RootURL}}">{{.RootURL}}</a> | head -n 1
|
||||
</pre>
|
||||
{{end}}
|
||||
|
@ -4,10 +4,10 @@
|
||||
|
||||
{{define "body"}}
|
||||
<pre>
|
||||
<a href="{{.Host}}/{{.Paste.Key}}{{.FileExt}}">{{.Host}}/{{.Paste.Key}}{{.FileExt}}</a>
|
||||
<a href="{{.RootURL}}/{{.Paste.Key}}{{.FileExt}}">{{.RootURL}}/{{.Paste.Key}}{{.FileExt}}</a>
|
||||
---
|
||||
{{if and (ne .Paste.State.String "deleted") .CanDeleteBool}}
|
||||
with delete token: <a href="{{.Host}}/{{.Paste.Key}}{{.FileExt}}?deleteToken={{.Paste.DeleteToken}}">{{.Host}}/{{.Paste.Key}}{{.FileExt}}?deleteToken={{.Paste.DeleteToken}}</a>
|
||||
with delete token: <a href="{{.RootURL}}/{{.Paste.Key}}{{.FileExt}}?deleteToken={{.Paste.DeleteToken}}">{{.RootURL}}/{{.Paste.Key}}{{.FileExt}}?deleteToken={{.Paste.DeleteToken}}</a>
|
||||
{{else -}}
|
||||
with delete token: <unknown>
|
||||
{{end -}}
|
||||
@ -22,7 +22,7 @@ delete token: {{.CanDelete}}
|
||||
|
||||
{{if and (ne .Paste.State.String "deleted") .CanDeleteBool}}
|
||||
```
|
||||
curl --request "DELETE" "<a href="{{.Host}}/{{.Paste.Key}}{{.FileExt}}?deleteToken={{.Paste.DeleteToken}}">{{.Host}}/{{.Paste.Key}}{{.FileExt}}?deleteToken={{.Paste.DeleteToken}}"</a>
|
||||
curl --request "DELETE" "<a href="{{.RootURL}}/{{.Paste.Key}}{{.FileExt}}?deleteToken={{.Paste.DeleteToken}}">{{.RootURL}}/{{.Paste.Key}}{{.FileExt}}?deleteToken={{.Paste.DeleteToken}}"</a>
|
||||
```
|
||||
</pre>
|
||||
{{end}}
|
||||
|
@ -7,10 +7,10 @@ the command line.
|
||||
## USAGE
|
||||
|
||||
# Upload a file
|
||||
curl -F'file=@yourfile.png' {{.Host}}
|
||||
curl -F'file=@yourfile.png' {{.RootURL}}
|
||||
|
||||
# Shorten a URL
|
||||
curl -F'shorten=http://example.com/some/long/url' {{.Host}}
|
||||
curl -F'shorten=http://example.com/some/long/url' {{.RootURL}}
|
||||
|
||||
# The first line of the result will contain the shortened URL.
|
||||
#
|
||||
@ -18,4 +18,4 @@ the command line.
|
||||
# information on how to delete the shortened object.
|
||||
|
||||
# To upload a file and only extract the shortened URL (i.e. throw away the rest)
|
||||
curl -F'file=@yourfile.png' {{.Host}} | head -n 1
|
||||
curl -F'file=@yourfile.png' {{.RootURL}} | head -n 1
|
||||
|
@ -1,7 +1,7 @@
|
||||
{{.Host}}/{{.Paste.Key}}{{.FileExt}}
|
||||
{{.RootURL}}/{{.Paste.Key}}{{.FileExt}}
|
||||
---
|
||||
{{if and (ne .Paste.State.String "deleted") .CanDeleteBool}}
|
||||
with delete token: {{.Host}}/{{.Paste.Key}}{{.FileExt}}?deleteToken={{.Request.URL.Query.Get "deleteToken"}}
|
||||
with delete token: {{.RootURL}}/{{.Paste.Key}}{{.FileExt}}?deleteToken={{.Paste.DeleteToken}}
|
||||
{{else -}}
|
||||
with delete token: <unknown>
|
||||
{{end -}}type: {{.Paste.Type}}
|
||||
@ -16,6 +16,6 @@ delete token: {{.CanDelete}}
|
||||
{{if and (ne .Paste.State.String "deleted") .CanDeleteBool}}
|
||||
```
|
||||
# To delete this {{.Paste.Type}}, execute:
|
||||
curl --request "DELETE" "{{.Host}}/{{.Paste.Key}}{{.FileExt}}?deleteToken={{.Paste.DeleteToken}}"
|
||||
curl --request "DELETE" "{{.RootURL}}/{{.Paste.Key}}{{.FileExt}}?deleteToken={{.Paste.DeleteToken}}"
|
||||
```
|
||||
{{end}}
|
||||
|
@ -13,7 +13,7 @@ var (
|
||||
fileStorePath = flag.String("file-store", "", "path to the directory where uploaded files will be stored")
|
||||
httpListen = flag.String("listen", "127.0.0.1:8000", "listen address (host:port)")
|
||||
metricsListen = flag.String("metrics_listen", "127.0.0.1:58614", "listen address for metrics (host:port)")
|
||||
hostURL = flag.String("host", "", "host root (defaults to using the request 'Host' header with HTTPS")
|
||||
rootURL = flag.String("root_url", "", "host root (example: 'https://example.com', uses an educated guess if omitted)")
|
||||
)
|
||||
|
||||
func main() {
|
||||
@ -30,5 +30,5 @@ func main() {
|
||||
defer database.Close()
|
||||
|
||||
go rushlink.StartMetricsServer(*metricsListen, database, filestore)
|
||||
rushlink.StartMainServer(*httpListen, database, filestore, hostURL)
|
||||
rushlink.StartMainServer(*httpListen, database, filestore, *rootURL)
|
||||
}
|
||||
|
28
router.go
28
router.go
@ -19,13 +19,13 @@ const urlKeyExpr = "{key:[A-Za-z0-9-_]{4,}}"
|
||||
const urlKeyWithExtExpr = urlKeyExpr + "{ext:\\.[A-Za-z0-9-_]+}"
|
||||
|
||||
type rushlink struct {
|
||||
db *db.Database
|
||||
fs *db.FileStore
|
||||
host *url.URL
|
||||
db *db.Database
|
||||
fs *db.FileStore
|
||||
rootURL *url.URL
|
||||
}
|
||||
|
||||
func (rl *rushlink) Host() *url.URL {
|
||||
return rl.host
|
||||
func (rl *rushlink) RootURL() *url.URL {
|
||||
return rl.rootURL
|
||||
}
|
||||
|
||||
func (rl *rushlink) recoveryMiddleware(next http.Handler) http.Handler {
|
||||
@ -80,21 +80,19 @@ func (w *statusResponseWriter) WriteHeader(statusCode int) {
|
||||
}
|
||||
|
||||
// StartMainServer starts the main http server listening on addr.
|
||||
func StartMainServer(addr string, db *db.Database, fs *db.FileStore, rawhost *string) {
|
||||
var host *url.URL
|
||||
if rawhost != nil {
|
||||
func StartMainServer(addr string, db *db.Database, fs *db.FileStore, rawRootURL string) {
|
||||
var rootURL *url.URL
|
||||
if rawRootURL != "" {
|
||||
var err error
|
||||
host, err = url.Parse(*rawhost)
|
||||
rootURL, err = url.Parse(rawRootURL)
|
||||
if err != nil {
|
||||
log.Fatalln(errors.Wrap(err, "could not parse host flag"))
|
||||
log.Fatalln(errors.Wrap(err, "could not parse rootURL flag"))
|
||||
}
|
||||
} else {
|
||||
log.Println("warning: the --host flag will be required in the future")
|
||||
}
|
||||
rl := rushlink{
|
||||
db: db,
|
||||
fs: fs,
|
||||
host: host,
|
||||
db: db,
|
||||
fs: fs,
|
||||
rootURL: rootURL,
|
||||
}
|
||||
|
||||
// Initialize Gorilla router
|
||||
|
33
views.go
33
views.go
@ -21,7 +21,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const defaultScheme = "https"
|
||||
const defaultScheme = "http"
|
||||
|
||||
// Plain text templates
|
||||
var textBaseTemplate *text.Template = text.Must(text.New("").Parse(string(MustAsset("templates/txt/base.txt.tmpl"))))
|
||||
@ -107,7 +107,7 @@ func (rl *rushlink) render(w http.ResponseWriter, r *http.Request, tmplName stri
|
||||
}
|
||||
|
||||
// Add the request to the template data
|
||||
mapExtend(data, "Host", rl.resolveHost(r))
|
||||
mapExtend(data, "RootURL", rl.resolveRootURL(r))
|
||||
mapExtend(data, "Request", r)
|
||||
|
||||
switch contentType {
|
||||
@ -165,19 +165,38 @@ func (rl *rushlink) renderInternalServerError(w http.ResponseWriter, r *http.Req
|
||||
rl.renderError(w, r, http.StatusInternalServerError, msg)
|
||||
}
|
||||
|
||||
// resolveHost constructs the `scheme://host` part of rushlinks public API.
|
||||
// resolveRootURL constructs the `scheme://host` part of rushlinks public API.
|
||||
//
|
||||
// If the `--host` flag is set, it will return that URL.
|
||||
// If the `--root_url` flag is set, it will return that URL.
|
||||
// Otherwise, this function will return 'https://{Host}', where `{Host}` is
|
||||
// the value provided by the client in the HTTP `Host` header. This value may
|
||||
// be invalid, but it is impossible to handle this error (because we *cannot*
|
||||
// know the real host).
|
||||
func (rl *rushlink) resolveHost(r *http.Request) string {
|
||||
rlHost := rl.Host()
|
||||
func (rl *rushlink) resolveRootURL(r *http.Request) string {
|
||||
rlHost := rl.RootURL()
|
||||
if rlHost != nil {
|
||||
// Root URL overridden by command line arguments
|
||||
log.Println("override")
|
||||
return rlHost.String()
|
||||
}
|
||||
return defaultScheme + r.Host
|
||||
|
||||
// Guess scheme
|
||||
scheme := defaultScheme
|
||||
forwardedScheme := r.Header.Get("X-Forwarded-Proto")
|
||||
switch forwardedScheme {
|
||||
case "http":
|
||||
scheme = "http"
|
||||
break
|
||||
case "https":
|
||||
scheme = "https"
|
||||
break
|
||||
}
|
||||
// Guess host
|
||||
host := r.Host
|
||||
if forwardedHost := r.Header.Get("X-Forwarded-Host"); forwardedHost != "" {
|
||||
host = forwardedHost
|
||||
}
|
||||
return scheme + "://" + host
|
||||
}
|
||||
|
||||
// Try to resolve the preferred content-type for the response to this request.
|
||||
|
Loading…
Reference in New Issue
Block a user