From 4856ab07509848e56b9c78d2d42410dcdb5e415f Mon Sep 17 00:00:00 2001 From: andreimarcu Date: Fri, 30 Oct 2015 18:36:47 -0400 Subject: [PATCH] Allow for non-/ deployments. Fixes #61 --- README.md | 4 +++ fileserve.go | 4 +-- server.go | 54 +++++++++++++++++++++--------------- templates/404.html | 2 +- templates/API.html | 2 +- templates/base.html | 12 ++++---- templates/display/audio.html | 4 +-- templates/display/base.html | 2 +- templates/display/bin.html | 12 ++++---- templates/display/file.html | 2 +- templates/display/image.html | 4 +-- templates/display/md.html | 2 +- templates/display/pdf.html | 4 +-- templates/display/story.html | 8 +++--- templates/display/video.html | 4 +-- templates/index.html | 8 +++--- templates/paste.html | 8 +++--- upload.go | 6 ++-- 18 files changed, 78 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index f92a11e..98f5718 100644 --- a/README.md +++ b/README.md @@ -69,8 +69,11 @@ remoteuploads = true A helper utility ```linx-genkey``` is provided which hashes keys to the format required in the auth files. + Deployment ---------- +Linx-server supports being deployed in a subdirectory (ie. example.com/mylinx/) as well as on its own (example.com/). + #### 1. Using fastcgi @@ -98,6 +101,7 @@ Run linx-server with the ```-certfile path/to/cert.file``` and ```-keyfile path/ #### 3. Using the built-in http server Run linx-server normally. + Development ----------- Any help is welcome, PRs will be reviewed and merged accordingly. diff --git a/fileserve.go b/fileserve.go index 8c49278..7783175 100644 --- a/fileserve.go +++ b/fileserve.go @@ -45,10 +45,10 @@ func staticHandler(c web.C, w http.ResponseWriter, r *http.Request) { return } else { if path == "/favicon.ico" { - path = "/static/images/favicon.gif" + path = Config.sitePath + "/static/images/favicon.gif" } - filePath := strings.TrimPrefix(path, "/static/") + filePath := strings.TrimPrefix(path, Config.sitePath+"static/") file, err := staticBox.Open(filePath) if err != nil { notFoundHandler(c, w, r) diff --git a/server.go b/server.go index c00b2c1..9a1bc8c 100644 --- a/server.go +++ b/server.go @@ -6,6 +6,7 @@ import ( "net" "net/http" "net/http/fcgi" + "net/url" "os" "regexp" "strconv" @@ -25,6 +26,7 @@ var Config struct { metaDir string siteName string siteURL string + sitePath string certFile string keyFile string contentSecurityPolicy string @@ -91,6 +93,13 @@ func setup() *web.Mux { Config.siteURL = Config.siteURL + "/" } + parsedUrl, err := url.Parse(Config.siteURL) + if err != nil { + log.Fatal("Could not parse siteurl:", err) + } + + Config.sitePath = parsedUrl.Path + // Template setup p2l, err := NewPongo2TemplatesLoader() if err != nil { @@ -99,6 +108,7 @@ func setup() *web.Mux { TemplateSet := pongo2.NewSet("templates", p2l) TemplateSet.Globals["sitename"] = Config.siteName TemplateSet.Globals["siteurl"] = Config.siteURL + TemplateSet.Globals["sitepath"] = Config.sitePath TemplateSet.Globals["using_auth"] = Config.authFile != "" err = populateTemplatesMap(TemplateSet, Templates) if err != nil { @@ -110,43 +120,43 @@ func setup() *web.Mux { timeStartedStr = strconv.FormatInt(timeStarted.Unix(), 10) // Routing setup - nameRe := regexp.MustCompile(`^/(?P[a-z0-9-\.]+)$`) - selifRe := regexp.MustCompile(`^/selif/(?P[a-z0-9-\.]+)$`) - selifIndexRe := regexp.MustCompile(`^/selif/$`) - torrentRe := regexp.MustCompile(`^/(?P[a-z0-9-\.]+)/torrent$`) + nameRe := regexp.MustCompile("^" + Config.sitePath + `(?P[a-z0-9-\.]+)$`) + selifRe := regexp.MustCompile("^" + Config.sitePath + `selif/(?P[a-z0-9-\.]+)$`) + selifIndexRe := regexp.MustCompile("^" + Config.sitePath + `selif/$`) + torrentRe := regexp.MustCompile("^" + Config.sitePath + `(?P[a-z0-9-\.]+)/torrent$`) if Config.authFile == "" { - mux.Get("/", indexHandler) - mux.Get("/paste/", pasteHandler) + mux.Get(Config.sitePath, indexHandler) + mux.Get(Config.sitePath+"paste/", pasteHandler) } else { - mux.Get("/", http.RedirectHandler("/API", 303)) - mux.Get("/paste/", http.RedirectHandler("/API/", 303)) + mux.Get(Config.sitePath, http.RedirectHandler(Config.sitePath+"API", 303)) + mux.Get(Config.sitePath+"paste/", http.RedirectHandler(Config.sitePath+"API/", 303)) } - mux.Get("/paste", http.RedirectHandler("/paste/", 301)) + mux.Get(Config.sitePath+"paste", http.RedirectHandler(Config.sitePath+"paste/", 301)) - mux.Get("/API/", apiDocHandler) - mux.Get("/API", http.RedirectHandler("/API/", 301)) + mux.Get(Config.sitePath+"API/", apiDocHandler) + mux.Get(Config.sitePath+"API", http.RedirectHandler(Config.sitePath+"API/", 301)) if Config.remoteUploads { - mux.Get("/upload", uploadRemote) - mux.Get("/upload/", uploadRemote) + mux.Get(Config.sitePath+"upload", uploadRemote) + mux.Get(Config.sitePath+"upload/", uploadRemote) if Config.remoteAuthFile != "" { remoteAuthKeys = readAuthKeys(Config.remoteAuthFile) } } - mux.Post("/upload", uploadPostHandler) - mux.Post("/upload/", uploadPostHandler) - mux.Put("/upload", uploadPutHandler) - mux.Put("/upload/", uploadPutHandler) - mux.Put("/upload/:name", uploadPutHandler) + mux.Post(Config.sitePath+"upload", uploadPostHandler) + mux.Post(Config.sitePath+"upload/", uploadPostHandler) + mux.Put(Config.sitePath+"upload", uploadPutHandler) + mux.Put(Config.sitePath+"upload/", uploadPutHandler) + mux.Put(Config.sitePath+"upload/:name", uploadPutHandler) - mux.Delete("/:name", deleteHandler) + mux.Delete(Config.sitePath+":name", deleteHandler) - mux.Get("/static/*", staticHandler) - mux.Get("/favicon.ico", staticHandler) - mux.Get("/robots.txt", staticHandler) + mux.Get(Config.sitePath+"static/*", staticHandler) + mux.Get(Config.sitePath+"favicon.ico", staticHandler) + mux.Get(Config.sitePath+"robots.txt", staticHandler) mux.Get(nameRe, fileDisplayHandler) mux.Get(selifRe, fileServeHandler) mux.Get(selifIndexRe, unauthorizedHandler) diff --git a/templates/404.html b/templates/404.html index e792bf3..c1728e5 100644 --- a/templates/404.html +++ b/templates/404.html @@ -1,5 +1,5 @@ {% extends "base.html" %} {% block content %} - + {% endblock %} diff --git a/templates/API.html b/templates/API.html index a58b1ac..f46af5e 100644 --- a/templates/API.html +++ b/templates/API.html @@ -1,7 +1,7 @@ {% extends "base.html" %} {% block head %} - + {% endblock %} {% block content %} diff --git a/templates/base.html b/templates/base.html index e171516..b4aea86 100644 --- a/templates/base.html +++ b/templates/base.html @@ -3,8 +3,8 @@ {% block title %}{{ sitename }}{% endblock %} - - + + {% block head %}{% endblock %} @@ -14,12 +14,12 @@ {% block content %}{% endblock %} diff --git a/templates/display/audio.html b/templates/display/audio.html index a88319f..689bab3 100644 --- a/templates/display/audio.html +++ b/templates/display/audio.html @@ -2,8 +2,8 @@ {% block main %} {% endblock %} diff --git a/templates/display/base.html b/templates/display/base.html index 1a36b4d..1c595b8 100644 --- a/templates/display/base.html +++ b/templates/display/base.html @@ -18,7 +18,7 @@ {% block infomore %}{% endblock %} {{ size }} | torrent | - get + get {% block infoleft %}{% endblock %} diff --git a/templates/display/bin.html b/templates/display/bin.html index a3b85fd..bfa66c2 100644 --- a/templates/display/bin.html +++ b/templates/display/bin.html @@ -1,14 +1,14 @@ {% extends "base.html" %} {% block head %} - + {% endblock %} {% block innercontentmore %} class="scrollable"{% endblock %} {% block infoleft %}
-
+
@@ -43,6 +43,6 @@
- - + + {% endblock %} diff --git a/templates/display/video.html b/templates/display/video.html index 77c2a60..9fc90d5 100644 --- a/templates/display/video.html +++ b/templates/display/video.html @@ -2,7 +2,7 @@ {% block main %} {% endblock %} diff --git a/templates/index.html b/templates/index.html index 9262cf3..240a17d 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,12 +1,12 @@ {% extends "base.html" %} {% block head %} - + {% endblock %} {% block content %}
- +

@@ -39,6 +39,6 @@
- - + + {% endblock %} diff --git a/templates/paste.html b/templates/paste.html index 1b31aac..3d71c74 100644 --- a/templates/paste.html +++ b/templates/paste.html @@ -1,11 +1,11 @@ {% extends "base.html" %} {% block head %} - + {% endblock %} {% block content %} - +
. @@ -34,6 +34,6 @@
- - + + {% endblock %} diff --git a/upload.go b/upload.go index c1bba9e..33efd87 100644 --- a/upload.go +++ b/upload.go @@ -103,7 +103,7 @@ func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) { return } - http.Redirect(w, r, "/"+upload.Filename, 303) + http.Redirect(w, r, Config.sitePath+upload.Filename, 303) } } @@ -147,7 +147,7 @@ func uploadRemote(c web.C, w http.ResponseWriter, r *http.Request) { } if r.FormValue("url") == "" { - http.Redirect(w, r, "/", 303) + http.Redirect(w, r, Config.sitePath, 303) return } @@ -183,7 +183,7 @@ func uploadRemote(c web.C, w http.ResponseWriter, r *http.Request) { return } - http.Redirect(w, r, "/"+upload.Filename, 303) + http.Redirect(w, r, Config.sitePath+upload.Filename, 303) } }