Redesigning my paste service with Fennel
2021-01-21 00:00
So, in my previous post, I wrote about my paste service setup using Vim, nginx, and rsync. After using the tools involved in that setup, I had realized that I didn’t like all the scripts I had to throw in my ~/bin/
and the functions I had to add inside of my ~/.vimrc
.
After a bit of thinking, I decided on a few things:
- I want this to be even easier to setup
- I don’t want to have to bother with Let’s Encrypt’s
certbot
tool - I don’t need syntax highlighting
- I want to reduce the amount of scripts required for the paste service
- I want to de-clutter my
~/.vimrc
- I want to write a new tool in Fennel!
Replacing my subdomain with a subdirectory
The first thing I did was learn how to create a separate directory on my server that would contain my “pastes” (files). I wanted this to appear as a subdirectory on my homepage, so my “pastes” wouldn’t clutter up my website’s source code directory. I wanted something that looked like “https://m455.casa/paste/
”.
After figuring out how to do this, it turned it that it was actually pretty simple!
First, I setup a directory on my server where my pastes would reside:
sudo mkdir -p /var/www/paste
sudo chown -R $USER:$USER /var/www/paste
sudo chmod -R 755 /var/www/paste
Then, I added the following snippet to my already-made nginx configuration at /etc/nginx/sites-available/m455.casa
:
location /paste {
alias /var/www/paste;
}
I restarted nginx with sudo systemctl restart nginx
, and voila!
This method removed the need to setup TSL and SSL certificates for a subdomain at paste.m455.casa
and removed the need for more nginx sites-available
and sites-enabled
entries.
Writing a multi-purpose pasting script
This was the fun part. Since I decided I didn’t need syntax highlighting, I didn’t need the functions in Vim anymore to to generate a syntax-highlighted HTML file, so I deleted those functions. This de-cluttered my ~/.vimrc
, which was what I wanted when I outlined my goals for the new setup.
I also wanted to remove the amount of scripts I used, so I moved all of them into a directory containing old, unused scripts that I wrote.
Next, I wanted to write a single script that would handle uploading source code, by appending “.txt
” to the end of the filename, as long as it didn’t end in:
.txt
.jpg
.png
.bmp
.svg
.gif
This is because I wanted to be able to view, and allow others to view, the plaintext files in the browser after they were pasted. The .txt
is there just so I don’t end up with a file called cool.txt.txt
. It would still work, it would just be a little silly. I occasionally share screenshots and gifs with friends, so I wanted those to be rendered in the browser, so .txt
wouldn’t append to those kinds of files.
I know there are way more file suffixes and formats than that, but I don’t find myself pasting anything but those (though I don’t remember the last time I uploaded a .bmp
file haha).
I also wanted the script to be able to synchronize my paste directory, in case I moved files into my ~/dev/paste
directory for synchronizing, or for when I removed files.
The script, in the end, would replace the following scripts I had:
pupload
: A script for uploading any given file and generating a URL I could copy and paste.ptxt
: A script for uploading and appending.txt
to a filename, and generating a URL I could copy and paste.psync
: A script that would synchronize files in my paste directory at~/dev/paste
.
The result? This little Fennel script called fpaste.
All you have to do to configure it, is change four variable values:
(local ssh "m455@m455.casa")
(local domain "https://m455.casa/paste")
(local remote-path "/var/www/paste")
(local local-path "~/dev/paste")
Slashes at the end of the domain
, remote-path
, and local-path
values are optional. If slashes are present, they will be removed, if they aren’t, the script can handle that too.
The script is basically a wrapper around rsync
. It takes a filename and “cleans” its suffix, copies it to a local paste directory, synchronizes the local paste directory with the remote paste directory, and outputs a URL to share based on the values you entered in the configuration variables!