몽구스 서버 이벤트 처리기가 NULL이면 HTML 파일 (예 : localhost : 8080/index.html)이 번거롭지 않게 렌더링됩니다.몽구스가 html 파일을 렌더링해야합니다
다음은 몽구스 Github 레포의 코드 (https://github.com/cesanta/mongoose)입니다.
int main(void) {
struct mg_server *server = mg_create_server(NULL, NULL);
mg_set_option(server, "listening_port", "8080");
printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
for (;;) {
mg_poll_server(server, 1000);
}
mg_destroy_server(&server);
return 0;
}
요청 처리를 위해 몽구스의 이벤트 처리기를 사용하고 싶습니다. 여기에서 자습서를 보았습니다 : https://github.com/cesanta/mongoose/blob/master/examples/post.c. 유일한 문제는 아래 코드와 같이 포함 된 파일로 초기화되지 않으면 내 index.html 파일에 액세스 할 수 없다는 것입니다. 임베디드 파일 버전을 제거하고 실제 html 파일을 렌더링하고 싶습니다.
#include <stdio.h>
#include <string.h>
#include "mongoose.h"
static const char *html_form =
"<html><body>"
"<form action=\"/handle_request\">"
"<input type=\"text\" name=\"request_value\" /> <br/>"
"<input type=\"submit\" />"
"</form></body></html>";
static void send_reply(struct mg_connection *conn) {
char value[500];
if(strcmp(conn->uri, "/handle_request") == 0) {
mg_get_var(conn, "request_value", value, sizeof(value));
mg_send_header(conn, "Content-Type", "text/plain");
mg_printf_data(conn, value);
} if(strcmp(conn->uri, "/index.html") == 0) {
// #######################
// HELP ME HERE
// #######################
// Render the html file.
// #######################
} else {
mg_send_data(conn, html_form, strlen(html_form));
}
}
static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
if (ev == MG_REQUEST) {
send_reply(conn);
return MG_TRUE;
} else if (ev == MG_AUTH) {
return MG_TRUE;
} else {
return MG_FALSE;
}
}
int main(void) {
struct mg_server *server = mg_create_server(NULL, ev_handler);
mg_set_option(server, "listening_port", "8080");
printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
for (;;) {
mg_poll_server(server, 1000);
}
mg_destroy_server(&server);
return 0;
}
의견이 있으십니까? 미리 감사드립니다.
오프 주제 : mongoose.h/mongoose.c는 어디에서 받았습니까? 내 버전은 [mg_create_server가 없습니다] (http://stackoverflow.com/questions/32956469/mongoose-has-no-mg-create-server-function). – Claudiu