add URI prefix

This commit is contained in:
Kaczanowski Mateusz 2020-09-03 01:16:30 +02:00
parent 4000e8860a
commit affd538bd4
4 changed files with 41 additions and 18 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "pastebin"
version = "0.1.0"
version = "0.1.1"
authors = ["Kaczanowski Mateusz <kaczanowski.mateusz@gmail.com>"]
edition = "2018"

View file

@ -25,6 +25,7 @@ use api_generated::api::get_root_as_entry;
use std::collections::HashMap;
use std::io;
use std::io::Cursor;
use std::path::Path;
use rocket::config::{Config, Environment};
use rocket::http::{ContentType, Status};
@ -248,6 +249,13 @@ struct PastebinConfig {
help = "Override default URI",
)]
uri: Option<String>,
#[structopt(
long = "uri-prefix",
help = "Prefix appended to the URI (ie. '/pastebin')",
default_value = ""
)]
uri_prefix: String,
}
fn get_url(cfg: &PastebinConfig) -> String {
@ -274,10 +282,15 @@ fn get_url(cfg: &PastebinConfig) -> String {
}
}
fn get_error_response<'r>(html: String, status: Status) -> Response<'r> {
fn get_error_response<'r>(
uri_prefix: String,
html: String,
status: Status,
) -> Response<'r> {
let map = btreemap! {
"version" => VERSION,
"is_error" => "true",
"uri_prefix" => uri_prefix.as_str(),
};
let content = Handlebars::new()
@ -329,6 +342,7 @@ fn get<'r>(
lang: Option<String>,
state: State<'r, DB>,
resources: State<'r, HashMap<&str, &[u8]>>,
cfg: State<PastebinConfig>,
) -> Response<'r> {
let html = String::from_utf8_lossy(resources.get("../static/index.html").unwrap()).to_string();
@ -344,6 +358,7 @@ fn get<'r>(
let map = btreemap! {
"version" => VERSION,
"is_error" => "true",
"uri_prefix" => cfg.uri_prefix.as_str(),
};
let content = Handlebars::new()
@ -369,6 +384,7 @@ fn get<'r>(
"pastebin_id" => id,
"pastebin_language" => selected_lang,
"version" => VERSION.to_string(),
"uri_prefix" => cfg.uri_prefix.clone(),
};
if entry.burn() {
@ -404,6 +420,7 @@ fn get<'r>(
#[get("/new?<id>&<level>&<msg>&<glyph>&<url>")]
fn get_new<'r>(
state: State<'r, DB>,
cfg: State<PastebinConfig>,
resources: State<'r, HashMap<&str, &[u8]>>,
id: Option<String>,
level: Option<String>,
@ -425,6 +442,7 @@ fn get_new<'r>(
"level" => level.as_str(),
"glyph" => glyph.as_str(),
"url" => url.as_str(),
"uri_prefix" => cfg.uri_prefix.as_str(),
};
if let Some(id) = id {
@ -491,6 +509,7 @@ fn get_binary(id: String, state: State<DB>) -> Response {
fn get_static<'r>(
resource: String,
resources: State<'r, HashMap<&str, &[u8]>>,
cfg: State<PastebinConfig>,
) -> Response<'r> {
let pth = format!("../static/{}", resource);
let ext = get_extension(resource.as_str()).replace(".", "");
@ -500,7 +519,7 @@ fn get_static<'r>(
None => {
let html =
String::from_utf8_lossy(resources.get("../static/index.html").unwrap()).to_string();
return get_error_response(html, Status::NotFound);
return get_error_response(cfg.uri_prefix.clone(), html, Status::NotFound);
}
};
let content_type = ContentType::from_extension(ext.as_str()).unwrap();
@ -513,8 +532,10 @@ fn get_static<'r>(
}
#[get("/")]
fn index() -> Redirect {
Redirect::to("/new")
fn index(cfg: State<PastebinConfig>) -> Redirect {
let url = String::from(Path::new(cfg.uri_prefix.as_str()).join("new").to_str().unwrap());
Redirect::to(url)
}
fn rocket(pastebin_config: PastebinConfig) -> rocket::Rocket {
@ -551,6 +572,7 @@ fn rocket(pastebin_config: PastebinConfig) -> rocket::Rocket {
db_opts.create_if_missing(true);
db_opts.set_compaction_filter("ttl_entries", compaction_filter_expired_entries);
let uri_prefix = pastebin_config.uri_prefix.clone();
let resources = load_static_resources!(
"../static/index.html",
"../static/custom.js",
@ -566,7 +588,7 @@ fn rocket(pastebin_config: PastebinConfig) -> rocket::Rocket {
.manage(db)
.manage(resources)
.mount(
"/",
if uri_prefix == "" { "/" } else { uri_prefix.as_str() },
routes![index, create, remove, get, get_new, get_raw, get_binary, get_static],
)
}

View file

@ -57,7 +57,7 @@ $(document).ready(function() {
url: window.location.pathname,
type: 'DELETE',
success: function(result) {
uri = "/new";
uri = uri_prefix + "/new";
uri = replaceUrlParam(uri, 'level', "info");
uri = replaceUrlParam(uri, 'glyph', "fas fa-info-circle");
uri = replaceUrlParam(uri, 'msg', "The paste has been successfully removed.");
@ -85,7 +85,7 @@ $(document).ready(function() {
$("#send-btn").on("click", function(event) {
event.preventDefault();
uri = "/";
uri = uri_prefix == "" ? "/" : uri_prefix;
uri = replaceUrlParam(uri, 'lang', $("#language-selector").val());
uri = replaceUrlParam(uri, 'ttl', state.expiry);
uri = replaceUrlParam(uri, 'burn', state.burn);
@ -103,7 +103,7 @@ $(document).ready(function() {
type: 'POST',
data: data,
success: function(result) {
uri = "/new";
uri = uri_prefix + "/new";
uri = replaceUrlParam(uri, 'level', "success");
uri = replaceUrlParam(uri, 'glyph', "fas fa-check");
uri = replaceUrlParam(uri, 'msg', "The paste has been successfully created:");

View file

@ -12,20 +12,20 @@
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css" crossorigin="anonymous">
<link href="/static/prism.css" rel="stylesheet" />
<link href="/static/custom.css" rel="stylesheet" />
<link href="{{uri_prefix}}/static/prism.css" rel="stylesheet" />
<link href="{{uri_prefix}}/static/custom.css" rel="stylesheet" />
</head>
<body>
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
<a class="navbar-brand" href="/">PASTEBIN</a>
<a class="navbar-brand" href="{{uri_prefix}}/new">PASTEBIN</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="/new">New <span class="sr-only">(current)</span></a>
<a class="nav-link" href="{{uri_prefix}}/new">New <span class="sr-only">(current)</span></a>
</li>
{{#if is_created}}
{{#if (not is_burned)}}
@ -33,7 +33,7 @@
<a class="nav-link" href="#" id="remove-btn">Remove</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/new?id={{pastebin_id}}" id="clone-btn">Clone</a>
<a class="nav-link" href="{{uri_prefix}}/new?id={{pastebin_id}}" id="clone-btn">Clone</a>
</li>
{{/if}}
{{/if}}
@ -226,8 +226,8 @@
{{#if is_created}}
<a id="copy-btn" class="form-control btn btn-outline-success mt-2 mt-md-0 mb-md-0 ml-md-2 ml-sm-2" href="#">Copy</a>
{{#if (not is_burned)}}
<a class="form-control btn btn-outline-success mt-2 mt-md-0 mb-md-0 ml-md-2 ml-sm-2" href="/raw/{{pastebin_id}}">Raw</a>
<a class="form-control btn btn-outline-success mt-2 mt-md-0 mb-md-0 ml-md-2 ml-sm-2" href="/download/{{pastebin_id}}">Download</a>
<a class="form-control btn btn-outline-success mt-2 mt-md-0 mb-md-0 ml-md-2 ml-sm-2" href="{{uri_prefix}}/raw/{{pastebin_id}}">Raw</a>
<a class="form-control btn btn-outline-success mt-2 mt-md-0 mb-md-0 ml-md-2 ml-sm-2" href="{{uri_prefix}}/download/{{pastebin_id}}">Download</a>
{{/if}}
{{/if}}
{{#if is_editable}}
@ -307,13 +307,14 @@
</div>
{{/if}}
<script>var uri_prefix="{{uri_prefix}}"</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.4/clipboard.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-notify/0.2.0/js/bootstrap-notify.min.js"></script>
<script src="/static/prism.js"></script>
<script src="/static/custom.js"></script>
<script src="{{uri_prefix}}/static/prism.js"></script>
<script src="{{uri_prefix}}/static/custom.js"></script>
</body>
</html>