Skip to content

Commit

Permalink
macros and renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
ngn13 committed Apr 28, 2024
1 parent 1ee4403 commit 9e3278a
Show file tree
Hide file tree
Showing 18 changed files with 209 additions and 155 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
name: Publish Docker Images
on:
push:
branches: ['main']
tags:
- '*'

jobs:
push-image:
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,26 @@ make && sudo make install
### Getting started
#### Hello world application
```c
#include <ctorm/ctorm.h>
#include <ctorm/macros.h>
#include <stdlib.h>
#include <stdio.h>

// this function handles GET request that go to '/'
void hello_world(req_t* req, res_t* res){
// just send the string 'hello world!'
res_send(res, "hello world!");
RES_SEND("hello world!");
}

// main function (entrypoint)
int main(){
// init the application
app_init();
app_t *app = app_new();

// setup a GET route for '/'
GET("/", hello_world);

// start the application on port 8080
app_run("127.0.0.1:8080");
APP_RUN("127.0.0.1:8080");
}
```
Expand Down
9 changes: 5 additions & 4 deletions benchmark/ctorm/main.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include <ctorm/ctorm.h>
#include <ctorm/macros.h>
#include <stdlib.h>
#include <stdio.h>

void hello_world(req_t* req, res_t* res){
res_send(res, "hello world!");
RES_SEND("hello world!");
}

int main(){
log_set(false);
app_init();
app_t *app = app_new();

GET("/", hello_world);
app_run("127.0.0.1:8080");
APP_RUN("127.0.0.1:8080");
}
10 changes: 8 additions & 2 deletions docs/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ GETR("123$", 123_route) // handle routes that end with 123
```c
// static files be served at '/static' path,
// from the './files' direcotry
app_static("/static", "./files");
app_static(app, "/static", "./files");

// if your app_t object is called 'app', then
// you can use the macro
APP_STATIC("/static", "./files");
```
### Setup 404 (all) route
By default, routes that does not match with any other will be redirected
to a 404 page, you set a custom route for this:
```c
app_all(all_route);
app_all(app, all_route);
// or you can use the macro
APP_ALL(all_route);
```
2 changes: 1 addition & 1 deletion docs/log.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ general logging usage.

### General logging functions
```c
info("lalala some information");
info("some information");
warn("there may be something wrong");
error("PANIC!!");
```
Expand Down
6 changes: 6 additions & 0 deletions docs/render.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,11 @@ Ctorm supports simple and limited dynamic rendering.
To render this template, you can use the request render method:
```c
req_render_add(req, "msg", "hello world!");
// or if the req_t object is called 'req', then you
// can use the macro
REQ_RENDER_ADD("msg", "hello world!");

req_render(req, "templates/example.html");
// again, you can also use the macro
REQ_RENDER("templates/example.html");
```
16 changes: 13 additions & 3 deletions docs/req.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,25 @@ char* method = req_method(req);
### Get a header of a request
```c
char* agent = req_header(req, "User-Agent");
if(NULL == agent)
if(NULL == agent){
// user-agent header is not set
return;
}

// or you can use the macro
char* agent = REQ_HEADER("User-Agent");
```

### Get request URL query/parameter
```c
char* msg = req_query(req, "msg");
if(NULL == msg)
// msg parameter is specified
if(NULL == msg){
// msg parameter is specified
return;
}

// or you can use the macro
char* agent = REQ_QUERY("msg");
```

### Parse URL encoded form data
Expand Down
9 changes: 9 additions & 0 deletions docs/res.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,24 @@ res->code = 403;
// you can use local data, it will be copied to heap
// when you call res_send()
res_send(res, "hello world!");

// or you can use the macro
RES_SEND("hello world!");
```
### Sending a file
```c
res_sendfile(res, "files/index.html");
// or you can use the macro
RES_SENDFILE("files/index.html");
```

### Set a header
```c
// again, you can use local data
res_set(res, "Cool", "yes");

// or you can use the macro
RES_SET("Cool", "yes");
```
24 changes: 12 additions & 12 deletions example/main.c
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
#include "../include/ctorm.h"
#include "../include/macros.h"

void handle_notfound(req_t *req, res_t *res) {
res->code = 404;
res_sendfile(res, "./example/404.html");
RES_SENDFILE("./example/404.html");
}

void handle_post(req_t *req, res_t *res) {
body_t *body = req_body_parse(req);
if (NULL == body)
return res_send(res, "bad body");
return RES_SEND("bad body");

char *msg = req_body_get(body, "msg");
if (NULL == msg)
return res_send(res, "bad body");
return RES_SEND("bad body");

res_render_add(res, "msg", msg);
res_render(res, "./example/template/post.html");
RES_RENDER_ADD("msg", msg);
RES_RENDER("./example/template/post.html");

info("Message: %s", msg);
res_set(res, "Cool", "yes");
RES_SET("Cool", "yes");
req_body_free(body);
}

void handle_get(req_t *req, res_t *res) {
res_render(res, "./example/template/index.html");
RES_RENDER("./example/template/index.html");
}

int main() {
app_init();
app_t *app = app_new();

GET("/", handle_get);
POST("/post", handle_post);

app_static("/static", "./example/static");
app_all(handle_notfound);
APP_STATIC("/static", "./example/static");
APP_ALL(handle_notfound);

if (!app_run("0.0.0.0:8080"))
if (!APP_RUN("0.0.0.0:8080"))
error("app failed: %s\n", geterror());
}
36 changes: 18 additions & 18 deletions include/ctorm.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,24 @@ typedef struct app_t {
int socket;
} app_t;

void app_init();
bool app_run(const char *);
bool app_add(char *, bool, char *, route_t);
void app_route(req_t *, res_t *);
void app_static(char *, char *);
app_t *app_new();
bool app_run(app_t*, const char *);
bool app_add(app_t *, char *, bool, char *, route_t);
void app_route(app_t *, req_t *, res_t *);
void app_static(app_t *, char *, char *);
void app_404(req_t *, res_t *);
void app_all(route_t);
void app_all(app_t *, route_t);

#define GET(path, func) app_add("GET", false, path, func)
#define PUT(path, func) app_add("PUT", false, path, func)
#define HEAD(path, func) app_add("HEAD", false, path, func)
#define POST(path, func) app_add("POST", false, path, func)
#define DELETE(path, func) app_add("DELETE", false, path, func)
#define OPTIONS(path, func) app_add("OPTIONS", false, path, func)
#define GET(path, func) app_add(app, "GET", false, path, func)
#define PUT(path, func) app_add(app, "PUT", false, path, func)
#define HEAD(path, func) app_add(app, "HEAD", false, path, func)
#define POST(path, func) app_add(app, "POST", false, path, func)
#define DELETE(path, func) app_add(app, "DELETE", false, path, func)
#define OPTIONS(path, func) app_add(app, "OPTIONS", false, path, func)

#define GETR(path, func) app_add("GET", true, path, func)
#define PUTR(path, func) app_add("PUT", true, path, func)
#define HEADR(path, func) app_add("HEAD", true, path, func)
#define POSTR(path, func) app_add("POST", true, path, func)
#define DELETER(path, func) app_add("DELETE", true, path, func)
#define OPTIONSR(path, func) app_add("OPTIONS", true, path, func)
#define GETR(path, func) app_add(app, "GET", true, path, func)
#define PUTR(path, func) app_add(app, "PUT", true, path, func)
#define HEADR(path, func) app_add(app, "HEAD", true, path, func)
#define POSTR(path, func) app_add(app, "POST", true, path, func)
#define DELETER(path, func) app_add(app, "DELETE", true, path, func)
#define OPTIONSR(path, func) app_add(app, "OPTIONS", true, path, func)
13 changes: 7 additions & 6 deletions include/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
#include <stdbool.h>
#include <stddef.h>

typedef enum t_method {
typedef enum method_t{
METHOD_GET = 0,
METHOD_HEAD = 1,
METHOD_POST = 2,
METHOD_PUT = 3,
METHOD_DELETE = 4,
METHOD_OPTIONS = 5,
} t_method;
} method_t;

typedef struct t_method_map {
t_method code;
typedef struct method_map_t {
method_t code;
char *name;
bool body;
} t_method_map;
} method_map_t;

extern t_method_map http_method_map[];
extern method_map_t http_method_map[];
extern size_t http_method_sz;

int http_methodid(char *);
17 changes: 17 additions & 0 deletions include/macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "ctorm.h"

#define APP_RUN(host) app_run(app, host)
#define APP_ALL(route) app_all(app, route)
#define APP_STATIC(path, dir) app_static(app, path, dir)

#define REQ_HEADER(header) req_header(req, header)
#define REQ_QUERY(query) req_query(req, query)

#define RES_SEND(text) res_send(res, text)
#define RES_SENDFILE(path) res_sendfile(res, path)
#define RES_SET(header, value) res_set(res, header, value)

#define RES_RENDER_ADD(key, value) res_render_add(res, key, value)
#define RES_RENDER(path) res_render(res, path)
21 changes: 10 additions & 11 deletions include/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
#define BUF_MAX 8192

typedef enum parse_res {
P_CONT = 0,
P_FAIL = 1,
P_OK = 2,
RES_CONT = 0,
RES_FAIL = 1,
RES_OK = 2,
} parse_res;

typedef enum handle_st {
H_CONFAIL = 0,
H_BADREQ = 1,
H_OK = 2,
} handle_st;
typedef enum parse_ret {
RET_CONFAIL = 0,
RET_BADREQ = 1,
RET_OK = 2,
} parse_ret;

enum parse_st {
enum parse_state {
METHOD = 0,
SPACE = 1,
PATH = 2,
Expand All @@ -25,8 +25,7 @@ enum parse_st {
BODY = 7,
};

handle_st handle_test(req_t *, int);
handle_st handle_request(req_t *, int);
parse_ret parse_request(req_t *, int);

parse_res parse_method(req_t *, int, char *);
parse_res parse_path(req_t *, int, char *);
Expand Down
2 changes: 1 addition & 1 deletion include/req.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "table.h"

typedef struct req_t {
t_method method;
method_t method;
char *fullpath;
char *encpath;
char *path;
Expand Down
10 changes: 7 additions & 3 deletions include/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include <netinet/in.h>
#include <stdbool.h>

#define THREADS 50
typedef int t_socketop[2];
bool start_socket(app_t *, char *, int);
#define THREADS 30
typedef struct socket_args_t {
app_t *app;
int socket;
} socket_args_t;

bool socket_start(app_t *, char *, int);
Loading

0 comments on commit 9e3278a

Please sign in to comment.