131 lines
3.3 KiB
C
131 lines
3.3 KiB
C
#define _POSIX_C_SOURCE 199309L
|
|
|
|
#include <time.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include <vessel/web.h>
|
|
#include <vessel/mem.h>
|
|
#include <vessel/log.h>
|
|
#include <vessel/http.h>
|
|
#include <vessel/sock.h>
|
|
#include <vessel/main.h>
|
|
#include <vessel/stream.h>
|
|
#include <vessel/request.h>
|
|
#include <vessel/response.h>
|
|
|
|
volatile uint64_t n = 0;
|
|
|
|
static void index(Proxy *p) {
|
|
Response_init(&p->res, &p->w->fp, p->req.version, 200);
|
|
|
|
Response_header(&p->w->fp, "content-type", "text/html");
|
|
|
|
/* clang-format off */
|
|
Response_content_and_lengthf(&p->w->fp,
|
|
"<!DOCTYPE html>"
|
|
"<html lang=\"en\">"
|
|
" <head>"
|
|
" <meta charset=\"UTF-8\">"
|
|
" <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">"
|
|
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"
|
|
" <title>Home Page</title>"
|
|
" </head>"
|
|
""
|
|
" <body>"
|
|
" <h1>Hello!</h1>"
|
|
" <p>This is a sample website. Your request number: %lu.</p>"
|
|
" </body>"
|
|
"</html>",
|
|
++n
|
|
);
|
|
/* clang-format on */
|
|
}
|
|
|
|
static void notfound(Proxy *p) {
|
|
Response_init(&p->res, &p->w->fp, p->req.version, 404);
|
|
|
|
Response_header(&p->w->fp, "content-type", "text/html");
|
|
|
|
/* clang-format off */
|
|
Response_content_and_lengthf(&p->w->fp,
|
|
"<!DOCTYPE html>"
|
|
"<html lang=\"en\">"
|
|
" <head>"
|
|
" <meta charset=\"UTF-8\">"
|
|
" <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">"
|
|
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"
|
|
" <title>404 Not Found</title>"
|
|
" </head>"
|
|
""
|
|
" <body>"
|
|
" <h1>HTTP/404: Not Found</h1>"
|
|
" <p>Requested resource was not found on the server. Your request number: %lu.</p>"
|
|
" </body>"
|
|
"</html>",
|
|
++n
|
|
);
|
|
/* clang-format on */
|
|
}
|
|
|
|
int main(void) {
|
|
SockServer s = { 0 };
|
|
SockServerHook hooks[] = HOOKS(WEBSERVER_HOOKS);
|
|
|
|
s.host = "127.0.0.1";
|
|
s.port = 8080;
|
|
s.lg = LOG_ALL;
|
|
s.threads = 128;
|
|
|
|
if (!SockServer_create_generic(&s)) {
|
|
log_error(s.lg, "Failed to create a generic TCP server.");
|
|
return 1;
|
|
}
|
|
|
|
if (!SockServer_init(&s, SEC2TO(0.01))) {
|
|
log_error(s.lg, "Failed to create a generic TCP server.");
|
|
SockServer_destroy(&s);
|
|
return 1;
|
|
}
|
|
|
|
if (!SockServer_setup_signals(&s)) {
|
|
log_error(s.lg, "Failed to handle signals for the server.");
|
|
SockServer_destroy(&s);
|
|
return 1;
|
|
}
|
|
|
|
Route routes[] = ROUTES(
|
|
{
|
|
NULL,
|
|
"/",
|
|
RouteType_normal,
|
|
index,
|
|
},
|
|
{
|
|
NULL,
|
|
"/%p:_",
|
|
RouteType_not_found,
|
|
notfound,
|
|
});
|
|
|
|
Router router = { 0 };
|
|
|
|
router.routes = routes;
|
|
router.hooks = NULL;
|
|
|
|
if (!WebServer_init(&s, &router)) {
|
|
log_error(s.lg, "Failed to init the web server.");
|
|
SockServer_destroy(&s);
|
|
return 1;
|
|
}
|
|
|
|
if (!SockServer_start(&s, hooks)) {
|
|
log_error(s.lg, "Failed to start the server.");
|
|
SockServer_destroy(&s);
|
|
return 1;
|
|
}
|
|
|
|
SockServer_destroy(&s);
|
|
|
|
return 0;
|
|
}
|