2022-07-12 17:12:26 -03:00
|
|
|
// Copyright (c) 2022 Cesanta Software Limited
|
|
|
|
// All rights reserved
|
|
|
|
//
|
|
|
|
// UI example
|
|
|
|
// It implements the following endpoints:
|
|
|
|
// /api/config/get - respond with current config
|
|
|
|
// /api/config/set - POST a config change
|
|
|
|
// any other URI serves static files from s_root_dir
|
|
|
|
// Data and 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";
|
|
|
|
#define MQTT_SERVER "mqtt://broker.hivemq.com:1883"
|
|
|
|
#define MQTT_PUBLISH_TOPIC "mg/my_device"
|
|
|
|
#define MQTT_SUBSCRIBE_TOPIC "mg/#"
|
|
|
|
|
2023-04-14 10:11:12 -03:00
|
|
|
static struct config {
|
|
|
|
char *url, *pub, *sub;
|
|
|
|
} s_config;
|
2022-07-12 17:12:26 -03:00
|
|
|
|
|
|
|
// Try to update a single configuration value
|
|
|
|
static void update_config(struct mg_str json, const char *path, char **value) {
|
|
|
|
char *jval;
|
|
|
|
if ((jval = mg_json_get_str(json, path)) != NULL) {
|
2022-07-18 16:21:02 -03:00
|
|
|
free(*value);
|
2022-07-12 17:12:26 -03:00
|
|
|
*value = strdup(jval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 17:34:34 -03:00
|
|
|
static void fn(struct mg_connection *c, int ev, void *ev_data) {
|
2022-07-12 17:12:26 -03:00
|
|
|
if (ev == MG_EV_OPEN && c->is_listening) {
|
|
|
|
s_config.url = strdup(MQTT_SERVER);
|
|
|
|
s_config.pub = strdup(MQTT_PUBLISH_TOPIC);
|
|
|
|
s_config.sub = strdup(MQTT_SUBSCRIBE_TOPIC);
|
|
|
|
} else if (ev == MG_EV_HTTP_MSG) {
|
|
|
|
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
2024-04-17 16:13:10 -03:00
|
|
|
if (mg_match(hm->uri, mg_str("/api/config/get"), NULL)) {
|
2022-07-12 17:12:26 -03:00
|
|
|
mg_http_reply(c, 200, "Content-Type: application/json\r\n",
|
2023-05-17 14:26:45 -03:00
|
|
|
"{%m:%m,%m:%m,%m:%m}\n", MG_ESC("url"),
|
|
|
|
MG_ESC(s_config.url), MG_ESC("pub"), MG_ESC(s_config.pub),
|
|
|
|
MG_ESC("sub"), MG_ESC(s_config.sub));
|
2024-04-17 16:13:10 -03:00
|
|
|
} else if (mg_match(hm->uri, mg_str("/api/config/set"), NULL)) {
|
2022-07-12 17:12:26 -03:00
|
|
|
struct mg_str json = hm->body;
|
|
|
|
update_config(json, "$.url", &s_config.url);
|
|
|
|
update_config(json, "$.pub", &s_config.pub);
|
|
|
|
update_config(json, "$.sub", &s_config.sub);
|
2022-08-13 14:52:10 +01:00
|
|
|
mg_http_reply(c, 200, "", "ok\n");
|
2022-07-12 17:12:26 -03:00
|
|
|
} else {
|
|
|
|
struct mg_http_serve_opts opts = {.root_dir = s_root_dir};
|
|
|
|
mg_http_serve_dir(c, ev_data, &opts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
struct mg_mgr mgr; // Event manager
|
2022-08-01 11:19:32 +01:00
|
|
|
mg_log_set(MG_LL_INFO); // Set to 3 to enable debug
|
2022-07-12 17:12:26 -03:00
|
|
|
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;
|
|
|
|
}
|