1 Beginners Guide to Link
2 =======================
5 > Author: Ariston Lorenzo <me@ariston.dev>
7 > Last Modified: April 13, 2023
12 Link is a web server library built with a focus of speed for C++. Link is meant
13 to be a relatively easy framework to use and is considered the *ExpressJS* for
16 A New Project (with linkproj)
17 -----------------------------
18 Once you have installed Link, you can clone the [aristonl/linkproj](https://github.com/aristonl/linkproj)
19 repository which is meant to be a simple template you can use to quickly setup
20 a Link-based project. To build, all you need to do is:
22 mkdir build && cd build
27 Or you can also use ninja like so:
29 mkdir build && cd build
34 *linkproj* will run on port 3000 by default. You can start editing your site in
35 `www/` and handle all C++ related code in `src/`.
37 A New Project (from scratch)
38 ----------------------------
39 Once you have installed Link, create a new folder where your website
40 will be stored. Create a new .cpp file in the root of the project (or create a separate
41 src/ directory). The following code is just a simple "Hello, World!" site.
46 #include <openssl/ssl.h>
49 Link::Server server(8080) /* Set the port */
51 int main(int argc, char **argv)
53 /* Handle requests for http://localhost:8080/ */
54 server.Get("/", [](Link::Request* req, Link::Response* res) {
55 /* Set the HTTP Header */
56 res->SetHeader("Content-Type", "text/html; charset=UTF-8");
57 /* Send the actual content of the page */
58 res->SetBody("<h1>Hello, World!</h1>");
61 /* Start the server */
62 std::cout << "Server started on port 8080." << '\n';
69 To build the web server, you can either use Make (GNU or BSD) or CMake. The
70 following is a CMake example:
72 cmake_minimum_required(VERSION 3.16)
73 project(website C CXX)
75 # Set include directory for OpenSSL on Mac
76 include_directories(/opt/homebrew/opt/openssl@3/include)
80 add_executable(website ${cpp_src})
81 target_compile_features(website PUBLIC cxx_std_17)
82 set(CMAKE_CXX_FLAGS "-fpic -pthread")
84 target_link_libraries(website PRIVATE link)
86 If you plan to use CMake, it is best to use it in tandem with Ninja. Here's a
98 $(CXX) -o $@ main.o $(LIBS)
101 $(CXX) $(CFLAGS) -c $*.cpp
105 Once the `website` executable is built, run it using `./website` and open your
106 browser to http://localhost:8080 (or any other port you may have set it to). It should show "Hello, World!".
110 Obviously, we don't want to put all the HTML in the C++ file but instead have it
111 in a separate HTML file that we can call. To be able to do this, you can modify your source code like so:
116 int main(int argc, char **argv)
118 server.Get("/", [](Link::Request* req, Link::Response* res) {
119 res->SetHeader("Content-Type", "text/html; charset=UTF-8");
120 std::ifstream file("index.html");
121 std::string content((std::istreambuf_iterator<char>(file)),
122 std::istreambuf_iterator<char>());
123 res->SetBody(content);
126 std::cout << "Server started on port 8080." << '\n';
133 Then you can create `index.html` and edit it like a normal HTML file.
135 > -----------------------------------------------------------------------------
137 > **NOTE**: *When calling index.html with `ifstream`, the location of index.html
138 > should be entered as the relative location from the executable. For example,
139 > if your file tree looks like this:*
149 > *The location of index.html should be filled in like so:*
151 > std::ifstream file("../www/index.html");
154 > -----------------------------------------------------------------------------
156 You can also serve static files by pointing `server.SetStaticPages` to the folder
157 it's located in (usually `static/` or `public/`) like so:
160 int main(int argc, char **argv)
162 server.SetStaticPages("public/");
164 /* rest of the code here... */
168 > -----------------------------------------------------------------------------
170 > **NOTE**: *The location of the folder must also be specified as its relative
171 > location to the executable.*
173 > -----------------------------------------------------------------------------
175 The files can be accessed with just `http://localhost:8080/` with the path
176 being just the directory structure of the `public/`
177 folder. For example, if this is your `public/`
187 And you want to access `index.css`, the URL will be:
188 `http://localhost:8080/css/index.css`, and vice versa.