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>
|
<h2>Command line API</h2>
|
||||||
<pre>
|
<pre>
|
||||||
# Upload a file
|
# 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
|
# 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.
|
# 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.
|
# information on how to delete the shortened object.
|
||||||
|
|
||||||
# To upload a file and only extract the shortened URL (i.e. throw away the rest)
|
# 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>
|
</pre>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
@ -4,10 +4,10 @@
|
|||||||
|
|
||||||
{{define "body"}}
|
{{define "body"}}
|
||||||
<pre>
|
<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}}
|
{{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 -}}
|
{{else -}}
|
||||||
with delete token: <unknown>
|
with delete token: <unknown>
|
||||||
{{end -}}
|
{{end -}}
|
||||||
@ -22,7 +22,7 @@ delete token: {{.CanDelete}}
|
|||||||
|
|
||||||
{{if and (ne .Paste.State.String "deleted") .CanDeleteBool}}
|
{{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>
|
</pre>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
@ -7,10 +7,10 @@ the command line.
|
|||||||
## USAGE
|
## USAGE
|
||||||
|
|
||||||
# Upload a file
|
# Upload a file
|
||||||
curl -F'file=@yourfile.png' {{.Host}}
|
curl -F'file=@yourfile.png' {{.RootURL}}
|
||||||
|
|
||||||
# Shorten a URL
|
# 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.
|
# 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.
|
# information on how to delete the shortened object.
|
||||||
|
|
||||||
# To upload a file and only extract the shortened URL (i.e. throw away the rest)
|
# 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}}
|
{{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 -}}
|
{{else -}}
|
||||||
with delete token: <unknown>
|
with delete token: <unknown>
|
||||||
{{end -}}type: {{.Paste.Type}}
|
{{end -}}type: {{.Paste.Type}}
|
||||||
@ -16,6 +16,6 @@ delete token: {{.CanDelete}}
|
|||||||
{{if and (ne .Paste.State.String "deleted") .CanDeleteBool}}
|
{{if and (ne .Paste.State.String "deleted") .CanDeleteBool}}
|
||||||
```
|
```
|
||||||
# To delete this {{.Paste.Type}}, execute:
|
# 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}}
|
{{end}}
|
||||||
|
@ -13,7 +13,7 @@ var (
|
|||||||
fileStorePath = flag.String("file-store", "", "path to the directory where uploaded files will be stored")
|
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)")
|
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)")
|
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() {
|
func main() {
|
||||||
@ -30,5 +30,5 @@ func main() {
|
|||||||
defer database.Close()
|
defer database.Close()
|
||||||
|
|
||||||
go rushlink.StartMetricsServer(*metricsListen, database, filestore)
|
go rushlink.StartMetricsServer(*metricsListen, database, filestore)
|
||||||
rushlink.StartMainServer(*httpListen, database, filestore, hostURL)
|
rushlink.StartMainServer(*httpListen, database, filestore, *rootURL)
|
||||||
}
|
}
|
||||||
|
20
router.go
20
router.go
@ -21,11 +21,11 @@ const urlKeyWithExtExpr = urlKeyExpr + "{ext:\\.[A-Za-z0-9-_]+}"
|
|||||||
type rushlink struct {
|
type rushlink struct {
|
||||||
db *db.Database
|
db *db.Database
|
||||||
fs *db.FileStore
|
fs *db.FileStore
|
||||||
host *url.URL
|
rootURL *url.URL
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rl *rushlink) Host() *url.URL {
|
func (rl *rushlink) RootURL() *url.URL {
|
||||||
return rl.host
|
return rl.rootURL
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rl *rushlink) recoveryMiddleware(next http.Handler) http.Handler {
|
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.
|
// StartMainServer starts the main http server listening on addr.
|
||||||
func StartMainServer(addr string, db *db.Database, fs *db.FileStore, rawhost *string) {
|
func StartMainServer(addr string, db *db.Database, fs *db.FileStore, rawRootURL string) {
|
||||||
var host *url.URL
|
var rootURL *url.URL
|
||||||
if rawhost != nil {
|
if rawRootURL != "" {
|
||||||
var err error
|
var err error
|
||||||
host, err = url.Parse(*rawhost)
|
rootURL, err = url.Parse(rawRootURL)
|
||||||
if err != nil {
|
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{
|
rl := rushlink{
|
||||||
db: db,
|
db: db,
|
||||||
fs: fs,
|
fs: fs,
|
||||||
host: host,
|
rootURL: rootURL,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize Gorilla router
|
// Initialize Gorilla router
|
||||||
|
33
views.go
33
views.go
@ -21,7 +21,7 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultScheme = "https"
|
const defaultScheme = "http"
|
||||||
|
|
||||||
// Plain text templates
|
// Plain text templates
|
||||||
var textBaseTemplate *text.Template = text.Must(text.New("").Parse(string(MustAsset("templates/txt/base.txt.tmpl"))))
|
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
|
// Add the request to the template data
|
||||||
mapExtend(data, "Host", rl.resolveHost(r))
|
mapExtend(data, "RootURL", rl.resolveRootURL(r))
|
||||||
mapExtend(data, "Request", r)
|
mapExtend(data, "Request", r)
|
||||||
|
|
||||||
switch contentType {
|
switch contentType {
|
||||||
@ -165,19 +165,38 @@ func (rl *rushlink) renderInternalServerError(w http.ResponseWriter, r *http.Req
|
|||||||
rl.renderError(w, r, http.StatusInternalServerError, msg)
|
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
|
// Otherwise, this function will return 'https://{Host}', where `{Host}` is
|
||||||
// the value provided by the client in the HTTP `Host` header. This value may
|
// 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*
|
// be invalid, but it is impossible to handle this error (because we *cannot*
|
||||||
// know the real host).
|
// know the real host).
|
||||||
func (rl *rushlink) resolveHost(r *http.Request) string {
|
func (rl *rushlink) resolveRootURL(r *http.Request) string {
|
||||||
rlHost := rl.Host()
|
rlHost := rl.RootURL()
|
||||||
if rlHost != nil {
|
if rlHost != nil {
|
||||||
|
// Root URL overridden by command line arguments
|
||||||
|
log.Println("override")
|
||||||
return rlHost.String()
|
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.
|
// Try to resolve the preferred content-type for the response to this request.
|
||||||
|
Loading…
Reference in New Issue
Block a user