From 4e71c5dacd09d0155420765071f98f13e3ae77a1 Mon Sep 17 00:00:00 2001 From: "Begerad, Stefan" Date: Tue, 17 Jan 2023 21:57:02 +0100 Subject: [PATCH] feat: add back-end --- back-end/cfg.txt | 12 +++++++++++ back-end/main.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++ back-end/makefile | 24 +++++++++++++++++++++ back-end/readme.md | 23 ++++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 back-end/cfg.txt create mode 100644 back-end/main.c create mode 100644 back-end/makefile create mode 100644 back-end/readme.md diff --git a/back-end/cfg.txt b/back-end/cfg.txt new file mode 100644 index 0000000..5e4f533 --- /dev/null +++ b/back-end/cfg.txt @@ -0,0 +1,12 @@ +api = +{ +port = 65535; +}; +pq = +{ +db = "database"; +host = "localhost"; +port = 5432; +secret = "secret"; +user = "user"; +} diff --git a/back-end/main.c b/back-end/main.c new file mode 100644 index 0000000..f651a28 --- /dev/null +++ b/back-end/main.c @@ -0,0 +1,52 @@ +#include +//for use of exit() +#include +//for use of libconfig +#include + +int main(int argc, char *argv[]) { + fprintf(stdout,"main() Started...\n"); + + /*read command line*/ + if (argc > 1){ + printf("main() argv[1]: %s\n", argv[1]); + }else{ + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(1); + } + + config_t cfg; + const char *str; + int port; + + /*initialise new and EMPTY config*/ + config_init(&cfg); + + /* Read the file. If there is an error, report it and exit. */ + if(! config_read_file(&cfg, argv[1])) + { + fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), + config_error_line(&cfg), config_error_text(&cfg)); + /*destroy config, BUT NOT the structure*/ + config_destroy(&cfg); + return(EXIT_FAILURE); + } + + /*lookup db string*/ + if(config_lookup_string(&cfg, "pq.db", &str)){ + printf("db: %s\n", str); + }else{ + fprintf(stderr, "No 'db' setting in configuration file.\n"); + } + + /*lookup port int*/ + if(config_lookup_int(&cfg, "pq.port", &port)){ + printf("port: %d\n", port); + }else{ + fprintf(stderr, "No 'port' setting in configuration file.\n"); + } + + config_destroy(&cfg); + fprintf(stdout,"main() Done.\n"); + return(EXIT_SUCCESS); +} diff --git a/back-end/makefile b/back-end/makefile new file mode 100644 index 0000000..24a0576 --- /dev/null +++ b/back-end/makefile @@ -0,0 +1,24 @@ +# Docu +# +#$@ is the name of the file to be made +#$? is the names of the changed dependents +#$< the name of the related file that caused the action +#$* the prefix shared by target and dependent files +# +# Others +RM = /bin/rm -f +# +# Source, Executable, Includes, Library Defines +EXE = main +# +# Compiler, Linker Defines +CC = /usr/bin/gcc +# +all: main.o + $(CC) main.c -L/usr/lib/x86_64-linux-gnu -lconfig -Wall -o $(EXE) +# +main.o: + $(CC) -c main.c +# Clean Up Objects, Exectuables, Dumps out of source directory +clean: + $(RM) *.o $(EXE) *~ diff --git a/back-end/readme.md b/back-end/readme.md new file mode 100644 index 0000000..7037178 --- /dev/null +++ b/back-end/readme.md @@ -0,0 +1,23 @@ +* install dependencies on Debian Bullseye + +``` +sudo apt install libconfig-dev --no-install-recommends +``` + +* build + +``` +make clean all +``` + +* cleanup + +``` +make clean +``` + +* run + +``` +./main +```