--- title: "Want to Learn a New Language? Write a Blog Backend!" date: 2022-03-02 tags: - beginner - backend author: Twi --- Most of the content on my blog requires a fair bit of technical context to understand. This post is aimed at beginners as well as people with a lot more experience in the industry. It also covers a question that I get asked a lot: [What is a good project to use for learning a new programming language? It seems like a really open ended question and it can be hard to see where I would end up with such a thing.](conversation://Mara/hmm) Here is a project that you can use as an end goal for learning to program in a new language: I call it Within's Minimal Viable Blog. With this project you will build a blog engine that can also function as a portfolio site. [A portfolio site is a personal website where you can show off what you've done and helps you stand out from the crowd. The blog you are reading right now is a perfect example of a portfolio site. These kinds of sites are also important in a "keeping the internet weird" sense. You can do a lot more flair and customization to a website you own than you can to a Twitter profile or similiar.](conversation://Mara/happy) It's also designed to make you dip your toes into a lot of commonly used technologies and computer science fundamentals in the process. Namely it makes you deal with these buzzwords: - State management (remembering/recalling things persistently) - Basic web serving - HTML templating - Static file serving - Input sanitization (making sure that invalid input can't cause JavaScript injections, etc) - Sessions (remembering who a user is between visits to a page) [You can rip out a lot of this and still end up with a viable result, such as by making a static site generator. However if you have never done something like this before I'd be willing to argue that it's well worth your time to attempt to do something like this at least once.](conversation://Cadey/enby) At a high level, here's what you should end up with: - An abstract "Post" datatype with a title, publication date, a "URL slug" and content in plain text - A home page at `/` that describes yourself to the world - A list of blog posts at `/blog` - Individual blog posts at `/blog/{date}/{slug}` - A `/contact` page explaining how to contact you - An admin area at `/admin/*` that is protected by a username and password - An admin form that lets you create new posts - An admin form that lets you edit existing posts - An admin form that lets you delete posts - An admin view that lets you list all posts - Use a CSS theme you like (worst case: pick one at random) to make it look nice - HTML templates for the base layout and page details [All these admin views and forms are needed because they are what allow you to modify content on the blog. When you upload code to GitHub or use the web editor, this is the kind of thing that GitHub has implemented on the backend. You can also likely reuse the "new post" form as the "edit post" form because they do very similar things.](conversation://Mara/hacker) For extra credit, you can do the following extra things: - Add an "updated at" date that shows up if the post has been edited - Add tags on posts that let users find posts by tag - [JSONFeed](https://www.jsonfeed.org/) support - "Draft" posts which aren't visible on the public blog index or feeds, but can be shared by URL - Use CSRF protection on the admin panel - Deploy it on a VPS and serve it to the internet (use [Caddy](https://caddyserver.com/) to make this easier) - Pagination of post lists If you've never done a project like this, this may take you a bit longer. You will have to do some research on the best way to write things in your language of choice. A good starting language for this is something like Python with [Flask](https://flask.palletsprojects.com/en/2.0.x/) or Go with the standard library, namely with [net/http](https://pkg.go.dev/net/http) and [html/template](https://pkg.go.dev/html/template). This may take you a week or two. If you've done this kind of thing before, it may take a few days to a week depending on how much experimentation you are doing. [What the heck is a "URL slug"?](conversation://Numa/delet) [Most of the time a "URL slug" means some URL-safe set of characters that help humans identify what the content is about. If you look at the URL for this article, you can see that its slug is `new-language-blog-backend`, which is purely for humans to read. You can either take this as something you generate by hand on each post, or take the title, remove non-space and non-alphanumeric characters, lowercase it and then replace spaces with dashes. This would turn "Hello World!" into "hello-world" or similiar.](conversation://Mara/hacker) Here are a few steps that may help you get started doing this: - Serve a static file at `/` that contains `