Mongoose / CivetWeb¶
Giới thiệu và license¶
Mongoose và CivetWeb là thư viện web server viết bằng C, được link thẳng vào ứng dụng. Handler HTTP là hàm C nên truy cập trực tiếp dữ liệu của ứng dụng, không cần CGI hay IPC.
| Mongoose | CivetWeb | |
|---|---|---|
| License | GPL-2.0 / thương mại | MIT |
| Nguồn gốc | Cesanta | Fork từ Mongoose trước khi Mongoose đổi sang GPL |
| Tính năng | HTTP, WebSocket, MQTT, built-in TCP/IP stack cho MCU | HTTP, WebSocket, CGI, Lua, Duktape |
- Mongoose: https://github.com/cesanta/mongoose
- CivetWeb: https://github.com/civetweb/civetweb
Warning
Dùng Mongoose trong sản phẩm không công bố mã nguồn thì cần license thương mại. CivetWeb (MIT) không có ràng buộc này.
Kích thước và tài nguyên sử dụng¶
Build / cross-compile¶
Mongoose chỉ gồm mongoose.c và mongoose.h, thêm thẳng vào project của ứng dụng (Makefile/CMake). CivetWeb có thể build thành thư viện bằng CMake hoặc thêm trực tiếp civetweb.c.
Ví dụ tối thiểu (CivetWeb)¶
#include "civetweb.h"
static int hello_handler(struct mg_connection *conn, void *cbdata)
{
mg_printf(conn,
"HTTP/1.1 200 OK\r\n"
"Content-Type: application/json\r\n"
"Connection: close\r\n\r\n"
"{\"status\":\"ok\"}");
return 200;
}
int main(void)
{
const char *options[] = { "listening_ports", "8080", NULL };
struct mg_context *ctx;
mg_init_library(0);
ctx = mg_start(NULL, NULL, options);
mg_set_request_handler(ctx, "/api/status", hello_handler, NULL);
getchar(); /* chạy tới khi nhấn Enter */
mg_stop(ctx);
mg_exit_library();
return 0;
}
Tích hợp¶
REST API / WebSocket¶
HTTPS¶
Debug và tuning¶
Tài liệu tham khảo¶
- Mongoose documentation: https://mongoose.ws/documentation/
- CivetWeb user manual: https://github.com/civetweb/civetweb/blob/master/docs/UserManual.md