git.m455.casa

m455.casa

clone url: git://git.m455.casa/m455.casa


html/posts/redesigning-my-paste-service-with-fennel.html

1 <!DOCTYPE html>
2 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
3 <head>
4 <meta charset="utf-8" />
5 <meta name="generator" content="pandoc" />
6 <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
7 <title>Redesigning my paste service with Fennel</title>
8 <style>
9 code{white-space: pre-wrap;}
10 span.smallcaps{font-variant: small-caps;}
11 span.underline{text-decoration: underline;}
12 div.column{display: inline-block; vertical-align: top; width: 50%;}
13 div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
14 ul.task-list{list-style: none;}
15 </style>
16 <link rel="stylesheet" href="/assets/archive.css">
17 </head>
18 <body>
19 <main>
20 <h2 id="redesigning-my-paste-service-with-fennel">Redesigning my paste service with Fennel</h2>
21 <p>2021-01-21 00:00</p>
22 <p>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 <code>~/bin/</code> and the functions I had to add inside of my <code>~/.vimrc</code>.</p>
23 <p>After a bit of thinking, I decided on a few things:</p>
24 <ul>
25 <li>I want this to be even easier to setup</li>
26 <li>I don’t want to have to bother with Let’s Encrypt’s <code>certbot</code> tool</li>
27 <li>I don’t need syntax highlighting</li>
28 <li>I want to reduce the amount of scripts required for the paste service</li>
29 <li>I want to de-clutter my <code>~/.vimrc</code></li>
30 <li>I want to write a new tool in <a href="https://fennel-lang.org/">Fennel</a>!</li>
31 </ul>
32 <h3 id="replacing-my-subdomain-with-a-subdirectory">Replacing my subdomain with a subdirectory</h3>
33 <p>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 “<code>https://m455.casa/paste/</code>”.</p>
34 <p>After figuring out how to do this, it turned it that it was actually pretty simple!</p>
35 <p>First, I setup a directory on my server where my pastes would reside:</p>
36 <pre><code>sudo mkdir -p /var/www/paste
37 sudo chown -R $USER:$USER /var/www/paste
38 sudo chmod -R 755 /var/www/paste</code></pre>
39 <p>Then, I added the following snippet to my already-made nginx configuration at <code>/etc/nginx/sites-available/m455.casa</code>:</p>
40 <pre><code>location /paste {
41 alias /var/www/paste;
42 }</code></pre>
43 <p>I restarted nginx with <code>sudo systemctl restart nginx</code>, and voila!</p>
44 <p>This method removed the need to setup TSL and SSL certificates for a subdomain at <code>paste.m455.casa</code> and removed the need for more nginx <code>sites-available</code> and <code>sites-enabled</code> entries.</p>
45 <h3 id="writing-a-multi-purpose-pasting-script">Writing a multi-purpose pasting script</h3>
46 <p>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 <code>~/.vimrc</code>, which was what I wanted when I outlined my goals for the new setup.</p>
47 <p>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.</p>
48 <p>Next, I wanted to write a single script that would handle uploading source code, by appending “<code>.txt</code>” to the end of the filename, as long as it didn’t end in:</p>
49 <ul>
50 <li><code>.txt</code></li>
51 <li><code>.jpg</code></li>
52 <li><code>.png</code></li>
53 <li><code>.bmp</code></li>
54 <li><code>.svg</code></li>
55 <li><code>.gif</code></li>
56 </ul>
57 <p>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 <code>.txt</code> is there just so I don’t end up with a file called <code>cool.txt.txt</code>. 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 <code>.txt</code> wouldn’t append to those kinds of files.</p>
58 <p>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 <code>.bmp</code> file haha).</p>
59 <p>I also wanted the script to be able to synchronize my paste directory, in case I moved files into my <code>~/dev/paste</code> directory for synchronizing, or for when I removed files.</p>
60 <p>The script, in the end, would replace the following scripts I had:</p>
61 <ul>
62 <li><code>pupload</code>: A script for uploading any given file and generating a URL I could copy and paste.</li>
63 <li><code>ptxt</code>: A script for uploading and appending <code>.txt</code> to a filename, and generating a URL I could copy and paste.</li>
64 <li><code>psync</code>: A script that would synchronize files in my paste directory at <code>~/dev/paste</code>.</li>
65 </ul>
66 <p>The result? This little Fennel script called <a href="https://%7B%7Bgit-domain%7D%7D/fpaste">fpaste</a>.</p>
67 <p>All you have to do to configure it, is change four variable values:</p>
68 <pre><code>(local ssh &quot;m455@m455.casa&quot;)
69 (local domain &quot;https://m455.casa/paste&quot;)
70 (local remote-path &quot;/var/www/paste&quot;)
71 (local local-path &quot;~/dev/paste&quot;)</code></pre>
72 <p>Slashes at the end of the <code>domain</code>, <code>remote-path</code>, and <code>local-path</code> values are optional. If slashes are present, they will be removed, if they aren’t, the script can handle that too.</p>
73 <p>The script is basically a wrapper around <code>rsync</code>. 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!</p>
74 </main>
75 </body>
76 </html>