From f5b387f8602b30d47841696cb0165aa88218f42a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=EA=A6=AB=EA=A6=B6=EA=A6=8F=EA=A7=80=EA=A6=A6?= =?UTF-8?q?=EA=A6=BF=EA=A6=A7=EA=A6=AE=EA=A6=91=EA=A6=A9=EA=A6=AD=EA=A7=80?= Date: Tue, 20 Sep 2022 09:16:38 +0800 Subject: Rename index to main and move it up to root dir Also log the downloaded data. --- Makefile.am | 8 ++++---- configure.ac | 2 +- index.html | 4 ++-- main.c | 41 +++++++++++++++++++++++++++++++++++++++++ pages/index.c | 41 ----------------------------------------- 5 files changed, 48 insertions(+), 48 deletions(-) create mode 100644 main.c delete mode 100644 pages/index.c diff --git a/Makefile.am b/Makefile.am index 8bfd9ed..8909e90 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,5 +1,5 @@ -bin_PROGRAMS = index.js -index_js_SOURCES = pages/index.c -index_js_LDFLAGS = -s EXPORTED_RUNTIME_METHODS=[cwrap] \ - -s EXPORTED_FUNCTIONS=[_main,_index_getproducts] \ +bin_PROGRAMS = main.js +main_js_SOURCES = main.c +main_js_LDFLAGS = -s EXPORTED_RUNTIME_METHODS=[cwrap] \ + -s EXPORTED_FUNCTIONS=[_main,_getproducts] \ -s FETCH diff --git a/configure.ac b/configure.ac index 8cf2213..ccc0b82 100644 --- a/configure.ac +++ b/configure.ac @@ -1,5 +1,5 @@ AC_INIT([shopify-frontend-template-html], [0.0], [prabowo@darapsa.org]) -AM_INIT_AUTOMAKE([-Wall -Werror foreign subdir-objects]) +AM_INIT_AUTOMAKE([-Wall -Werror foreign]) AC_PROG_CC AC_CONFIG_FILES([Makefile]) AC_OUTPUT diff --git a/index.html b/index.html index 80bd3c6..875451e 100644 --- a/index.html +++ b/index.html @@ -10,7 +10,7 @@ - + diff --git a/main.c b/main.c new file mode 100644 index 0000000..9304259 --- /dev/null +++ b/main.c @@ -0,0 +1,41 @@ +#include +#include +#include + +static void handle_success(emscripten_fetch_t *fetch) +{ + printf("Finished downloading %llu bytes of %s from URL %s.\n", + fetch->numBytes, fetch->data, fetch->url); + emscripten_fetch_close(fetch); +} + +static void handle_error(emscripten_fetch_t *fetch) +{ + printf("Downloading %s failed, HTTP failure status code: %d.\n", + fetch->url, fetch->status); + emscripten_fetch_close(fetch); +} + +void getproducts(const char *token, const char *app_url) +{ + emscripten_fetch_attr_t attr; + emscripten_fetch_attr_init(&attr); + strcpy(attr.requestMethod, "GET"); + attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; + attr.onsuccess = handle_success; + attr.onerror = handle_error; + static const char *schema = "Bearer "; + char auth[strlen(schema) + strlen(token) + 1]; + sprintf(auth, "%s%s", schema, token); + attr.requestHeaders = (const char *[]){ "Authorization", auth, NULL }; + static const char *path = "/products"; + char url[strlen(app_url) + strlen(path) + 1]; + sprintf(url, "%s%s", app_url, path); + emscripten_fetch(&attr, url); +} + +int main(int argc, char *argv[]) +{ + EM_ASM(getToken()); + return 0; +} diff --git a/pages/index.c b/pages/index.c deleted file mode 100644 index fc165b6..0000000 --- a/pages/index.c +++ /dev/null @@ -1,41 +0,0 @@ -#include -#include -#include - -static void handle_success(emscripten_fetch_t *fetch) -{ - printf("Finished downloading %llu bytes from URL %s.\n", - fetch->numBytes, fetch->url); - emscripten_fetch_close(fetch); -} - -static void handle_error(emscripten_fetch_t *fetch) -{ - printf("Downloading %s failed, HTTP failure status code: %d.\n", - fetch->url, fetch->status); - emscripten_fetch_close(fetch); -} - -void index_getproducts(const char *token, const char *app_url) -{ - emscripten_fetch_attr_t attr; - emscripten_fetch_attr_init(&attr); - strcpy(attr.requestMethod, "GET"); - attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; - attr.onsuccess = handle_success; - attr.onerror = handle_error; - static const char *schema = "Bearer "; - char auth[strlen(schema) + strlen(token) + 1]; - sprintf(auth, "%s%s", schema, token); - attr.requestHeaders = (const char *[]){ "Authorization", auth, NULL }; - static const char *path = "/products"; - char url[strlen(app_url) + strlen(path) + 1]; - sprintf(url, "%s%s", app_url, path); - emscripten_fetch(&attr, url); -} - -int main(int argc, char *argv[]) -{ - EM_ASM(getToken()); - return 0; -} -- cgit v1.2.3