summaryrefslogtreecommitdiff
path: root/you_can_serve_static_data_over_http.html
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-01-27 20:06:51 +0100
committerAki <please@ignore.pl>2022-01-27 20:06:51 +0100
commitb543cd3735b8892cfa2a4d49798ab6aaf7b17ca9 (patch)
treec37648d676495f1a888fc7693dde5bb14a6e8dbe /you_can_serve_static_data_over_http.html
parent6269d0c73ed835030ed5a096d12c7165e9b067a2 (diff)
downloadignore.pl-b543cd3735b8892cfa2a4d49798ab6aaf7b17ca9.zip
ignore.pl-b543cd3735b8892cfa2a4d49798ab6aaf7b17ca9.tar.gz
ignore.pl-b543cd3735b8892cfa2a4d49798ab6aaf7b17ca9.tar.bz2
Added a rant about static files
Diffstat (limited to 'you_can_serve_static_data_over_http.html')
-rw-r--r--you_can_serve_static_data_over_http.html76
1 files changed, 76 insertions, 0 deletions
diff --git a/you_can_serve_static_data_over_http.html b/you_can_serve_static_data_over_http.html
new file mode 100644
index 0000000..0922468
--- /dev/null
+++ b/you_can_serve_static_data_over_http.html
@@ -0,0 +1,76 @@
+<!doctype html>
+<html lang="en">
+<meta charset="utf-8">
+<meta name="viewport" content="width=device-width, initial-scale=1">
+<meta name="author" content="aki">
+<meta name="tags" content="software development, http, json, web development, static data, static files, rant">
+<link rel="icon" type="image/png" href="cylo.png">
+<link rel="stylesheet" href="style.css">
+
+<title>You Can Serve Static Data Over HTTP</title>
+
+<nav><p><a href="https://ignore.pl">ignore.pl</a></p></nav>
+
+<article>
+<h1>You Can Serve Static Data Over HTTP</h1>
+<p class="subtitle">Published on 2022-01-27 20:00:00+01:00
+<p>This will be short. I need to let some steam off and flush this thing out of my mind because it's already hanging
+around for too long. The story goes like this: we're prototyping small tool for whatever and we're at the point where
+there is a visible need to store some (most likely) static data. Oh, the thing is in JavaScript by the way.
+<p>"Yeah, let's just put it into a JSON file and serve it over web server."
+<p>Said me naively but turns out that there is a legitimate confusion on the other side of the trench.
+<p>"Do you mean a database?"
+<p>Let's end the flashback here, we already have enough background for what will follow. Now, hear me out, <em>your API
+endpoint is just pretending to be a file.</em>
+<p>You don't need a full blown web application back-end. You don't need an over-featured database engine that can
+provide its own REST API endpoints with data. And you don't need a complex ORM library and couple your data model to it
+and write your own endpoints. You don't need any of these and possibly others.
+<p>You <em>may</em> need them, but never let your habits decide your designs.
+<p>It's not illegal to use static files and it does work. Moreover, with a limited set of data it gives huge freedom to
+developer to try out things and break them. It's suited for rapid prototyping but it also has its own place in
+production environment (e.g., git).</p>
+<img src="you_can_serve_static_data_over_http-1.png" alt="free junk">
+<h2>Is it possible to learn this power?</h2>
+<p>Not from a modern developer. You need to jump down the rabbit hole of the history of solid and proven solutions.
+<p>First off, create some files. In my example we needed some static data for JavaScript, so we used JSON:
+<pre>
+[
+ {"type": 1, "name": "a"},
+ {"type": 1, "name": "b"}
+]
+</pre>
+<p>Now, save it as a file called <code>things</code> without any extension at all. Something like this will do:
+<pre>
+$ pwd
+/home/ignore/public
+
+$ ls
+things
+</pre>
+<p>It's ready to be served to the client. Start up your favourite server that can handle static data and position it at
+the directory that you've selected:
+<pre>
+$ python3 -m http.server
+Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
+</pre>
+<p>It can now be accessed at <code>http://localhost:8000/things</code>. If you are working with JS as I did that time,
+shift the <code>public</code> content a bit to also serve the client application:
+<pre>
+$ ls . api
+.:
+index.html style.css main.js api/
+
+api:
+things
+</pre>
+<p>Dead simple, right? Now you know. Hey, you can even mock more complex APIs with a help of symlinks and creativity.
+And if you worry about whether it works - just try it out. If it has some problems - force the server to set correct
+mime type for files without extensions.
+<p>Hopefully, next post will be more interesting, but this had to be done. Just had to. I may rest now.
+<!--
+Don't worry or go "ha ha, this guy got triggered by such thing" - you've been bamboozled. It's all have been an act.
+Without it the whole post would be not only basic but also boring. One thing remains true: for some reason the idea of
+this post was following me for quite a while now and I just wanted to get rid off it.
+-->
+</article>
+<script src="https://stats.ignore.pl/track.js"></script>