Added mighty espify script
This commit is contained in:
parent
0c4f6db4fa
commit
7e4b9af16f
1 changed files with 74 additions and 0 deletions
74
espify.sh
Executable file
74
espify.sh
Executable file
|
@ -0,0 +1,74 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
print_usage()
|
||||||
|
{
|
||||||
|
echo "Usage: ./espify.sh <dir> [<find-filter-args...>]"
|
||||||
|
echo "Example: ./espify.sh /home/user/mywebsite"
|
||||||
|
echo "Example: ./espify.sh dist/ -not -name foo.html"
|
||||||
|
}
|
||||||
|
|
||||||
|
get_var_name()
|
||||||
|
{
|
||||||
|
echo -e "static_$(echo -e $1 | tr -d '_' | tr '/.~-' '_')"
|
||||||
|
}
|
||||||
|
|
||||||
|
if [ "$1" == "" ]; then
|
||||||
|
echo "Error: missing content path." >&2
|
||||||
|
print_usage >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! [ -d "$1" ]; then
|
||||||
|
echo "Error: \`$1' is not a directory." >&2
|
||||||
|
print_usage >&2
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
prefix="$(echo $1 | sed -e 's/\/*$//')"
|
||||||
|
prefix_len=${#prefix}
|
||||||
|
shift 1 # Eat first argument
|
||||||
|
|
||||||
|
flist="$(find -L $prefix -type f \( -name '*.js' -or -name '*.html' -or -name '*.css' -or -name 'favicon.ico' \) $@ | cut -c $[prefix_len+2]-)"
|
||||||
|
|
||||||
|
cat <<EOF
|
||||||
|
#include "gadgety.h"
|
||||||
|
#include <ESP8266WebServer.h>
|
||||||
|
|
||||||
|
EOF
|
||||||
|
|
||||||
|
for i in $flist; do
|
||||||
|
echo -e "unsigned char PROGMEM $(get_var_name $i)[] = {"
|
||||||
|
gzip -c "$prefix/$i" | xxd -i -c 16
|
||||||
|
echo -e "};\n"
|
||||||
|
done
|
||||||
|
|
||||||
|
cat <<EOF
|
||||||
|
static ESP8266WebServer *_server = 0;
|
||||||
|
|
||||||
|
#define SERVE_STATIC(p) static void serve_ ## p (void) { \\
|
||||||
|
_server->sendHeader("Content-Encoding", "gzip"); \\
|
||||||
|
_server->send_P(200, "text/html", (const char*)p, sizeof(p)); \\
|
||||||
|
}
|
||||||
|
|
||||||
|
static void redirect_index(void)
|
||||||
|
{
|
||||||
|
_server->sendHeader("Location", "/");
|
||||||
|
_server->send(301);
|
||||||
|
}
|
||||||
|
|
||||||
|
EOF
|
||||||
|
|
||||||
|
for i in $flist; do
|
||||||
|
echo -e "SERVE_STATIC($(get_var_name $i))"
|
||||||
|
done
|
||||||
|
|
||||||
|
echo -e "\nvoid gadgety_static_init_server(ESP8266WebServer* server)\n{"
|
||||||
|
echo -e " _server = server;"
|
||||||
|
echo -e " server->onNotFound(redirect_index);"
|
||||||
|
echo -e " server->on(\"/\", serve_static_index_html);"
|
||||||
|
|
||||||
|
for i in $flist; do
|
||||||
|
echo -e " server->on(\"/$i\", serve_$(get_var_name $i));"
|
||||||
|
done
|
||||||
|
|
||||||
|
echo -e "}"
|
Loading…
Reference in a new issue