summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2022-09-18 21:18:44 +0800
committerꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2022-09-18 21:18:44 +0800
commit603d4f0f85cac6942996c2c1f74672bd2c562e27 (patch)
tree2ecfa2afadc4f9ae7c3480c4332f3d332b5b1d13
parentc45dc4381f4b3d9dcee3976a7a3a4fb3119be1a7 (diff)
MHD access handler is moved to the lib
-rw-r--r--web/main.c68
1 files changed, 25 insertions, 43 deletions
diff --git a/web/main.c b/web/main.c
index 9ec7463..e14ebe6 100644
--- a/web/main.c
+++ b/web/main.c
@@ -1,50 +1,32 @@
-#include <stdio.h>
-#include <stdlib.h>
#include <string.h>
+#include <stdio.h>
#include <shopify.h>
-static enum MHD_Result handle_request(void *cls, struct MHD_Connection *conn,
- const char *url, const char *method, const char *version,
- const char *upload_data, size_t *upload_data_size,
- void **con_cls)
-{
- if (strcmp(method, "GET"))
- return MHD_NO;
- struct shopify_param *params = *con_cls;
- if (!params) {
- params = malloc(sizeof(struct shopify_param));
- *con_cls = params;
- return MHD_YES;
- }
- static const char *redir_url = "/auth";
- if (!shopify_valid(conn, url, redir_url, API_SECRET_KEY, &params)) {
- free(params);
- return MHD_NO;
- }
- const size_t dir_len = strlen(APP_DIR);
- static const char *toml_rel_path = "/shopify.app.toml";
- char toml_abs_path[dir_len + strlen(toml_rel_path) + 1];
- sprintf(toml_abs_path, "%s%s", APP_DIR, toml_rel_path);
- static const char *html_rel_path = "/web/frontend/index.html";
- char html_abs_path[dir_len + strlen(html_rel_path) + 1];
- sprintf(html_abs_path, "%s%s", APP_DIR, html_rel_path);
- struct MHD_Response *resp;
- enum MHD_Result ret = shopify_respond(params, url, redir_url, APP_URL,
- APP_ID, API_KEY, API_SECRET_KEY, toml_abs_path,
- html_abs_path, conn, &resp);
- MHD_destroy_response(resp);
- free(params);
- return ret;
-}
-
int main(int argc, char *argv[])
{
- shopify_init();
- struct MHD_Daemon *daemon
- = MHD_start_daemon(MHD_USE_INTERNAL_POLLING_THREAD, 3000, NULL,
- NULL, &handle_request, NULL, MHD_OPTION_END);
- getchar();
- MHD_stop_daemon(daemon);
- shopify_cleanup();
+ const size_t app_dir_len = strlen(APP_DIR);
+ static const char *scope_rel = "/shopify.app.toml";
+ char scope[app_dir_len + strlen(scope_rel) + 1];
+ sprintf(scope, "%s%s", APP_DIR, scope_rel);
+ static const char *index_rel = "/web/frontend/index.html";
+ char index[app_dir_len + strlen(index_rel) + 1];
+ sprintf(index, "%s%s", APP_DIR, index_rel);
+ shopify_app(API_KEY, API_SECRET_KEY, APP_URL, "/auth", APP_ID, scope,
+ index, (struct shopify_api[]){
+ {
+ "/products",
+ "GET",
+ shopify_graphql,
+ "{"\
+ " productCreate() {"\
+ " product {"\
+ " id"\
+ " }"\
+ " }"\
+ "}"
+ },
+ {}
+ }
+ );
return 0;
}