move to new skeleton

This commit is contained in:
Sergio R. Caprile committed 2024-04-29 11:23:00 -03:00
1 parent d391bca2d3
commit fe0b144b84
63 files changed
+20 -20

No files matched your search

+25
View File
@@ -0,0 +1,25 @@
PROG ?= example # Program we are building
DELETE = rm -rf # Command to remove files
OUT ?= -o $(PROG) # Compiler argument for output file
SOURCES = main.c mongoose.c # Source code files
CFLAGS = -W -Wall -Wextra -g -I. # Build options
# Mongoose build options. See https://mongoose.ws/documentation/#build-options
CFLAGS_MONGOOSE += -DMG_ENABLE_LINES
ifeq ($(OS),Windows_NT) # Windows settings. Assume MinGW compiler. To use VC: make CC=cl CFLAGS=/MD OUT=/Feprog.exe
PROG ?= example.exe # Use .exe suffix for the binary
CC = gcc # Use MinGW gcc compiler
CFLAGS += -lws2_32 # Link against Winsock library
DELETE = cmd /C del /Q /F /S # Command prompt command to delete files
OUT ?= -o $(PROG) # Build output
endif
all: $(PROG) # Default target. Build and run program
$(RUN) ./$(PROG) $(ARGS)
$(PROG): $(SOURCES) # Build program from sources
$(CC) $(SOURCES) $(CFLAGS) $(CFLAGS_MONGOOSE) $(CFLAGS_EXTRA) $(OUT)
clean: # Cleanup. Delete built program and all build artifacts
$(DELETE) $(PROG) *.o *.obj *.exe *.dSYM
+1
View File
@@ -0,0 +1 @@
See detailed tutorial at https://mongoose.ws/tutorials/webui/webui-rest/
+47
View File
@@ -0,0 +1,47 @@
// Copyright (c) 2022 Cesanta Software Limited
// All rights reserved
//
// REST basics example
// It implements the following endpoints:
// /api/f1 - respond with a simple mock result
// /api/sum - respond with the result of adding two numbers
// any other URI serves static files from s_root_dir
// Results are JSON strings
#include "mongoose.h"
static const char *s_http_addr = "http://localhost:8000"; // HTTP port
static const char *s_root_dir = "web_root";
static void fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
if (mg_match(hm->uri, mg_str("/api/f1"), NULL)) {
mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{%m:%d}\n",
MG_ESC("result"), 123);
} else if (mg_match(hm->uri, mg_str("/api/sum"), NULL)) {
// Attempt to fetch a JSON array from the body, hm->body
struct mg_str json = hm->body;
double num1, num2;
if (mg_json_get_num(json, "$[0]", &num1) &&
mg_json_get_num(json, "$[1]", &num2)) {
// Success! create a JSON response
mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{%m:%g}\n",
MG_ESC("result"), num1 + num2);
}
} else {
struct mg_http_serve_opts opts = {.root_dir = s_root_dir};
mg_http_serve_dir(c, hm, &opts);
}
}
}
int main(void) {
struct mg_mgr mgr; // Event manager
mg_log_set(MG_LL_DEBUG); // Set log level
mg_mgr_init(&mgr); // Initialise event manager
mg_http_listen(&mgr, s_http_addr, fn, NULL); // Create HTTP listener
for (;;) mg_mgr_poll(&mgr, 1000); // Infinite event loop
mg_mgr_free(&mgr);
return 0;
}
+1
View File
@@ -0,0 +1 @@
../../../mongoose.c
+1
View File
@@ -0,0 +1 @@
../../../mongoose.h
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<body>
<h1>REST basics demo</h1>
<div style="height: 0.3em;">&nbsp;</div>
<button id="btn1">Call F1</button>
<button id="btn2">Call SUM(1, 2)</button>
<div style="margin-top: 1em;">Action log:</div>
<div id="log" style="background: #eee; height: 10em; padding: 0.5em; overflow:auto;"><div>
</body>
<script>
var E = function(id) { return document.getElementById(id); };
var btn1 = E('btn1'), btn2 = E('btn2'), msglog = E('log');
var enable = function(en) { btn1.disabled = btn2.disabled = !en; };
var log = text => msglog.innerHTML += text + '<br/>\n';
enable(true);
btn1.onclick = ev => fetch('/api/f1')
.then(r => r.json())
.then(r => {
console.log(r);
log('GET /api/f1: ' + JSON.stringify(r) + ' -> ' + JSON.stringify(r.result))
})
.catch(err => console.log(err));
const getsum = (a, b) =>
fetch('/api/sum', {method: 'POST', body:JSON.stringify([a, b])})
.then(r => r.json())
.catch(err => console.log(err));
btn2.onclick = ev => getsum(1, 2)
.then(r => {
console.log(r);
log('POST [1, 2] to /api/sum: ' + JSON.stringify(r) + ' -> ' + JSON.stringify(r.result))
})
.catch(err => console.log(err));
</script>
</html>