MojoForum - A Forum in Mojolicious (Part-1)

Posted by Ashutosh on April 25, 2020


MojoForum - Project in Mojolicious

banner

I believe that the good way to learn any technology is through the Project. Project based learning helps us to learn the critical parameters of the technology and we also know how to use them on the production environment. Therefore I decided to write a series of articles, where I write on how to develop a forum (MojoForum) with the help of the Mojolicous. Project repository is on the Github. I am developing the forum during the series and then check in the code to the Github.

As we all know, Mojolicious is a light-weight web framework written in Perl. It is simple yet remarkably powerful to write complex web applications. It requires a few modules to start developing web projects.

We all know, the forum is a place where the user can request a question and exchange the views with other users. Without further delay, let's jump into the series.

Before starting the Project in Mojolicous, the following are the prerequisites for this tutorial:

  1. Perl version 5.24 or greater installed on your local machine. On Unix or Mac Operating System, Perl is available through the default OS installation packages, if you are using the Windows machine, install Activestate Perl from here or strawberry Perl from here.
  1. Elementary knowledge of Perl (Not Object Oriented) is essential. If you are not accustomed to basic Perl, then you are not ready for this tutorial. I would recommend acquiring fundamental knowledge of Perl first and continue the tutorial.
  1. MySQL 5.0 onwards, must be installed on your system. If not, install it according to your flavored OS:

    1. Windows Installation Packages.
    2. Debian/Ubuntu Unix Installation.
    3. Redhat/CentOS/Fedora Installation.
    4. macOS Installation.
  1. MySQL fundamental knowledge.
  1. Basic knowledge of HTML, CSS, Tailwind CSS framework.

I am using Visual Studio Code with Tabnine plugin for developing the forums.

Installation of Mojolicious

You are over here!!! You fulfilled all the prerequisites to get started with Mojolicious Application. Great!!!

Let's start with the Mojolicious Installation.

I already write an article for Mojolicous Installation. Please use this link to install Mojolicious.

Start Project in Mojolicious

It's reasonably fair to assume that installation is successful. Let's generate the app.

mojo generate app mojoForum

In Mojolicious one line is enough to create the Project. Isn’t is easy?

Application Launch

cd mojoForum
morbo script/mojoForum

If you receive a message in command line

Server available at http://127.0.0.1:3000

You are good to. Go to the browser and launch the application http://localhost:3000

Database seeding

A Minimum Viable Product specification of a forum is:

  1. Database to store forum information and columns:

    1. Threads
    2. Replies
    3. Users

Once we set up the database, we need routes to view the data on our web application.

Let's start by creating the database in MySQL first. We want our forum application DB isolated from other applications therefore, we create a new database in MySQL.

If you have phpmyadmin/MySQL Workbench/SQL pro, you can use these tools to set up a database and it's tables. Otherwise, use the Command line terminal.

Sign in to MySQL database from command prompt:

mysql -u root -p

Enter the administrative password and you will be at MySQL Command prompt.

Create a new database by typing the following command:

CREATE DATABASE MojoForum;

Anyways, you are not restricted to use this name. You are liable to select any name for your application database.

Select the database for sequential operations.

USE MojoForum;

Select the database for successive operations.

Next step is to create Users, Threads and Replies table.

We can understand the above table structure as:

  1. Users may have threads.
  2. Users may add replies to threads.
  3. Replies belongs to thread.

We also need to establish a relationship between:

  1. Users and Threads table
  2. Users and Replies table
  3. Threads and Replies table

Create Users table.

CREATE TABLE 'users' (
 'id' int(11) unsigned NOT NULL AUTO_INCREMENT,
 'first_name' varchar(25) NOT NULL DEFAULT '',
 'middle_name' varchar(25) DEFAULT NULL,
 'last_name' varchar(25) NOT NULL DEFAULT '',
 'email' varchar(50) DEFAULT NULL,
 'password' varchar(255) DEFAULT NULL,
 PRIMARY KEY ('id')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Create threads table.

CREATE TABLE 'threads' (
  'id' int(11) unsigned NOT NULL AUTO_INCREMENT,
  'user_id' int(11) unsigned DEFAULT NULL,
  'title' varchar(50) DEFAULT NULL,
  'body' mediumtext,
  PRIMARY KEY ('id'),
  KEY 'user_id' ('user_id'),
  CONSTRAINT 'threads_ibfk_1' FOREIGN KEY ('user_id') REFERENCES 'users' ('id') ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Create replies table

CREATE TABLE 'replies' (
  'id' int(11) unsigned NOT NULL AUTO_INCREMENT,
  'user_id' int(11) unsigned DEFAULT NULL,
  'thread_id' int(11) unsigned DEFAULT NULL,
  'body' mediumtext,
  PRIMARY KEY ('id'),
  KEY 'user_id' ('user_id'),
  KEY 'thread_id' ('thread_id'),
  CONSTRAINT 'replies_ibfk_1' FOREIGN KEY ('user_id') REFERENCES 'users' ('id') ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT 'replies_ibfk_2' FOREIGN KEY ('thread_id') REFERENCES 'threads' ('id') ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

We have successfully created the database and tables, and columns. We also establish the relationship between users, thread and replies table.

That's all for this article. In this article we learn,

  1. To install Mojolicious
  2. Install MySQL
  3. Create Database
  4. Create users, threads and replies table.

In the next article, we learn to seed the database and setup the scaffolding of our Forums application.

Stay tuned.