SpriteHTTPD - embedded HTTP server with read-only filesystem and templating, originally developed for ESP8266, now stand-alone and POSIX compatible.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
spritehttpd/demo/server_demo.c

84 lines
1.8 KiB

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include "httpd.h"
#include "httpd-utils.h"
#include "httpd-espfs.h"
extern unsigned char espfs_image[];
extern unsigned int espfs_image_len;
httpd_thread_handle_t *s_serverHandle = NULL;
/** "About" page */
httpd_cgi_state tplIndex(HttpdConnData *connData, char *token, void **arg)
{
if (token == NULL) { return HTTPD_CGI_DONE; }
else if (streq(token, "date")) {
tplSend(connData, __DATE__, -1);
} else if (streq(token, "time")) {
tplSend(connData, __TIME__, -1);
} else if (streq(token, "vers_httpd")) {
tplSend(connData, httpdGetVersion(), -1);
}
return HTTPD_CGI_DONE;
}
/**
* Application routes
*/
const HttpdBuiltInUrl routes[] = {
// TODO password lock ...
// --- Web pages ---
// ROUTE_TPL_FILE("/", tplIndex, "/index.tpl"),
ROUTE_FILE("/", "index.html"),
ROUTE_FILESYSTEM(),
ROUTE_END(),
};
void sigpipe_handler(int unused)
{
}
void handle_sigint(int signum)
{
fprintf(stderr, " SIGINT detected, shutting down HTTPD\n");
httpdShutdown(s_serverHandle);
}
int main()
{
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_handler = handle_sigint;
sigaction(SIGINT, &action, NULL);
// prevent abort on sigpipe
sigaction(SIGPIPE, &(struct sigaction) {{sigpipe_handler}}, NULL);
struct httpd_options opts = {
.port = 8080,
};
s_serverHandle = httpdStart(routes, &opts);
httpdSetName("SpriteHTTPD Server Demo");
httpdJoin(s_serverHandle);
return 0;
}
int httpdPlatEspfsRead(void *dest, uint32_t offset, size_t len)
{
if (offset + len > espfs_image_len) {
return -1;
}
memcpy(dest, &espfs_image[offset], len);
return 0;
}