http server

This commit is contained in:
leo 2022-03-22 18:18:39 +01:00
parent 49ea8aec16
commit b9f879a45c
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
10 changed files with 119 additions and 4 deletions

View File

@ -0,0 +1,3 @@
idf_component_register(SRCS "HTTPServe.c"
INCLUDE_DIRS "include"
REQUIRES esp_http_server)

View File

@ -0,0 +1,73 @@
#include <stdio.h>
#include "esp_err.h"
#include "esp_log.h"
#include "esp_http_server.h"
#include "HTTPServe.h"
esp_err_t start_server(HTTP_serve_config_t config){
httpd_config_t serv_config = HTTPD_DEFAULT_CONFIG();
serv_config.uri_match_fn = httpd_uri_match_wildcard;
httpd_handle_t server = NULL;
ESP_LOGI("SERV", "addr %i", (&config)->mountpoint);
httpd_uri_t uri_get = {
.uri = config.getUri,
.method = HTTP_GET,
.handler = get_handler,
.user_ctx = &config,
};
httpd_uri_t uri_post = {
.uri = config.postUri,
.method = HTTP_POST,
.handler = post_handler,
.user_ctx = NULL,
};
esp_err_t status = httpd_start(&server, &serv_config);
if(status == ESP_OK) {
httpd_register_uri_handler(server, &uri_get);
httpd_register_uri_handler(server, &uri_post);
}
return status;
}
esp_err_t get_handler(httpd_req_t *req){
char* prefix = ((HTTP_serve_config_t*)req->user_ctx)->mountpoint;
const char* uri = req->uri;
bool inRoot = !strcmp(uri,"/");
char filename[strlen(uri) + strlen(prefix) + 1 + (inRoot?10:0)];
strcpy(filename, prefix);
strcat(filename, uri);
if(inRoot)
strcat(filename, "index.html");
const char err404[] = "404 page not found";
ESP_LOGI("HTTP","req : %s %s", prefix, filename);
FILE* f;
if(!(f = fopen(filename,"r"))){
httpd_resp_send(req, err404, HTTPD_RESP_USE_STRLEN);
return ESP_FAIL;
}
if(strstr(uri, ".js") != NULL)
httpd_resp_set_type(req, "application/javascript");
char chunk[1024];
size_t chunksize;
do {
chunksize = fread(chunk, 1, sizeof(chunk), f);
if (httpd_resp_send_chunk(req, chunk, chunksize) != ESP_OK) {
fclose(f);
return ESP_FAIL;
}
} while (chunksize != 0);
httpd_resp_send_chunk(req, NULL, 0);
fclose(f);
return ESP_OK;
}
esp_err_t post_handler(httpd_req_t *req){
return ESP_OK;
}

View File

@ -0,0 +1,13 @@
#include "esp_http_server.h"
struct HTTP_serve_config {
char mountpoint[32];
char getUri[32];
char postUri[32];
};
typedef struct HTTP_serve_config HTTP_serve_config_t;
esp_err_t get_handler(httpd_req_t *req);
esp_err_t post_handler(httpd_req_t *req);
esp_err_t start_server(HTTP_serve_config_t config);

1
http/assets/index.css Normal file
View File

@ -0,0 +1 @@
:root{font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Open Sans,Helvetica Neue,sans-serif}main.svelte-1fm71xa{text-align:center;padding:1em;margin:0 auto}h1.svelte-1fm71xa{color:#ff3e00;text-transform:uppercase;font-size:4rem;font-weight:100;line-height:1.1;margin:2rem auto;max-width:14rem}@media (min-width: 480px){h1.svelte-1fm71xa{max-width:none}}

1
http/assets/index.js Normal file
View File

@ -0,0 +1 @@
import{S as _,i as y,s as x,e as d,a as v,b as u,n as f,d as m,o as S,c as A,f as L,g,h,m as I,t as M,j as O,k as P}from"./vendor.js";const k=function(){const n=document.createElement("link").relList;if(n&&n.supports&&n.supports("modulepreload"))return;for(const e of document.querySelectorAll('link[rel="modulepreload"]'))s(e);new MutationObserver(e=>{for(const t of e)if(t.type==="childList")for(const a of t.addedNodes)a.tagName==="LINK"&&a.rel==="modulepreload"&&s(a)}).observe(document,{childList:!0,subtree:!0});function o(e){const t={};return e.integrity&&(t.integrity=e.integrity),e.referrerpolicy&&(t.referrerPolicy=e.referrerpolicy),e.crossorigin==="use-credentials"?t.credentials="include":e.crossorigin==="anonymous"?t.credentials="omit":t.credentials="same-origin",t}function s(e){if(e.ep)return;e.ep=!0;const t=o(e);fetch(e.href,t)}};k();function N(i){let n,o,s;return{c(){n=d("canvas"),o=v(),s=d("p"),s.textContent=`${C} ppm`},m(e,t){u(e,n,t),i[3](n),u(e,o,t),u(e,s,t)},p:f,i:f,o:f,d(e){e&&m(n),i[3](null),e&&m(o),e&&m(s)}}}let C=7500;function q(i,n,o){let{min:s=0}=n,{max:e=15e3}=n,t;S(()=>{const r=t.getContext("2d");let l=t.width,b=t.height,p=l/5,w=(C-s)/e*Math.PI;r.arc(l/2,b,l/2-p,-Math.PI,-Math.PI+w,!1),r.lineWidth=p;let c=r.createLinearGradient(0,0,l,0);c.addColorStop("0","green"),c.addColorStop("0.5","yellow"),c.addColorStop("1.0","red"),r.strokeStyle=c,r.stroke()});function a(r){A[r?"unshift":"push"](()=>{t=r,o(0,t)})}return i.$$set=r=>{"min"in r&&o(1,s=r.min),"max"in r&&o(2,e=r.max)},[t,s,e,a]}class E extends _{constructor(n){super();y(this,n,q,N,x,{min:1,max:2})}}function j(i){let n,o,s,e,t;return e=new E({}),{c(){n=d("main"),o=d("h1"),o.textContent="AAA",s=v(),L(e.$$.fragment),g(o,"class","svelte-1fm71xa"),g(n,"class","svelte-1fm71xa")},m(a,r){u(a,n,r),h(n,o),h(n,s),I(e,n,null),t=!0},p:f,i(a){t||(M(e.$$.fragment,a),t=!0)},o(a){O(e.$$.fragment,a),t=!1},d(a){a&&m(n),P(e)}}}class B extends _{constructor(n){super();y(this,n,null,j,x,{})}}new B({target:document.getElementById("app")});

1
http/assets/vendor.js Normal file
View File

@ -0,0 +1 @@
function S(){}function C(t){return t()}function j(){return Object.create(null)}function m(t){t.forEach(C)}function O(t){return typeof t=="function"}function L(t,n){return t!=t?n==n:t!==n||t&&typeof t=="object"||typeof t=="function"}function M(t){return Object.keys(t).length===0}function Q(t,n){t.appendChild(n)}function R(t,n,e){t.insertBefore(n,e||null)}function z(t){t.parentNode.removeChild(t)}function U(t){return document.createElement(t)}function B(t){return document.createTextNode(t)}function V(){return B(" ")}function W(t,n,e){e==null?t.removeAttribute(n):t.getAttribute(n)!==e&&t.setAttribute(n,e)}function F(t){return Array.from(t.childNodes)}let l;function f(t){l=t}function P(){if(!l)throw new Error("Function called outside component initialization");return l}function X(t){P().$$.on_mount.push(t)}const a=[],v=[],h=[],A=[],T=Promise.resolve();let y=!1;function q(){y||(y=!0,T.then(N))}function x(t){h.push(t)}const $=new Set;let d=0;function N(){const t=l;do{for(;d<a.length;){const n=a[d];d++,f(n),D(n.$$)}for(f(null),a.length=0,d=0;v.length;)v.pop()();for(let n=0;n<h.length;n+=1){const e=h[n];$.has(e)||($.add(e),e())}h.length=0}while(a.length);for(;A.length;)A.pop()();y=!1,$.clear(),f(t)}function D(t){if(t.fragment!==null){t.update(),m(t.before_update);const n=t.dirty;t.dirty=[-1],t.fragment&&t.fragment.p(t.ctx,n),t.after_update.forEach(x)}}const _=new Set;let G;function H(t,n){t&&t.i&&(_.delete(t),t.i(n))}function Y(t,n,e,o){if(t&&t.o){if(_.has(t))return;_.add(t),G.c.push(()=>{_.delete(t),o&&(e&&t.d(1),o())}),t.o(n)}}function Z(t){t&&t.c()}function I(t,n,e,o){const{fragment:u,on_mount:p,on_destroy:i,after_update:g}=t.$$;u&&u.m(n,e),o||x(()=>{const s=p.map(C).filter(O);i?i.push(...s):m(s),t.$$.on_mount=[]}),g.forEach(x)}function J(t,n){const e=t.$$;e.fragment!==null&&(m(e.on_destroy),e.fragment&&e.fragment.d(n),e.on_destroy=e.fragment=null,e.ctx=[])}function K(t,n){t.$$.dirty[0]===-1&&(a.push(t),q(),t.$$.dirty.fill(0)),t.$$.dirty[n/31|0]|=1<<n%31}function tt(t,n,e,o,u,p,i,g=[-1]){const s=l;f(t);const r=t.$$={fragment:null,ctx:null,props:p,update:S,not_equal:u,bound:j(),on_mount:[],on_destroy:[],on_disconnect:[],before_update:[],after_update:[],context:new Map(n.context||(s?s.$$.context:[])),callbacks:j(),dirty:g,skip_bound:!1,root:n.target||s.$$.root};i&&i(r.root);let b=!1;if(r.ctx=e?e(t,n.props||{},(c,w,...E)=>{const k=E.length?E[0]:w;return r.ctx&&u(r.ctx[c],r.ctx[c]=k)&&(!r.skip_bound&&r.bound[c]&&r.bound[c](k),b&&K(t,c)),w}):[],r.update(),b=!0,m(r.before_update),r.fragment=o?o(r.ctx):!1,n.target){if(n.hydrate){const c=F(n.target);r.fragment&&r.fragment.l(c),c.forEach(z)}else r.fragment&&r.fragment.c();n.intro&&H(t.$$.fragment),I(t,n.target,n.anchor,n.customElement),N()}f(s)}class nt{$destroy(){J(this,1),this.$destroy=S}$on(n,e){const o=this.$$.callbacks[n]||(this.$$.callbacks[n]=[]);return o.push(e),()=>{const u=o.indexOf(e);u!==-1&&o.splice(u,1)}}$set(n){this.$$set&&!M(n)&&(this.$$.skip_bound=!0,this.$$set(n),this.$$.skip_bound=!1)}}export{nt as S,V as a,R as b,v as c,z as d,U as e,Z as f,W as g,Q as h,tt as i,Y as j,J as k,I as m,S as n,X as o,L as s,H as t};

BIN
http/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

16
http/index.html Normal file
View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>CO2 Sense</title>
<script type="module" crossorigin src="/assets/index.js"></script>
<link rel="modulepreload" href="/assets/vendor.js">
<link rel="stylesheet" href="/assets/index.css">
</head>
<body>
<div id="app"></div>
</body>
</html>

View File

@ -14,6 +14,8 @@
#include "APlib.h"
#include "HTTPServe.h"
#include <dirent.h>
void app_main(void)
{
esp_err_t ret = nvs_flash_init();
@ -46,4 +48,8 @@ void app_main(void)
};
ESP_ERROR_CHECK(start_server(serv_config));
while(1){
vTaskDelay(100 / portTICK_PERIOD_MS);
}
}

View File

@ -859,13 +859,14 @@ CONFIG_HEAP_TRACING_OFF=y
# CONFIG_LOG_DEFAULT_LEVEL_NONE is not set
# CONFIG_LOG_DEFAULT_LEVEL_ERROR is not set
# CONFIG_LOG_DEFAULT_LEVEL_WARN is not set
# CONFIG_LOG_DEFAULT_LEVEL_INFO is not set
CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y
CONFIG_LOG_DEFAULT_LEVEL_INFO=y
# CONFIG_LOG_DEFAULT_LEVEL_DEBUG is not set
# CONFIG_LOG_DEFAULT_LEVEL_VERBOSE is not set
CONFIG_LOG_DEFAULT_LEVEL=4
CONFIG_LOG_DEFAULT_LEVEL=3
CONFIG_LOG_MAXIMUM_EQUALS_DEFAULT=y
# CONFIG_LOG_MAXIMUM_LEVEL_DEBUG is not set
# CONFIG_LOG_MAXIMUM_LEVEL_VERBOSE is not set
CONFIG_LOG_MAXIMUM_LEVEL=4
CONFIG_LOG_MAXIMUM_LEVEL=3
CONFIG_LOG_COLORS=y
CONFIG_LOG_TIMESTAMP_SOURCE_RTOS=y
# CONFIG_LOG_TIMESTAMP_SOURCE_SYSTEM is not set