2021-04-29 16:40:41 +00:00
|
|
|
#include <stdlib.h>
|
2021-05-26 20:48:16 +00:00
|
|
|
#include <string.h>
|
2021-04-29 16:40:41 +00:00
|
|
|
|
2021-04-30 11:36:06 +00:00
|
|
|
#include <controller.h>
|
2021-04-29 16:40:41 +00:00
|
|
|
|
2021-05-04 22:25:25 +00:00
|
|
|
#include "entities.h"
|
|
|
|
|
2021-04-29 16:40:41 +00:00
|
|
|
|
|
|
|
GET("/", hello);
|
|
|
|
GET("/index.*", hello);
|
|
|
|
response_t hello(ctx_t ctx) {
|
|
|
|
return rawResponse(200, "Hello World\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
GET("/foobar", foobar);
|
|
|
|
response_t foobar(ctx_t ctx) {
|
2021-04-30 11:36:06 +00:00
|
|
|
return fileResponse("demo/foobar.txt");
|
2021-04-29 16:40:41 +00:00
|
|
|
}
|
2021-05-04 22:25:25 +00:00
|
|
|
|
2021-05-26 19:11:20 +00:00
|
|
|
response_t authenticate(ctx_t ctx) {
|
2021-05-26 20:48:16 +00:00
|
|
|
if (ctx.auth.type != BASIC) {
|
|
|
|
return basicAuthResponse(401, "Protected Area");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(ctx.auth.basic.user, "admin") != 0 ||
|
|
|
|
strcmp(ctx.auth.basic.password, "password") != 0
|
|
|
|
) {
|
|
|
|
// username or password wrong
|
|
|
|
return basicAuthResponse(401, "Protected Area");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-26 19:11:20 +00:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
GET("/user", authenticate, user);
|
2021-05-04 22:25:25 +00:00
|
|
|
response_t user(ctx_t ctx) {
|
|
|
|
user_t user = {
|
|
|
|
.username = "overflowerror",
|
|
|
|
.github = "https://github.com/overflowerror"
|
|
|
|
};
|
|
|
|
|
|
|
|
return jsonResponse(200, user_t, &user);
|
|
|
|
}
|
2021-05-23 15:19:06 +00:00
|
|
|
|
|
|
|
GET("/template", template);
|
|
|
|
response_t template(ctx_t ctx) {
|
|
|
|
return templateResponse(200, "demo.templ", "Page Title", "Overflow");
|
|
|
|
}
|