-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
81 lines (62 loc) · 2.53 KB
/
Copy pathMakefile
File metadata and controls
81 lines (62 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Note these compile rules will be invoked by Dockerfile "RUN make" command and they also work on the host machine if microhttpd lib is installed
CC = gcc
CFLAGS = -Wall -g -I/usr/include/postgresql
LIBS = -lmicrohttpd -L/usr/lib/x86_64-linux-gnu -lpq
SRCDIR = src
BINDIR = bin
SOURCES = $(wildcard $(SRCDIR)/*.c)
OBJECTS = $(patsubst $(SRCDIR)/%.c, $(BINDIR)/%.o, $(SOURCES))
all: $(BINDIR)/main
$(BINDIR)/main: $(OBJECTS)
mkdir -p $(BINDIR)
$(CC) $(CFLAGS) -o $@ $^ $(LIBS)
$(BINDIR)/%.o: $(SRCDIR)/%.c | $(BINDIR)
$(CC) $(CFLAGS) -c -o $@ $<
$(BINDIR):
mkdir -p $@
clean:
rm -f $(BINDIR)/*.o $(BINDIR)/main
clear: ## clears the terminal output
printf '\ec';printf '\e[3J';
build: clear
docker compose build
run: build
docker compose up -d
@echo "API running at http://localhost:8888"
stop: clear
docker compose down
logs: clear
docker compose logs -f
# --- Database initial migration ---
dbwait: run ## waits until postgres is ready to accept connections
@. ./.env; \
echo "Waiting for PostgreSQL to be ready..."; \
for i in $$(seq 1 30); do \
docker compose exec -T rest-api-c-db pg_isready -U $$POSTGRES_USER -d postgres >/dev/null 2>&1 && echo "PostgreSQL is ready!" && exit 0; \
echo "Waiting... ($$i/30)"; \
sleep 2; \
done; \
echo "PostgreSQL did not become ready in time"; \
exit 1
dbcreate: dbwait ## creates the rest_api_c database if not exists
docker compose exec -T rest-api-c-db psql -U rest_api_c -d postgres -c "CREATE DATABASE rest_api_c;" 2>/dev/null || true
dbinit: dbcreate ## creates the users table if not exists
docker compose exec -T rest-api-c-db psql -U rest_api_c -d rest_api_c -c "CREATE TABLE IF NOT EXISTS users (id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL);"
@echo "API running at http://localhost:8888"
# --- CRUD tasks for testing ---
healthcheck: clear
curl -s http://localhost:8888/
listusers: clear
curl -s http://localhost:8888/users
getuserbyid: clear ## make getuserbyid id=1
curl -s http://localhost:8888/users/$(id)
createuser: clear ## make createuser name="Test User" email="test.user@mail.com"
curl -s -X POST http://localhost:8888/users \
-H "Content-Type: application/json" \
-d '{"name": "$(name)", "email": "$(email)"}'
updateuser: clear ## make updateuser id=1 name="New Test User" email="new.test.user@mail.com"
curl -s -X PUT http://localhost:8888/users/$(id) \
-H "Content-Type: application/json" \
-d '{"name": "$(name)", "email": "$(email)"}'
deleteuser: clear ## make deleteuser id=1
curl -s -X DELETE http://localhost:8888/users/$(id)