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
@@ -0,0 +1 @@
See detailed tutorial at https://mongoose.ws/tutorials/webui/webui-push/
+80
View File
@@ -0,0 +1,80 @@
// Copyright (c) 2022 Cesanta Software Limited
// All rights reserved
//
// This example demonstrates how to push data over a REST API
// We're going to send a JSON array of some integer values, s_data.
// Periodically, s_data changes, which is tracked by s_version.
// Clients inform their current version, we only send on version mismatch,
// or when version is zero (client has no data); this minimizes traffic and
// avoids updating the UI unnecesarily.
//
// 1. Start this server, type `make`
// 2. Open http://localhost:8000 in your browser
#include "mongoose.h"
static const char *s_listen_on = "http://localhost:8000";
static const char *s_root_dir = "web_root";
#define DATA_SIZE 10 // Total number of elements
static int s_data[DATA_SIZE]; // Simulate some complex data
static long s_version = 0; // Data "version"
static long getparam(struct mg_http_message *hm, const char *json_path) {
double dv = 0;
mg_json_get_num(hm->body, json_path, &dv);
return dv;
}
static size_t printdata(mg_pfn_t out, void *ptr, va_list *ap) {
const char *comma = "";
size_t n = 0;
for (int i = 0; i < DATA_SIZE; ++i) {
n += mg_xprintf(out, ptr, "%s%d", comma, s_data[i]);
comma = ",";
}
return n;
(void) ap;
}
static void fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = ev_data;
if (mg_match(hm->uri, mg_str("/api/data"), NULL)) {
long version = getparam(hm, "$.version");
if (version > 0 && version == s_version) {
// Version match: no changes
mg_http_reply(c, 200, "Content-Type: application/json\r\n",
"{%m:%m,%m:%ld}\n", MG_ESC("status"), MG_ESC("no change"),
MG_ESC("version"), version);
} else {
// Version mismatch, return data
mg_http_reply(c, 200, "Content-Type: application/json\r\n",
"{%m:%ld,%m:[%M]}\n", MG_ESC("version"), s_version,
MG_ESC("data"), printdata);
}
} else {
struct mg_http_serve_opts opts = {.root_dir = s_root_dir};
mg_http_serve_dir(c, hm, &opts);
}
}
}
static void timer_fn(void *arg) {
for (int i = 0; i < DATA_SIZE; i++) {
s_data[i] = rand();
}
s_version++;
(void) arg;
}
int main(void) {
struct mg_mgr mgr;
mg_mgr_init(&mgr);
srand(time(NULL));
mg_timer_add(&mgr, 5000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn, NULL);
mg_http_listen(&mgr, s_listen_on, fn, NULL);
for (;;) mg_mgr_poll(&mgr, 1000);
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,39 @@
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Data push using REST demo</h1>
<div id="UI" style="background: #fff; border:1px solid black; height: 10em; padding: 0.5em; overflow:auto;"></div>
<div style="margin-top: 1em;">Event log:</div>
<div id="log" style="background: #eee; height: 10em; padding: 0.5em; overflow:auto;"></div>
</body>
<script>
var ui = document.getElementById('UI');
var log = document.getElementById('log');
var version = 0;
const refreshUI = d => ui.innerHTML = JSON.stringify(d, null, 2)
const watch = function() {
var tid;
fetch('/api/data', {method: 'POST', body:JSON.stringify({version})})
.then(r => r.json())
.then(r => {
if (r.status) {
log.innerHTML += 'Status: ' + r.status + '<br/>';
} else {
version = r.version;
log.innerHTML += 'Version: ' + version + '<br/>';
refreshUI(r.data);
}
})
.catch(err => console.log(err));
clearTimeout(tid);
tid = setTimeout(watch, 1000);
};
watch();
</script>
</html>