Allow for non-/ deployments. Fixes #61

This commit is contained in:
andreimarcu 2015-10-30 18:36:47 -04:00
parent 07aaad2cd8
commit 4856ab0750
18 changed files with 78 additions and 64 deletions

View File

@ -69,8 +69,11 @@ remoteuploads = true
A helper utility ```linx-genkey``` is provided which hashes keys to the format required in the auth files. A helper utility ```linx-genkey``` is provided which hashes keys to the format required in the auth files.
Deployment Deployment
---------- ----------
Linx-server supports being deployed in a subdirectory (ie. example.com/mylinx/) as well as on its own (example.com/).
#### 1. Using fastcgi #### 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 #### 3. Using the built-in http server
Run linx-server normally. Run linx-server normally.
Development Development
----------- -----------
Any help is welcome, PRs will be reviewed and merged accordingly. Any help is welcome, PRs will be reviewed and merged accordingly.

View File

@ -45,10 +45,10 @@ func staticHandler(c web.C, w http.ResponseWriter, r *http.Request) {
return return
} else { } else {
if path == "/favicon.ico" { 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) file, err := staticBox.Open(filePath)
if err != nil { if err != nil {
notFoundHandler(c, w, r) notFoundHandler(c, w, r)

View File

@ -6,6 +6,7 @@ import (
"net" "net"
"net/http" "net/http"
"net/http/fcgi" "net/http/fcgi"
"net/url"
"os" "os"
"regexp" "regexp"
"strconv" "strconv"
@ -25,6 +26,7 @@ var Config struct {
metaDir string metaDir string
siteName string siteName string
siteURL string siteURL string
sitePath string
certFile string certFile string
keyFile string keyFile string
contentSecurityPolicy string contentSecurityPolicy string
@ -91,6 +93,13 @@ func setup() *web.Mux {
Config.siteURL = Config.siteURL + "/" 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 // Template setup
p2l, err := NewPongo2TemplatesLoader() p2l, err := NewPongo2TemplatesLoader()
if err != nil { if err != nil {
@ -99,6 +108,7 @@ func setup() *web.Mux {
TemplateSet := pongo2.NewSet("templates", p2l) TemplateSet := pongo2.NewSet("templates", p2l)
TemplateSet.Globals["sitename"] = Config.siteName TemplateSet.Globals["sitename"] = Config.siteName
TemplateSet.Globals["siteurl"] = Config.siteURL TemplateSet.Globals["siteurl"] = Config.siteURL
TemplateSet.Globals["sitepath"] = Config.sitePath
TemplateSet.Globals["using_auth"] = Config.authFile != "" TemplateSet.Globals["using_auth"] = Config.authFile != ""
err = populateTemplatesMap(TemplateSet, Templates) err = populateTemplatesMap(TemplateSet, Templates)
if err != nil { if err != nil {
@ -110,43 +120,43 @@ func setup() *web.Mux {
timeStartedStr = strconv.FormatInt(timeStarted.Unix(), 10) timeStartedStr = strconv.FormatInt(timeStarted.Unix(), 10)
// Routing setup // Routing setup
nameRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)$`) nameRe := regexp.MustCompile("^" + Config.sitePath + `(?P<name>[a-z0-9-\.]+)$`)
selifRe := regexp.MustCompile(`^/selif/(?P<name>[a-z0-9-\.]+)$`) selifRe := regexp.MustCompile("^" + Config.sitePath + `selif/(?P<name>[a-z0-9-\.]+)$`)
selifIndexRe := regexp.MustCompile(`^/selif/$`) selifIndexRe := regexp.MustCompile("^" + Config.sitePath + `selif/$`)
torrentRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)/torrent$`) torrentRe := regexp.MustCompile("^" + Config.sitePath + `(?P<name>[a-z0-9-\.]+)/torrent$`)
if Config.authFile == "" { if Config.authFile == "" {
mux.Get("/", indexHandler) mux.Get(Config.sitePath, indexHandler)
mux.Get("/paste/", pasteHandler) mux.Get(Config.sitePath+"paste/", pasteHandler)
} else { } else {
mux.Get("/", http.RedirectHandler("/API", 303)) mux.Get(Config.sitePath, http.RedirectHandler(Config.sitePath+"API", 303))
mux.Get("/paste/", http.RedirectHandler("/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(Config.sitePath+"API/", apiDocHandler)
mux.Get("/API", http.RedirectHandler("/API/", 301)) mux.Get(Config.sitePath+"API", http.RedirectHandler(Config.sitePath+"API/", 301))
if Config.remoteUploads { if Config.remoteUploads {
mux.Get("/upload", uploadRemote) mux.Get(Config.sitePath+"upload", uploadRemote)
mux.Get("/upload/", uploadRemote) mux.Get(Config.sitePath+"upload/", uploadRemote)
if Config.remoteAuthFile != "" { if Config.remoteAuthFile != "" {
remoteAuthKeys = readAuthKeys(Config.remoteAuthFile) remoteAuthKeys = readAuthKeys(Config.remoteAuthFile)
} }
} }
mux.Post("/upload", uploadPostHandler) mux.Post(Config.sitePath+"upload", uploadPostHandler)
mux.Post("/upload/", uploadPostHandler) mux.Post(Config.sitePath+"upload/", uploadPostHandler)
mux.Put("/upload", uploadPutHandler) mux.Put(Config.sitePath+"upload", uploadPutHandler)
mux.Put("/upload/", uploadPutHandler) mux.Put(Config.sitePath+"upload/", uploadPutHandler)
mux.Put("/upload/:name", uploadPutHandler) mux.Put(Config.sitePath+"upload/:name", uploadPutHandler)
mux.Delete("/:name", deleteHandler) mux.Delete(Config.sitePath+":name", deleteHandler)
mux.Get("/static/*", staticHandler) mux.Get(Config.sitePath+"static/*", staticHandler)
mux.Get("/favicon.ico", staticHandler) mux.Get(Config.sitePath+"favicon.ico", staticHandler)
mux.Get("/robots.txt", staticHandler) mux.Get(Config.sitePath+"robots.txt", staticHandler)
mux.Get(nameRe, fileDisplayHandler) mux.Get(nameRe, fileDisplayHandler)
mux.Get(selifRe, fileServeHandler) mux.Get(selifRe, fileServeHandler)
mux.Get(selifIndexRe, unauthorizedHandler) mux.Get(selifIndexRe, unauthorizedHandler)

View File

@ -1,5 +1,5 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block content %} {% block content %}
<a href="/"><img src='/static/images/404.jpg'></a> <a href="{{ sitepath }}"><img src='{{ sitepath }}static/images/404.jpg'></a>
{% endblock %} {% endblock %}

View File

@ -1,7 +1,7 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block head %} {% block head %}
<link href="/static/css/github-markdown.css" rel="stylesheet" type="text/css"> <link href="{{ sitepath }}static/css/github-markdown.css" rel="stylesheet" type="text/css">
{% endblock %} {% endblock %}
{% block content %} {% block content %}

View File

@ -3,8 +3,8 @@
<head> <head>
<title>{% block title %}{{ sitename }}{% endblock %}</title> <title>{% block title %}{{ sitename }}{% endblock %}</title>
<meta charset='utf-8' content='text/html' http-equiv='content-type'> <meta charset='utf-8' content='text/html' http-equiv='content-type'>
<link href='/static/css/linx.css' media='screen, projection' rel='stylesheet' type='text/css'> <link href='{{ sitepath }}static/css/linx.css' media='screen, projection' rel='stylesheet' type='text/css'>
<link href='/static/images/favicon.gif' rel='icon' type='image/gif'> <link href='{{ sitepath }}static/images/favicon.gif' rel='icon' type='image/gif'>
{% block head %}{% endblock %} {% block head %}{% endblock %}
</head> </head>
@ -14,12 +14,12 @@
<div id="header"> <div id="header">
<div id="navigation" class="right"> <div id="navigation" class="right">
{% if !using_auth %} {% if !using_auth %}
<a href="/">Upload</a> | <a href="{{ sitepath }}">Upload</a> |
<a href="/paste/">Paste</a> | <a href="{{ sitepath }}paste/">Paste</a> |
{% endif %} {% endif %}
<a href="/API/">API</a> <a href="{{ sitepath }}API/">API</a>
</div> </div>
<h2><a href="/" title="{{ sitename }}">{{ sitename }}</a></h2> <h2><a href="{{ sitepath }}" title="{{ sitename }}">{{ sitename }}</a></h2>
</div> </div>
{% block content %}{% endblock %} {% block content %}{% endblock %}

View File

@ -2,8 +2,8 @@
{% block main %} {% block main %}
<audio class="display-audio" controls preload='auto'> <audio class="display-audio" controls preload='auto'>
<source src='/selif/{{ filename }}'> <source src='{{ sitepath }}selif/{{ filename }}'>
<a href='/selif/{{ filename }}'>Download it instead</a> <a href='{{ sitepath }}selif/{{ filename }}'>Download it instead</a>
</audio> </audio>
{% endblock %} {% endblock %}

View File

@ -18,7 +18,7 @@
{% block infomore %}{% endblock %} {% block infomore %}{% endblock %}
<span>{{ size }}</span> | <span>{{ size }}</span> |
<a href="{{ filename }}/torrent" download>torrent</a> | <a href="{{ filename }}/torrent" download>torrent</a> |
<a href="/selif/{{ filename }}" download>get</a> <a href="{{ sitepath }}selif/{{ filename }}" download>get</a>
</div> </div>
{% block infoleft %}{% endblock %} {% block infoleft %}{% endblock %}

View File

@ -1,14 +1,14 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block head %} {% block head %}
<link href="/static/css/highlight/tomorrow.css" rel="stylesheet" type="text/css"> <link href="{{ sitepath }}static/css/highlight/tomorrow.css" rel="stylesheet" type="text/css">
{% endblock %} {% endblock %}
{% block innercontentmore %} class="scrollable"{% endblock %} {% block innercontentmore %} class="scrollable"{% endblock %}
{% block infoleft %} {% block infoleft %}
<div id="editform"> <div id="editform">
<form id="reply" action='/upload' method='post' > <form id="reply" action='{{ sitepath }}upload' method='post' >
<div class="right"> <div class="right">
<select id="expiry" name="expires"> <select id="expiry" name="expires">
<option disabled=disabled>Expires:</option> <option disabled=disabled>Expires:</option>
@ -43,10 +43,10 @@
{% if extra.lang_hl != "text" %} {% if extra.lang_hl != "text" %}
<script src="/static/js/highlight/highlight.pack.js"></script> <script src="{{ sitepath }}static/js/highlight/highlight.pack.js"></script>
<script src="/static/js/bin_hljs.js"></script> <script src="{{ sitepath }}static/js/bin_hljs.js"></script>
{% endif %} {% endif %}
<script src="/static/js/util.js"></script> <script src="{{ sitepath }}static/js/util.js"></script>
<script src="/static/js/bin.js"></script> <script src="{{ sitepath }}static/js/bin.js"></script>
{% endblock %} {% endblock %}

View File

@ -2,7 +2,7 @@
{% block main %} {% block main %}
<div class="normal display-file"> <div class="normal display-file">
<p class="center">You are requesting <a href="/selif/{{ filename }}">{{ filename }}</a>, <a href="/selif/{{ filename }}">click here</a> to download.</p> <p class="center">You are requesting <a href="{{ sitepath }}selif/{{ filename }}">{{ filename }}</a>, <a href="{{ sitepath }}selif/{{ filename }}">click here</a> to download.</p>
{% if files|length > 0 %} {% if files|length > 0 %}
<p>Contents of the archive:</p> <p>Contents of the archive:</p>

View File

@ -1,7 +1,7 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block main %} {% block main %}
<a href="/selif/{{ filename }}"> <a href="{{ sitepath }}selif/{{ filename }}">
<img class="display-image" src="/selif/{{ filename }}" /> <img class="display-image" src="{{ sitepath }}selif/{{ filename }}" />
</a> </a>
{% endblock %} {% endblock %}

View File

@ -1,7 +1,7 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block head %} {% block head %}
<link href="/static/css/github-markdown.css" rel="stylesheet" type="text/css"> <link href="{{ sitepath }}static/css/github-markdown.css" rel="stylesheet" type="text/css">
{% endblock %} {% endblock %}
{% block main %} {% block main %}

View File

@ -1,10 +1,10 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block main %} {% block main %}
<object class="display-pdf" data="/selif/{{ filename }}" type="application/pdf"> <object class="display-pdf" data="{{ sitepath }}selif/{{ filename }}" type="application/pdf">
<p>It appears your Web browser is not configured to display PDF files. <p>It appears your Web browser is not configured to display PDF files.
No worries, just <a href="/selif/{{ filename }}">click here to download the PDF file.</a></p> No worries, just <a href="{{ sitepath }}selif/{{ filename }}">click here to download the PDF file.</a></p>
</object> </object>
{% endblock %} {% endblock %}

View File

@ -1,7 +1,7 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block head %} {% block head %}
<link href="/static/css/story.css" rel="stylesheet" type="text/css"> <link href="{{ sitepath }}static/css/story.css" rel="stylesheet" type="text/css">
{% endblock %} {% endblock %}
{% block innercontentmore %} class="scrollable"{% endblock %} {% block innercontentmore %} class="scrollable"{% endblock %}
@ -9,7 +9,7 @@
{% block infoleft %} {% block infoleft %}
<div id="editform"> <div id="editform">
<form id="reply" action='/upload' method='post' > <form id="reply" action='{{ sitepath }}upload' method='post' >
<div class="right"> <div class="right">
<select id="expiry" name="expires"> <select id="expiry" name="expires">
<option disabled=disabled>Expires:</option> <option disabled=disabled>Expires:</option>
@ -43,6 +43,6 @@
</div> </div>
<script src="/static/js/util.js"></script> <script src="{{ sitepath }}static/js/util.js"></script>
<script src="/static/js/bin.js"></script> <script src="{{ sitepath }}static/js/bin.js"></script>
{% endblock %} {% endblock %}

View File

@ -2,7 +2,7 @@
{% block main %} {% block main %}
<video class="display-video" controls autoplay> <video class="display-video" controls autoplay>
<source src="/selif/{{ filename }}"/> <source src="{{ sitepath }}selif/{{ filename }}"/>
<a href='/selif/{{ filename }}'>Download it instead</a> <a href='{{ sitepath }}selif/{{ filename }}'>Download it instead</a>
</video> </video>
{% endblock %} {% endblock %}

View File

@ -1,12 +1,12 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block head %} {% block head %}
<link href='/static/css/dropzone.css' media='screen, projection' rel='stylesheet' type='text/css'> <link href='{{ sitepath }}static/css/dropzone.css' media='screen, projection' rel='stylesheet' type='text/css'>
{% endblock %} {% endblock %}
{% block content %} {% block content %}
<div id="fileupload"> <div id="fileupload">
<form action="/upload" class="dropzone" id="dropzone" method="POST" enctype="multipart/form-data" data-maxsize="{{ maxsize }}"> <form action="{{ sitepath }}upload" class="dropzone" id="dropzone" method="POST" enctype="multipart/form-data" data-maxsize="{{ maxsize }}">
<div class="fallback"> <div class="fallback">
<input id="fileinput" name="file" type="file" /><br /> <input id="fileinput" name="file" type="file" /><br />
<input id="submitbtn" type="submit" value="Upload"> <input id="submitbtn" type="submit" value="Upload">
@ -39,6 +39,6 @@
<div class="clear"></div> <div class="clear"></div>
</div> </div>
<script src="/static/js/dropzone.js"></script> <script src="{{ sitepath }}static/js/dropzone.js"></script>
<script src="/static/js/upload.js"></script> <script src="{{ sitepath }}static/js/upload.js"></script>
{% endblock %} {% endblock %}

View File

@ -1,11 +1,11 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block head %} {% block head %}
<link href="/static/css/hint.css" rel="stylesheet" type="text/css"> <link href="{{ sitepath }}static/css/hint.css" rel="stylesheet" type="text/css">
{% endblock %} {% endblock %}
{% block content %} {% block content %}
<form id="reply" action='/upload' method='post'> <form id="reply" action='{{ sitepath }}upload' method='post'>
<div id="main"> <div id="main">
<div id="info" class="ninfo"> <div id="info" class="ninfo">
<input class="codebox" name='filename' id="filename" type='text' value="" placeholder="filename (empty for random filename)" />.<span class="hint--top hint--bounce" data-hint="Enable syntax highlighting by adding the extension"><input id="extension" class="codebox" name='extension' type='text' value="" placeholder="txt" /></span> <input class="codebox" name='filename' id="filename" type='text' value="" placeholder="filename (empty for random filename)" />.<span class="hint--top hint--bounce" data-hint="Enable syntax highlighting by adding the extension"><input id="extension" class="codebox" name='extension' type='text' value="" placeholder="txt" /></span>
@ -34,6 +34,6 @@
</div> </div>
</form> </form>
<script src="/static/js/util.js"></script> <script src="{{ sitepath }}static/js/util.js"></script>
<script src="/static/js/paste.js"></script> <script src="{{ sitepath }}static/js/paste.js"></script>
{% endblock %} {% endblock %}

View File

@ -103,7 +103,7 @@ func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) {
return 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") == "" { if r.FormValue("url") == "" {
http.Redirect(w, r, "/", 303) http.Redirect(w, r, Config.sitePath, 303)
return return
} }
@ -183,7 +183,7 @@ func uploadRemote(c web.C, w http.ResponseWriter, r *http.Request) {
return return
} }
http.Redirect(w, r, "/"+upload.Filename, 303) http.Redirect(w, r, Config.sitePath+upload.Filename, 303)
} }
} }