added json response in demo

This commit is contained in:
overflowerror 2021-05-05 00:25:25 +02:00
parent 0bb7de5fa2
commit e1955df2eb
3 changed files with 35 additions and 3 deletions

View file

@ -1,6 +1,6 @@
CC = gcc
LD = gcc
CFLAGS = -Wall -g -std=c99 -ICFloor/src/ -ICson/src/ -D_POSIX_SOURCE -D_XOPEN_SOURCE=500
CFLAGS = -Wall -g -std=c99 -ICFloor/src/ -ICson/src/ -ICson/marshaller/lib/ -D_POSIX_SOURCE -D_XOPEN_SOURCE=500
LDFLAGS = -lpthread -lrt
CFLOOR_LIB = CFloor/libcfloor.a
@ -10,7 +10,7 @@ LIBS = $(CFLOOR_LIB) $(CSON_LIB)
OBJS = obj/router.o obj/request.o obj/base_cfloor.o obj/base_cgi.o
DEPS = $(OBJS:%.o=%.d)
DEMO_OBJS = obj/demo.o
DEMO_OBJS = obj/demo.o obj/entities.tab.o
HAS_MAIN =
NEEDS_MAIN = \
@ -30,6 +30,7 @@ base_cgi: CFLAGS += -DBASE_CGI
base_cgi: HAS_MAIN = "yes"
base_cgi: standalone
standalone: CFLAGS += -Idemo/
standalone: $(DEMO_OBJS) $(OBJS) $(LIBS)
@$(NEEDS_MAIN)
$(LD) $(LDFLAGS) -o $@ $^
@ -38,7 +39,7 @@ $(CFLOOR_LIB):
$(MAKE) -C CFloor/ libcfloor.a
$(CSON_LIB):
$(MAKE) -C Cson/ libcson.a
$(MAKE) -C Cson/ marshaller-lib
-include $(DEPS)
@ -50,11 +51,21 @@ obj/%.o: src/%.c obj
obj:
@mkdir -p obj
Cson/marshaller/marshaller-gen:
$(MAKE) -C Cson/marshaller/ marshaller-gen
obj/entities.tab.o: obj/entities.tab.c
$(CC) $(CFLAGS) -MMD -c -o $@ $<
obj/entities.tab.c: demo/entities.h Cson/marshaller/marshaller-gen
./Cson/marshaller/marshaller-gen -o $@ $<
clean:
@echo "Cleaning up..."
@rm -f obj/*.o
@rm -f obj/*.d
@rm -f obj/*.c
@rm -f standalone
$(MAKE) -C CFloor/ clean
$(MAKE) -C Cson/ clean

View file

@ -3,6 +3,8 @@
#include <controller.h>
#include "entities.h"
GET("/", hello);
GET("/index.*", hello);
@ -14,3 +16,13 @@ GET("/foobar", foobar);
response_t foobar(ctx_t ctx) {
return fileResponse("demo/foobar.txt");
}
GET("/user", user);
response_t user(ctx_t ctx) {
user_t user = {
.username = "overflowerror",
.github = "https://github.com/overflowerror"
};
return jsonResponse(200, user_t, &user);
}

9
demo/entities.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef ENTITIES_H
#define ENTITIES_H
typedef struct {
char* username;
char* github;
} user_t;
#endif