upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/main/tollgate_api.c
diff options
context:
space:
mode:
Diffstat (limited to 'main/tollgate_api.c')
-rw-r--r--main/tollgate_api.c36
1 files changed, 27 insertions, 9 deletions
diff --git a/main/tollgate_api.c b/main/tollgate_api.c
index 72ed726..25e7dd2 100644
--- a/main/tollgate_api.c
+++ b/main/tollgate_api.c
@@ -78,7 +78,8 @@ static cJSON *create_session_event(uint32_t client_ip, uint64_t allotment_ms)
78 78
79 cJSON *metric_tag = cJSON_CreateArray(); 79 cJSON *metric_tag = cJSON_CreateArray();
80 cJSON_AddItemToArray(metric_tag, cJSON_CreateString("metric")); 80 cJSON_AddItemToArray(metric_tag, cJSON_CreateString("metric"));
81 cJSON_AddItemToArray(metric_tag, cJSON_CreateString("milliseconds")); 81 const tollgate_config_t *mcfg = tollgate_config_get();
82 cJSON_AddItemToArray(metric_tag, cJSON_CreateString(mcfg->metric[0] ? mcfg->metric : "milliseconds"));
82 cJSON_AddItemToArray(tags, metric_tag); 83 cJSON_AddItemToArray(tags, metric_tag);
83 84
84 cJSON_AddItemToObject(root, "tags", tags); 85 cJSON_AddItemToObject(root, "tags", tags);
@@ -98,13 +99,14 @@ static esp_err_t api_get_discovery(httpd_req_t *req)
98 99
99 cJSON *metric_tag = cJSON_CreateArray(); 100 cJSON *metric_tag = cJSON_CreateArray();
100 cJSON_AddItemToArray(metric_tag, cJSON_CreateString("metric")); 101 cJSON_AddItemToArray(metric_tag, cJSON_CreateString("metric"));
101 cJSON_AddItemToArray(metric_tag, cJSON_CreateString("milliseconds")); 102 cJSON_AddItemToArray(metric_tag, cJSON_CreateString(cfg->metric[0] ? cfg->metric : "milliseconds"));
102 cJSON_AddItemToArray(tags, metric_tag); 103 cJSON_AddItemToArray(tags, metric_tag);
103 104
104 cJSON *step_tag = cJSON_CreateArray(); 105 cJSON *step_tag = cJSON_CreateArray();
105 cJSON_AddItemToArray(step_tag, cJSON_CreateString("step_size")); 106 cJSON_AddItemToArray(step_tag, cJSON_CreateString("step_size"));
106 char step_str[32]; 107 char step_str[32];
107 snprintf(step_str, sizeof(step_str), "%d", cfg->step_size_ms); 108 bool is_bytes = (strcmp(cfg->metric, "bytes") == 0);
109 snprintf(step_str, sizeof(step_str), "%d", is_bytes ? cfg->step_size_bytes : cfg->step_size_ms);
108 cJSON_AddItemToArray(step_tag, cJSON_CreateString(step_str)); 110 cJSON_AddItemToArray(step_tag, cJSON_CreateString(step_str));
109 cJSON_AddItemToArray(tags, step_tag); 111 cJSON_AddItemToArray(tags, step_tag);
110 112
@@ -280,7 +282,10 @@ static esp_err_t api_post_payment(httpd_req_t *req)
280 } 282 }
281 283
282 const tollgate_config_t *cfg = tollgate_config_get(); 284 const tollgate_config_t *cfg = tollgate_config_get();
283 uint64_t allotment = cashu_calculate_allotment_ms(token->total_amount, cfg->price_per_step, cfg->step_size_ms); 285 bool is_bytes = (strcmp(cfg->metric, "bytes") == 0);
286 uint64_t step_size = is_bytes ? (uint64_t)cfg->step_size_bytes : (uint64_t)cfg->step_size_ms;
287 uint64_t allotment = cashu_calculate_allotment(token->total_amount, cfg->price_per_step,
288 cfg->metric, step_size);
284 if (allotment == 0) { 289 if (allotment == 0) {
285 free(states); 290 free(states);
286 free(token); 291 free(token);
@@ -299,7 +304,12 @@ static esp_err_t api_post_payment(httpd_req_t *req)
299 for (int i = 0; i < secret_count; i++) { 304 for (int i = 0; i < secret_count; i++) {
300 secrets[i] = token->proofs[i].secret; 305 secrets[i] = token->proofs[i].secret;
301 } 306 }
302 session_t *session = session_create(client_ip, allotment, secrets, secret_count); 307 session_t *session;
308 if (is_bytes) {
309 session = session_create_bytes(client_ip, allotment, secrets, secret_count);
310 } else {
311 session = session_create(client_ip, allotment, secrets, secret_count);
312 }
303 if (!session) { 313 if (!session) {
304 free(states); 314 free(states);
305 free(token); 315 free(token);
@@ -339,12 +349,20 @@ static esp_err_t api_get_usage(httpd_req_t *req)
339 return ESP_OK; 349 return ESP_OK;
340 } 350 }
341 351
342 int64_t elapsed = (int64_t)xTaskGetTickCount() * portTICK_PERIOD_MS - session->start_time_ms; 352 const tollgate_config_t *cfg = tollgate_config_get();
343 int64_t remaining = session->allotment_ms - elapsed; 353 bool is_bytes = (strcmp(cfg->metric, "bytes") == 0);
344 if (remaining < 0) remaining = 0;
345 354
346 char resp[64]; 355 char resp[64];
347 snprintf(resp, sizeof(resp), "%lld/%llu", (long long)remaining, (unsigned long long)session->allotment_ms); 356 if (is_bytes) {
357 int64_t remaining = (int64_t)session->allotment_bytes - (int64_t)session->bytes_consumed;
358 if (remaining < 0) remaining = 0;
359 snprintf(resp, sizeof(resp), "%lld/%llu", (long long)remaining, (unsigned long long)session->allotment_bytes);
360 } else {
361 int64_t elapsed = (int64_t)xTaskGetTickCount() * portTICK_PERIOD_MS - session->start_time_ms;
362 int64_t remaining = session->allotment_ms - elapsed;
363 if (remaining < 0) remaining = 0;
364 snprintf(resp, sizeof(resp), "%lld/%llu", (long long)remaining, (unsigned long long)session->allotment_ms);
365 }
348 httpd_resp_set_type(req, "text/plain"); 366 httpd_resp_set_type(req, "text/plain");
349 httpd_resp_send(req, resp, strlen(resp)); 367 httpd_resp_send(req, resp, strlen(resp));
350 return ESP_OK; 368 return ESP_OK;