Web Applications / Chapter 2: Building a Blog / Building a Blog

Blog Design

In this chapter, we are going to learn how to make a simple blogging website that we will call LWBlog. LWBlog contains a series of articles, or posts, written by the blog authors.

The LWBlog homepage shows a list of articles in the order that they were created. An article consists of a title, a body, the author who wrote the article and the date that the article was written.

Blog authors can create new articles. Authors can also edit and delete articles that they created.

Anyone can read the articles, but users must first sign in to the website in order to create or edit articles.

Architectural Overview

LWBlog stores the blog articles and user accounts in a database. The database is stored in a computer called db.com. The database server, serve_db, also runs in db.com. The LWBlog webserver is a Python application that runs in a separate computer called lwblog.com. When lwblog.com receives an HTTP request from a client, lwblog.com queries the database in db.com as necessary and sends a HTTP response back to the client. Client lwblog.com (Python Server) db.com (Database)

Components of LWBlog

Database Schema

Let's begin by thinking about what we want to store in the database. We want to store articles and user accounts, so we can create an Articles table and a Users table.

The articles table contains the title, the body, the author, and the creation date. Here is an example:

idtitlebodyauthorcreated
101New PhoneI went to the shopping mall and purchased the new LWPhone.102012024-05-05T13:15:30Z
102Playing SoccerSoccer is really hard! I have to use my feet! 327492024-07-12T12:24:77Z
103MovedI moved to San Jose, California today.238972024-10-02T20:35:19Z

The id column uniquely identifies a blog. The author column contains the article author's account ID. The created column indicates when the article was created. The Z at the end of the timestamp indicates that the timezone is UTC (Coordinated Universal Time).

The users table contains the user's email, name, and the account password. For example:

idemailgiven_namefamily_namepasswordcreated
10201curtis@fakemail.comCurtisConwaywefkx2372024-01-11T04:32:39Z
32749billy@dummymail.comBillyHomescj2892024-03-09T02:54:29Z
23897amy@bademail.comAmyMiltonfj8290jk2024-10-02T20:35:19Z

The email column is unique to ensure that every account uses a diffrent email address.

Retrieving the Home Page

Let's now discuss the server program implementation. The server should first send "USE blog" to the database at db.com as follows:

send_message("db.com", 'USE blog')
response = receive_json_message() # receive response from db.com

The server should then wait for and process client requests in an indefinite loop. Inside the loop, the server can receive client requests as follows:

request = receive_http_request()

When a user wants to visit LWBlog, the user types lwblog.com on the web browser's address bar and presses enter. The browser then sends the following message to lwblog.com:

GET / HTTP/1.1

receive_http_request() parses the request into a dictionary and stores it in request.

The server should then check the request path by calling request['path']. If the path is '/', the server should construct the home page and send it back to the client.

To construct the home page, the server should first retrieve all blog articles from the database as follows:

send_message("db.com", "SELECT id,title,body,author,created FROM articles")
response = receive_json_message()
rows = response['object']['rows']

Python then iterates over the rows and constructs HTML elements for each article. Here is an example of building an HTML snippet from a row:

row[1] is the article title and row[2] is the article body.

The server can stitch together the HTML snippets into one body of text using the join method. join is a string method which concatenates elements in a list to a single string. Here is an example:

",".join(["a","b","c"]) == "a,b,c"

Then the server can insert the text body into a HTML document as follows:

Finally, the server can construct the HTTP response and send it back to the client as follows:

send_message(client, "HTTP/1.1 200 OK\n\n" + html_doc)
Quiz (10 points)

Let's build the LWBlog homepage.

The simulator below consists of 3 computers:

  • The LWBlog server (lwblog.com)
  • The client (client.com)
  • The database server (db.com)

db.com contains the database. The database, named blog, is stored in /dbs. Please start serve_db in TERMINAL_DB.

The LWBlog server code should be stored in lwblog.com at /lwblog.py.

The server should handle requests to the homepage as described in the lesson above. You do not have to implement other pages yet. If the request path is not "/", the server should respond with status code 404.

We suggest constructing the homepage in a separate function to reduce the amount of code nesting.

Please use do_http to send test requests to lwblog.com.

Do your work in the simulator below. Click the "Save & Submit" button to save your work and submit it for grading.

Ready Logicwalk OS Simulator
Become a subscriber to save your progress, see the correct answer, and more!
Previous Lesson Next Lesson

Comments

Please log in to add comments