This is a simple library that allows you to quickly create a very simple HTTP webserver.
- Clone the repository to your desired location using the
git clone --recurse-submodules <repository_url>
command. - Run the
build.sh
script to build the library including the necessary dependencies. - Once done with your project, clean the project by running the
clean.sh
script.
- Include the
HTTPServer.h
file to your project. - Create a server using the
http_server_constructor
. - Add the routes you need to the server by calling the
register_routes
method in the server structure. - Run the
launch
method in the server structure.
An example is given below.
#include "HTTPServer.h"
#include <stdio.h>
char *home(HTTPServer *server, HTTPRequest *request)
{
int number_of_files = 2
char *template = render_template(number_of_files, "path1", "path2");
return template;
}
int main()
{
HTTPServer server = http_server_constructor();
server.register_routes(&server, home, "/", 1, GET);
server.launch(&server);
return 0;
}
- Save and compile your project adding the webserver and pthread library as follows,
gcc <your files here> libwebserver.a -lpthread
- Enjoy!
The project uses a custom library of data structures written in C. Link
The library is extremely primitive and only appropriate for educational projects and not for any commercial use.
Feel free to provide feedback on what I can improve and issues you faced 🙌🏽.