ServerUs is a single threaded webserver made in C++ used to deploy static and dynamic web application. The webserver also supports for the template html code which helps in adding the dynamic message to a web application.
- C++11 or higher versions
- MingW64 is preferred
-
Install the zip file and unzip it to the required location of your computer.
-
Note that the main HTML file name should be
index.htmlorindex.htm
To use ServerUs I have shared 3 example folders to get an example overview.
Although its quite easy to use ServerUs and I'm going to share each and every small step to give a better picture of the application.
Static websites in simple terms are those web applications which doesn't requires the server side processing of data.
So for those websites first of all you have to create a folder where your HTML and other files will be there.
For instance in my examples I have create a folder named app3 which consists of a portfolio website having all the necessary files.
Now in that folder create a cpp file which will have the required code to start the server.
In portfolio website lets see what the code can be :
#include<iostream>
#include<serverus>
using namespace serverus;
using namespace std;
int main()
{
ServerUs server(5050);
server.start();
return 0;
}In the above code server is the object of class ServerUs with 5050 as a port number after that server.start() is to start the server. Also it is compulsory to include serverus header and to use the namespace serverus
Now deploying dynamic websites which requires the code to get implemented on server end.
So in that case ServerUs provides the onRequest method :
void onRequest(string url, void(*ptrOnRequest)(Request&,Response&))The onRequest method takes two arguments, first one is the name of url associated with href link in the HTML code and second one is the function address of void return type and taking the instance of Request and Response class as an arguments.
The function will act as a mapping of the href link where you can write the code in c++ to perform any internal operation.
Also the class Request consists of get method:
string get(string name)This method will simply return the value to key you have used in the form tag in HTML.
And the Response method consists of write method:
void write(string responseBuffer)which is used to write the HTML code on the browser from the function.
Now Let's make it more clear by considering the app1 folder example:
We are creating a web application which will take the Roll Number and Name of Student and will put in the file at the server end. We are also providing the view, edit and delete student facility in that application.
index.html
<DOCTYPE HTML>
<html lang='en'>
<head>
<meta charset = 'UTF-8'>
<title>Monko School</title>
</head>
<body>
<a href='StudentAddForm.html'>Add Student</a><br>
<a href='getStudents'>View Students</a><br>
<a href='StudentEditForm.html'>Edit Student</a><br>
<a href='StudentDeleteForm.html'>Delete Student</a><br>
</body>
</html>Here the Add Student redirects to StudentAddForm.html
View Students redirects to getStudents. Here the getStudents is nothing but a key to map with the function
Edit Student and Delete Student will redirect to StudentEditForm.html and StudentDeleteForm.html respectively
StudentAddForm.html
<DOCTYPE HTML>
<html lang='en'>
<head>
<meta charset = 'UTF-8'>
<title>Monko School</title>
</head>
<body>
<h1> Student (ADD MODULE) </h1>
<form method = 'get' action = 'addStudent'>
Roll Number <input type='text' id='rn' name='rn'><br>
Name <input type='text' id='nm' name='nm'><br>
<button type='submit'>Add</button>
</form>
<a href='index.html'>Home</a>
</body>
</html>In this html file there is form which have nm and rn as the name to send with the url during submission. These names will act as key and the data entered in that field will act as value which can be extracted from get method of Request class in the cpp function.
Same goes with StudentEditForm.html and StudentDeleteForm.html.
Now myapp1.cpp is where we will write the logic to add,edit,view and delete the data form file also mapping the various requests. You can refer to myapp1.cpp from the app1 folder.
The code just consists of the logic for basic operation on the file and the main function has mapped all the Request URLs to there respective function. Here in various functions we have used write method of Response class to directly perform web related operation from cpp file.
Note The URL Mapping with the help of onRequest method should be done before starting the server, otherwise the later mappings will have no importance.
Request Forwarding is the feature of ServerUs which helps the programmer to forward the request to different function or file. This can be done by the forward method of Request class.
Template Files are the files with .tpl extension which are used to add dynamic messaging to web application
To implement the concept of template files:
- Set the attributes using
setAttributemethod:
void setAttribute(string name, string value)in the main cpp file which takes name-value pair as an argument.
-
Now create a file with
.tplextension where you can write the basic html code but the name of the attribute should be like this :$$${[name_of_attribute]} -
There is executable file named
TPLFileParserpresent in the exe folder. Run that file on the current working folder. -
Now in main cpp file add the following line of code: 4.1
#include"tpl.h"as a header.
4.2 registerTPLs(&[address_of_instanceof_ServerUs]) just before starting the server.
Here is the basic example of file with .tpl extension:
errorMessage.tpl
<!DOCTYPE HTML>
<html lang='en'>
<head>
<meta charset="utf-8">
<title>My Cool Website</title>
</head>
<body>
<h1>
$$${errorMessage}
</h1>
</body>
</html>You can refer to app2 folder for more clarification.
Now to compile the code open the command prompt for the current working folder and write the following command :
g++ -static *.cpp -o [file name].exe -I [location of the include folder] -L [location of the lib folder] -lserverus -lws2_32for example in my example the lib and include folder are in serverus folder so the command will be :
g++ -static *.cpp -o myapp3.exe -I ..\serverus\include -L ..\serverus\lib -lserverus -lws2_32And then run the code just by writing the name of executable file.
After that server will get start and now in the browser writing localhost:[portnumber] will deploy your website on the browser
You can also access the web application from remote devices just by typing the ipv4:portnumber in your remote device browser URL.
You can also use Dynamic DNS Service to provide your server a dynamic domain in case if your ip address changes periodically(The condition is that remote device should also be connected to same wifi as the server).
- Portfolio website: Showcase your skills and experience with a personal website built on ServerUs. You can host your resume, projects, and contact information, offering a dynamic platform for self-promotion.
- Static file hosting for a blog: If you have a blog built with another technology like Python or PHP, you can use ServerUs to host its static assets like images, CSS, and JavaScript files. This can offload some workload and improve performance.
- Internal team dashboard: Develop a web dashboard for your team to access shared documents, track progress, and communicate updates. ServerUs can host the necessary HTML, CSS, and JavaScript files for the dashboard interface.
-
Currently the server supports only GET Request but the future version will be able to handle various HTTP requests like : POST, PUT, DELETE, HEAD etc.
-
The future updations of the ServerUs will support the multithreading which will help to deploy the sites simultaneously.
