C++ Web Framework

Suhaan Bhandary 👋

I'm Suhaan Bhandary, the person behind Crystal. It started as a simple HTTP Server in C++ and as days went by I started adding features to it to make it a Robust Web Framework. It is a great project if you want to learn in depth about C++ or Backend Development and how it works behind the scenes.

Learning Bit By Bit, Suhaan

Where Code Meets User Friendly Syntax

Dive into the C++ World of Backend Brilliance! Create Robust, Flexible Apps with Your Unique Style, All Up to Modern Standards. Experience a Smooth Ride with MVC (Model View Controller) Architecture for Effortless Functionality!

controller code
monitor code

Seamless Development: Instant Live Restarts with Monitor

Monitor is a Program which Ensures Instant Live Restarts, Allowing one to see Changes in Real Time. Say Goodbye to Tedious Manual Restarts and Embrace a Seamless Workflow.

Dynamic Router

Crystal incorporates a dynamic routing system that boasts robust support for URL parameters. This functionality empowers developers to seamlessly manage an array of routes, each with its own set of dynamic parameters. A standout feature lies in its proficiency to effortlessly process URL parameters, thereby facilitating the dynamic rendering of content.

Registering Routes

Data in URL

URL parameters and search queries are powerful tools in web browsing. Parameters act like customizable keys, tailoring our web experience by unlocking specific content. Search queries, on the other hand, are like magic words in a search bar, fetching the most relevant results. Together, they make our online experience flexible and interactive, allowing us to navigate the web with precision.

Example of how to define a URL parameter

Example of how to access URL Param and Search Query from a Request

Status Codes

A status code in HTTP is a three-digit number returned by a server to indicate the outcome of a client's request. It categorizes responses into classes, such as success, redirection, client error, or server error.

All HTTP response status codes are separated into five classes or categories. The first digit of the status code defines the class of response, while the last two digits do not have any classifying or categorization role. There are five classes defined by the standard:

  • 1xx informational response: the request was received, continuing process
  • 2xx successful: the request was successfully received, understood, and accepted
  • 3xx redirection: further action needs to be taken in order to complete the request
  • 4xx client error: the request contains bad syntax or cannot be fulfilled
  • 5xx server error: the server failed to fulfil an apparently valid request

Sending status code with the Response

All Status Codes

JSON in C++

JSON (JavaScript Object Notation) is a lightweight data format. It's commonly used in REST APIs for exchanging information between a client and server. It allows for easy data interchange, making communication between different systems more efficient and standardized.

Example: Controller Parsing Json Response Body and Generating a Json Response to Client

Request Body to Controller

Response from Controller

Cookies

Browser cookies in HTTP are small data pieces stored by websites on a user's browser. Cookies can be used to store details like username, tokens and other things.

Cookies Usage

Public Files

Public Files can be easily served by stored them in the public folder and accessing it through the link /filePathRelativeToPublic. The files in the public folder are automatically registered in the router as a GET request which can be access from anywhere.

Redirect a Request

A Request can be redirected from one route to other, it can be useful to handle different cases where there is conditional routing or if the user is restricted from viewing the content.

Redirect Usage

HTML Template

Coming Soon..

Working on a solution similar to EJS template Language. Currently only {{variable}} in html file can be replaced with an string value.

Client Side Caching

Client-side caching is a way to save copies of web resources (like images, scripts, etc.) on a user's device. This helps the browser reuse these resources without asking the server again. It speeds up webpage loading.

ETags

ETags are unique IDs generated by a server to represent a specific version of a resource. When a user asks for a resource, the server sends it along with the ETag. When the user asks again, it sends the ETag back. If the resource hasn't changed, the server sends a no change code to client, then client uses the previous stored data.

All the Public Files have ETags added to them so that the client caches the data and reuses the files if no change in files. The ETag is created using the file size and it's modified date.

Etags on Custom Data

SQLite Support

Crystal contains a Singleton Pattern for SQLite.

Here is the visitor count after previous server restart. Visitor Count = 1

Live Restart

Live Restart is a feature which can be used while developing an Application to view changes as they happen.

For Live Restarts, we have a InHouse Program called Monitor.

Monitor is a Program which Ensures Instant Live Restarts, Allowing one to see Changes in Real Time. Say Goodbye to Tedious Manual Restarts and Embrace a Seamless Workflow.

Running Monitor

Live Routing Demo

Tic Tac Toe

A Classic Tic Tac Toe Game which uses HTML, CSS and JS as public files from the Backend Server. These public files are cached using ETags to improve load times on revisits.

Test

Url Param and Query

Try out the URL Param and Search Query working.

Test

Not Found Page

This is the default fallback page which is displayed when the given URL is wrong or doesn't match with any routes.

Test

Redirect to Google on Request

Redirect to Google shows the redirection functionality of the server, server can easily redirect a request from one route to other.

Test

Run anywhere with Docker

Unleash the full potential of our C++ framework! Seamlessly deploy on any hosting platform with Docker's power. Experience hassle-free setup and scaling for an elevated development journey.

Docker Config