Tornado: Blog Transition
11/25/14
Hey! So, I have abandoned doing this. I made notes while I was doing stuff, expecting to blog about it when I was done.
I'm not going to be done now for the foreseeable future, so here are my blind ramblings.
My Unfinished Attempt at Tornado Blog Implementation
The time has come! My blog content needs to be restructured more intelligently, and not be a bunch of files in a "blog" directory. It also makes sense to work towards making blog posts easier, perhaps using a different markup language that can be compiled into HTML....
Maybe.
Anyways, I suppose where I need to start is by thinking about what the Tornado setup will be. Perhaps a decent place to start is Tornado's blog example on Github.
Reading....
Reading....
Hmmm.... That's a lot of stuff I don't need. This is for, like, a multiple-user blog system, which I'm not particularly interested in creating just yet.
What does my Tornado repo currently have in it, again? Some basic template stuff and a reimplemenation of the home page.
Hmmm.... What's the next step here? Okay, so general pool of blog posts screams database. I could start there. I don't need anything heavy-duty right now, might as well use SQLite. So, what are the columns for a blogpost table?
- The blog post (aka the html body, or the "blogpost div within the body in this case)
- Date published (valuable piece of blog info to reader and also how we'll order blog posts)
- The blog's title (used for header title value and as the headline of the blog)
- ...
Hmmm... (I'm humming in thought a lot during the writing of this post, Stephan). You know, there's other stuff I might want.
- HTML header stuff specific to this blog post (in case I want to do anything special for any specific post, styling or javascript-wise)
- Blog post in a series value (for instance, this post is a part of a series of posts wherein I work my way through transitioning my site to Tornado)
- Content tags (I write about a lot of random things)
Though, I suppose I can make changes when I see fit. For now, let's stick with the basics: the blog post div, the published date, and the blog's title. Let's make a simple SQLite setup!
....
Ooh, a quickstart guide to SQLite.
Reading....
The program 'sqlite3' is currently not installed. You can install it by typing:
sudo apt-get install sqlite3
Fair enough.
All right, it's installed, but running the above command brings me to a sqlite shell. I think I would rather python make the databse if it didn't already exist. So, let's make a python script that does just that.
....
Ooh, official sqlite3 module documentation. This will probably be useful.
Re—Oh, that was fast. Let's try the examples that look useful right away.
conn = sqlite3.connect('databases/blog.db')
c = conn.cursor()
If I do this without an actual database at that path, what happens?
It does! Cool.
Now, let's create a table "posts" with date, title, and content.
"CREATE TABLE posts (date text, title text, content text)"
)
Did it... did it work?
$ python sqlite3_test.py
Traceback (most recent call last):
File "sqlite3_test.py", line 5, in
c.execute(
"CREATE TABLE posts (date text, title text, content text)"
)
sqlite3.OperationalError: table posts already exists
Yes! Success! (Note to self: better way of checking)
(For now, I'll just wrap that create table statement in a try/except statement.)
Okay, I got a table....
Now what?
Well, I'm definitely going to need to make a script that transforms all my current blog posts into database entries. I might want to start with a proof of concept blog post in the database.
Yeah. Yeah, I like that.
import time
date = int( time.time() )
post = '''
<p>
Testity test test test.
</p>
'''
And now there's a test blog post that I should be able to setup. Time to make some Tornado code that:
- Lists all blog content by date order to general blog page
- Gives individual blog posts their own link based on title
So, the general webpage. Right now I got a simple setup that looks identical to my current homepage. I'd link to where the Tornado setup lives, but I've been noticing a lot of bots trying to hack their way into my server using various scripts that connect via the IP address.
No, thank you. I'll need to setup accessing of my tornado setup via some less bot-accessible means. Right now, I have the static html file setup with the server name:
I think it'll be fine for me to give my tornado setup one of those names for a bit.
server_name www.ksionda.me;
# Static html file setup
server_name www.ksionda.me;