···11+---
22+title: "Want to Learn a New Language? Write a Blog Backend!"
33+date: 2022-03-02
44+tags:
55+ - beginner
66+ - backend
77+author: Twi
88+---
99+1010+Most of the content on my blog requires a fair bit of technical context to
1111+understand. This post is aimed at beginners as well as people with a lot more
1212+experience in the industry. It also covers a question that I get asked a lot:
1313+1414+[What is a good project to use for learning a new programming language? It seems
1515+like a really open ended question and it can be hard to see where I would end up
1616+with such a thing.](conversation://Mara/hmm)
1717+1818+Here is a project that you can use as an end goal for learning to program in a
1919+new language: I call it Within's Minimal Viable Blog. With this project you will
2020+build a blog engine that can also function as a portfolio site.
2121+2222+[A portfolio site is a personal website where you can show off what you've done
2323+and helps you stand out from the crowd. The blog you are reading right now is a
2424+perfect example of a portfolio site. These kinds of sites are also important in
2525+a "keeping the internet weird" sense. You can do a lot more flair and
2626+customization to a website you own than you can to a Twitter profile or
2727+similiar.](conversation://Mara/happy)
2828+2929+It's also designed to make you dip your toes into a lot of commonly used
3030+technologies and computer science fundamentals in the process. Namely it makes
3131+you deal with these buzzwords:
3232+3333+- State management (remembering/recalling things persistently)
3434+- Basic web serving
3535+- HTML templating
3636+- Static file serving
3737+- Input sanitization (making sure that invalid input can't cause JavaScript
3838+ injections, etc)
3939+- Sessions (remembering who a user is between visits to a page)
4040+4141+[You can rip out a lot of this and still end up with a viable result, such as by
4242+making a static site generator. However if you have never done something like
4343+this before I'd be willing to argue that it's well worth your time to attempt to
4444+do something like this at least once.](conversation://Cadey/enby)
4545+4646+At a high level, here's what you should end up with:
4747+4848+- An abstract "Post" datatype with a title, publication date, a "URL slug" and
4949+ content in plain text
5050+- A home page at `/` that describes yourself to the world
5151+- A list of blog posts at `/blog`
5252+- Individual blog posts at `/blog/{date}/{slug}`
5353+- A `/contact` page explaining how to contact you
5454+- An admin area at `/admin/*` that is protected by a username and password
5555+- An admin form that lets you create new posts
5656+- An admin form that lets you edit existing posts
5757+- An admin form that lets you delete posts
5858+- An admin view that lets you list all posts
5959+- Use a CSS theme you like (worst case: pick one at random) to make it look nice
6060+- HTML templates for the base layout and page details
6161+6262+[All these admin views and forms are needed because they are what allow you to
6363+modify content on the blog. When you upload code to GitHub or use the web
6464+editor, this is the kind of thing that GitHub has implemented on the
6565+backend. You can also likely reuse the "new post" form as the "edit post" form
6666+because they do very similar things.](conversation://Mara/hacker)
6767+6868+For extra credit, you can do the following extra things:
6969+7070+- Add an "updated at" date that shows up if the post has been edited
7171+- Add tags on posts that let users find posts by tag
7272+- [JSONFeed](https://www.jsonfeed.org/) support
7373+- "Draft" posts which aren't visible on the public blog index or feeds, but can
7474+ be shared by URL
7575+- Use CSRF protection on the admin panel
7676+- Deploy it on a VPS and serve it to the internet (use
7777+ [Caddy](https://caddyserver.com/) to make this easier)
7878+- Pagination of post lists
7979+8080+If you've never done a project like this, this may take you a bit longer. You
8181+will have to do some research on the best way to write things in your language
8282+of choice. A good starting language for this is something like Python with
8383+[Flask](https://flask.palletsprojects.com/en/2.0.x/) or Go with the standard
8484+library, namely with [net/http](https://pkg.go.dev/net/http) and
8585+[html/template](https://pkg.go.dev/html/template). This may take you a week or
8686+two. If you've done this kind of thing before, it may take a few days to a week
8787+depending on how much experimentation you are doing.
8888+8989+[What the heck is a "URL slug"?](conversation://Numa/delet)
9090+9191+[Most of the time a "URL slug" means some URL-safe set of characters that help
9292+humans identify what the content is about. If you look at the URL for this
9393+article, you can see that its slug is `new-language-blog-backend`, which is
9494+purely for humans to read. You can either take this as something you generate by
9595+hand on each post, or take the title, remove non-space and non-alphanumeric
9696+characters, lowercase it and then replace spaces with dashes. This would turn
9797+"Hello World!" into "hello-world" or similiar.](conversation://Mara/hacker)
9898+9999+Here are a few steps that may help you get started doing this:
100100+101101+- Serve a static file at `/` that contains `<h1>Hello, world!</h1>`
102102+- Create a SQLite connection and the posts table
103103+- Insert a post into your database by hand with the `sqlite3` console
104104+- Wire up a `/blog/{date}/{slug}` route to show that post
105105+- Wire up `/blog` to show all the posts in the database
106106+- Make static pages for `/` and `/contact`
107107+- Choose a templating language and create a base template
108108+- Edit all your routes to use that base template
109109+- Create the admin login page and a POST route to receive the HTML form and
110110+ check the username/password against something in the SQLite database, creating
111111+ a session for the admin panel
112112+- Create an external tool that lets you manually set your username and password
113113+- Create an admin view that shows all posts
114114+- Create a form that lets you create a new post and its associated POST handler
115115+- Create a form that lets you edit an existing post and its associated POST
116116+ handler
117117+- Use a CSS theme to make it all look nice
118118+119119+There are no wrong ways to do this. You can take shortcuts where you want. You
120120+can use Markdown for formatting posts. You can do anything you want based on
121121+this skeleton and it will be _your_ creation. Nobody else will have something
122122+exactly like this. It will let you stand out as a professional and can help you
123123+create your own home on the internet.
124124+125125+[Something else to keep in mind here is that this first attempt will be ugly as
126126+sin. It will be hacky as all hell. However the important part here is that it
127127+works above all else. You can come back and make it better later. The first pass
128128+is always the most ugly, do not let this discourage you. This is a personal form
129129+of expression. Don't be afraid to let your personality show through your
130130+website. That's what helps to make you stand out in a sea of LinkedIn and
131131+Twitter profiles.](conversation://Numa/happy)
132132+133133+[Yeah, getting started in something you've never really done before can be hard
134134+because you won't really have a good idea of what's "correct". The only
135135+"correct" thing in this project is that you can connect with a browser and see
136136+the things you expect. Take chances, make mistakes, get
137137+messy!](conversation://Mara/happy)
138138+139139+If you do go through with this and want to show it off to people in a followup
140140+post, email your results to
141141+[blogchallenge2022@xeserv.us](mailto:blogchallenge2022@xeserv.us) with the
142142+following information:
143143+144144+- Your name/alias (you don't have to use your legal name)
145145+- How much background you have in programming
146146+- The language you are writing this in
147147+- Screenshots you want to share
148148+- Anything you learned along the way, where you got stuck, what got you unstuck
149149+ and anything else you want to say
150150+- If you uploaded it to GitHub, the repository URL would be nice but is not
151151+ required
152152+- If you deployed it to a server, the link to where it's running would also be
153153+ nice but is definitely not required
154154+155155+The road to expertise is paved with the effort of experimentation. I hope that
156156+this open-ended project can help you learn things.