MojoForum - Project in Mojolicious
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:
- 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.
- 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.
MySQL 5.0 onwards, must be installed on your system. If not, install it according to your flavored OS:
- MySQL fundamental knowledge.
- 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?
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
A Minimum Viable Product specification of a forum is:
Database to store forum information and columns:
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.
Select the database for successive operations.
Next step is to create Users, Threads and Replies table.
We can understand the above table structure as:
- Users may have threads.
- Users may add replies to threads.
- Replies belongs to thread.
We also need to establish a relationship between:
- Users and Threads table
- Users and Replies table
- 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,
- To install Mojolicious
- Install MySQL
- Create Database
- Create users, threads and replies table.
In the next article, we learn to seed the database and setup the scaffolding of our Forums application.