Skip to content

Commit

Permalink
fix locals macro and add new example for locals
Browse files Browse the repository at this point in the history
Signed-off-by: ngn <[email protected]>
  • Loading branch information
ngn13 committed Jan 15, 2025
1 parent 023ae95 commit d4bcfec
Show file tree
Hide file tree
Showing 20 changed files with 126 additions and 67 deletions.
13 changes: 13 additions & 0 deletions example/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM ghcr.io/ngn13/ctorm:latest

WORKDIR /example
COPY . ./

RUN mkdir /dist
RUN make \
DONT_CHECK_LIBCTORM=1 \
INCD=/usr/include/ctorm \
DIST=/dist

WORKDIR /
CMD ["/example/init.sh"]
6 changes: 4 additions & 2 deletions example/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ BINS = $(patsubst %,$(DIST)/example_%,$(DIRS))
all: $(BINS)

$(DIST)/libctorm.so:
ifeq (,$(wildcard $(DIST)/libctorm.so))
$(error you should first compile libctorm)
ifneq ($(DONT_CHECK_LIBCTORM), 1)
ifeq (,$(wildcard $(DIST)/libctorm.so))
$(error you should first compile libctorm)
endif
endif

$(DIST)/example_%: %/main.c $(DIST)/libctorm.so
Expand Down
12 changes: 0 additions & 12 deletions example/echo/Dockerfile

This file was deleted.

2 changes: 1 addition & 1 deletion example/echo/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int main() {
ctorm_app_all(app, GET_notfound);

// run the app
if (!ctorm_app_run(app, "0.0.0.0:8080"))
if (!ctorm_app_run(app, "0.0.0.0:8081"))
ctorm_fail("failed to start the app: %s", ctorm_geterror());

// clean up
Expand Down
10 changes: 0 additions & 10 deletions example/hello/Dockerfile

This file was deleted.

24 changes: 24 additions & 0 deletions example/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

# docker init script

examples=()
pids=()

for example in dist/*; do
examples+=("${example}")
done

for i in "${!examples[@]}"; do
"./${examples[$i]}" &
pids[$i]=$!
done

for i in "${!pids[@]}"; do
if ! wait "${pids[$i]}"; then
echo "${examples[$i]} exited with a non zero exit-code (${?})"
exit 1
fi
done

exit 0
34 changes: 34 additions & 0 deletions example/locals/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <ctorm.h>

void GET_index(ctorm_req_t *req, ctorm_res_t *res) {
char *username = REQ_LOCAL("username");
RES_FMT("username: %s", username);
}

void username_middleware(ctorm_req_t *req, ctorm_res_t *res) {
char *username = REQ_QUERY("username");

if (NULL == username) {
RES_SEND("no username provided");
REQ_CANCEL();
return;
}

REQ_LOCAL("username", username);
}

int main() {
// create the app
ctorm_app_t *app = ctorm_app_new(NULL);

// setup the routes
MIDDLEWARE_GET(app, "/*", username_middleware);
GET(app, "/", GET_index);

// run the app
if (!ctorm_app_run(app, "0.0.0.0:8083"))
ctorm_fail("failed to start the app: %s", ctorm_geterror());

// clean up
ctorm_app_free(app);
}
10 changes: 0 additions & 10 deletions example/middleware/Dockerfile

This file was deleted.

2 changes: 1 addition & 1 deletion example/middleware/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ int main() {
GET(app, "/", GET_index);
GET(app, "/users", GET_user_list);

if (!ctorm_app_run(app, "0.0.0.0:8080"))
if (!ctorm_app_run(app, "0.0.0.0:8084"))
ctorm_fail("failed to start the app: %s", ctorm_geterror());

ctorm_app_free(app);
Expand Down
10 changes: 0 additions & 10 deletions example/params/Dockerfile

This file was deleted.

2 changes: 1 addition & 1 deletion example/params/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ int main() {
GET(app, "/echo/:param/*", GET_param);

// run the app
if (!ctorm_app_run(app, "0.0.0.0:8080"))
if (!ctorm_app_run(app, "0.0.0.0:8082"))
ctorm_fail("failed to start the app: %s", ctorm_geterror());

// clean up
Expand Down
2 changes: 1 addition & 1 deletion inc/ctorm.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@
* @param[in] ... Local value
*/
#define REQ_LOCAL(local, ...) ctorm_req_local(req, local, ##__VA_ARGS__)
#define REQ_LOCAL(local, ...) ctorm_req_local(req, local, ##__VA_ARGS__, NULL)

/*!
Expand Down
31 changes: 24 additions & 7 deletions scripts/test.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
#!/bin/bash

set -e

export LD_LIBRARY_PATH='./dist'
examples=("hello" "echo" "params" "middleware")
examples=(
"hello"
"echo"
"params"
"locals"
"middleware"
)
index="${1}"

function run_example(){
echo "testing example: ${1}"
(./dist/example_${1} & 2>/dev/null)
echo "${1}: testing..."

./dist/example_${1} &
./scripts/test_${1}.sh
killall -9 "example_${1}"

res=$?
kill -9 $!

if [ $res -ne 0 ]; then
echo "${1}: failed"
return 1
fi

echo "${1}: success"
return 0
}

if [ ! -z "${index}" ]; then
Expand All @@ -26,5 +41,7 @@ if [ ! -z "${index}" ]; then
fi

for example in "${examples[@]}"; do
run_example "${example}"
if ! run_example "${example}"; then
exit 1
fi
done
2 changes: 1 addition & 1 deletion scripts/test_echo.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

data=$(curl -X POST 'http://127.0.0.1:8080/post' \
data=$(curl -X POST 'http://127.0.0.1:8081/post' \
-H 'Content-Type: application/x-www-form-urlencoded' \
--data 'msg=testing' --silent)

Expand Down
16 changes: 16 additions & 0 deletions scripts/test_locals.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

no_user=$(curl --silent -w "%{http_code}" 'http://127.0.0.1:8083/')
with_user=$(curl --silent -w "%{http_code}" 'http://127.0.0.1:8083/?username=ngn')

if [[ "${no_user}" != "no username provided200" ]]; then
echo 'fail (1)'
exit 1
fi

if [[ "${with_user}" != "username: ngn200" ]]; then
echo 'fail (2)'
exit 1
fi

echo 'success'
6 changes: 3 additions & 3 deletions scripts/test_middleware.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

test_users() {
users=$(curl --silent 'http://127.0.0.1:8080/users' | jq -r '.list[0]| "\(.name) \(.age)"')
users=$(curl --silent 'http://127.0.0.1:8084/users' | jq -r '.list[0]| "\(.name) \(.age)"')

if [[ "${users}" == "${1}" ]]; then
return 0
Expand All @@ -15,12 +15,12 @@ if ! test_users 'John 23'; then
exit 1
fi

curl --silent 'http://127.0.0.1:8080/user/add' \
curl --silent 'http://127.0.0.1:8084/user/add' \
-H 'Content-Type: application/json' \
-H 'Authorization: secretpassword' \
--data '{"name": "test", "age": 42}' -o /dev/null

curl -X DELETE --silent 'http://127.0.0.1:8080/user/delete?name=John' \
curl -X DELETE --silent 'http://127.0.0.1:8084/user/delete?name=John' \
-H 'Authorization: secretpassword' -o /dev/null

if ! test_users 'test 42'; then
Expand Down
4 changes: 2 additions & 2 deletions scripts/test_params.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

res_200=$(curl --silent -w "%{http_code}" 'http://127.0.0.1:8080/echo/just%20testing/empty')
res_404=$(curl --silent -o /dev/null -w "%{http_code}" 'http://127.0.0.1:8080/echo/test')
res_200=$(curl --silent -w "%{http_code}" 'http://127.0.0.1:8082/echo/just%20testing/empty')
res_404=$(curl --silent -o /dev/null -w "%{http_code}" 'http://127.0.0.1:8082/echo/test')

if [[ "${res_200}" != "param: just testing200" ]]; then
echo 'fail (1)'
Expand Down
2 changes: 1 addition & 1 deletion src/app.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* ctorm | Simple web framework for C
* Written by ngn (https://ngn.tf) (2024)
* Written by ngn (https://ngn.tf) (2025)
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down
3 changes: 0 additions & 3 deletions src/req.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
#include <stdlib.h>
#include <string.h>

#include <errno.h>
#include <stdio.h>

#define rdebug(f, ...) \
debug("(" FG_BOLD "socket " FG_CYAN "%d" FG_RESET FG_BOLD " Request " FG_CYAN "0x%p" FG_RESET ") " f, \
req->con->socket, \
Expand Down
2 changes: 0 additions & 2 deletions src/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ bool ctorm_socket_parse_host(const char *host, struct addrinfo *info) {
return false;
}

debug("name: %s port: %s", hostname, hostport);

struct addrinfo *hostinfo = NULL, *cur = NULL;
int port = 0;

Expand Down

0 comments on commit d4bcfec

Please sign in to comment.