Skip to content

Commit b8a87bf

Browse files
committed
updated example
1 parent a8f9684 commit b8a87bf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+5047
-7
lines changed

doc/api/errors.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Error Handling
2+
3+
You have the freedom to handle errors however you like. However, Lavandula provides a interface for returning JSON errors from your controllers.
4+
5+
6+
7+
```
8+
9+
```

examples/foodApp/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# My Lavandula Project
2+
3+
## Getting Started
4+
5+
Run your project:
6+
```bash
7+
lavu run
8+
```

examples/foodApp/app/app.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "../lavandula/include/lavandula.h"
2+
#include "routes.h"
3+
4+
int main() {
5+
AppBuilder builder = createBuilder();
6+
usePort(&builder, 8080);
7+
8+
App app = build(builder);
9+
10+
registerRoutes(&app);
11+
12+
runApp(&app);
13+
14+
return 0;
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "../../lavandula/include/lavandula.h"
2+
3+
appRoute(registerUser, ctx) {
4+
return notImplementedYet();
5+
}
6+
7+
appRoute(loginUser, ctx) {
8+
return notImplementedYet();
9+
}
10+
11+
appRoute(logoutUser, ctx) {
12+
return notImplementedYet();
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef controllers_h
2+
#define controllers_h
3+
4+
#include "../../lavandula/include/lavandula.h"
5+
6+
appRoute(registerUser, ctx);
7+
appRoute(loginUser, ctx);
8+
appRoute(logoutUser, ctx);
9+
10+
#endif
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "../../lavandula/include/lavandula.h"
2+
3+
appRoute(getDonations, ctx) {
4+
return notImplementedYet();
5+
}
6+
7+
appRoute(getDonation, ctx) {
8+
return notImplementedYet();
9+
}
10+
11+
appRoute(createDonation , ctx) {
12+
return notImplementedYet();
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef app_middleware_h
2+
#define app_middleware_h
3+
4+
#include "../../lavandula/include/lavandula.h"
5+
6+
middleware(registerUserValidator, ctx, m);
7+
middleware(loginUserValidator, ctx, m);
8+
middleware(logoutUserValidator, ctx, m);
9+
10+
#endif
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "../../lavandula/include/lavandula.h"
2+
#include "middleware.h"
3+
4+
HttpResponse successOrFailureResponse(bool success, char *message) {
5+
JsonBuilder *builder = jsonBuilder();
6+
jsonPutBool(builder, "success", success);
7+
jsonPutString(builder, "message", message);
8+
9+
char *json = jsonStringify(builder);
10+
freeJsonBuilder(builder);
11+
12+
return response(json, HTTP_BAD_REQUEST, APPLICATION_JSON);
13+
}
14+
15+
HttpResponse success(char *message) {
16+
return successOrFailureResponse(true, message);
17+
}
18+
19+
HttpResponse failure(char *message) {
20+
return successOrFailureResponse(false, message);
21+
22+
}
23+
24+
middleware(registerUserValidator, ctx, m) {
25+
JsonBuilder *body = jsonParse(ctx.request.body);
26+
if (!body) {
27+
return failure("Error: no JSON body provided.");
28+
}
29+
30+
if (!jsonHasKey(body, "username") || !jsonHasKey(body, "password")) {
31+
freeJsonBuilder(body);
32+
return failure("Missing 'username' or 'password' in request body");
33+
}
34+
freeJsonBuilder(body);
35+
36+
return next(ctx, m);
37+
}
38+
39+
middleware(loginUserValidator, ctx, m) {
40+
JsonBuilder *body = jsonParse(ctx.request.body);
41+
if (!body) {
42+
return failure("Error: no JSON body provided.");
43+
}
44+
45+
if (!jsonHasKey(body, "username") || !jsonHasKey(body, "password")) {
46+
freeJsonBuilder(body);
47+
return failure("Missing 'username' or 'password' in request body");
48+
}
49+
freeJsonBuilder(body);
50+
51+
return next(ctx, m);
52+
}
53+
54+
middleware(logoutUserValidator, ctx, m) {
55+
return next(ctx, m);
56+
}

examples/foodApp/app/routes.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "../lavandula/include/lavandula.h"
2+
3+
#include "controllers/controllers.h"
4+
#include "middleware/middleware.h"
5+
6+
void registerRoutes(App *app) {
7+
Route registerUserRoute = get(app, "/api/register", registerUser);
8+
useLocalMiddleware(&registerUserRoute, registerUserValidator);
9+
10+
Route loginUserRoute = post(app, "/api/login", loginUser);
11+
useLocalMiddleware(&loginUserRoute, loginUserValidator);
12+
13+
Route logoutUserRoute = post(app, "/api/logout", logoutUser);
14+
useLocalMiddleware(&logoutUserRoute, logoutUserValidator);
15+
}

examples/foodApp/app/routes.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef routes_h
2+
#define routes_h
3+
4+
#include "../lavandula/include/lavandula.h"
5+
6+
void registerRoutes(App *app);
7+
8+
#endif

0 commit comments

Comments
 (0)