mirror of
https://github.com/fluencelabs/fluid
synced 2025-04-24 14:22:18 +00:00
some improvements
This commit is contained in:
parent
39ebf92cc0
commit
732ae5a4ed
@ -1,4 +1,4 @@
|
||||
TARGET = hello_world
|
||||
TARGET = fluid
|
||||
CC = /opt/wasi-sdk/bin/clang
|
||||
SYSROOT = /opt/wasi-sdk/share/wasi-sysroot
|
||||
TARGET_TRIPLE = wasm32-unknown-wasi
|
||||
|
73
backend-c/run.sh
Executable file
73
backend-c/run.sh
Executable file
@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -e
|
||||
set -o pipefail
|
||||
|
||||
mkdir -p wasm
|
||||
|
||||
# Download SQLite
|
||||
SQLITE="sqlite3_0.2.0.wasm"
|
||||
if [ ! -f "wasm/$SQLITE" ]; then
|
||||
echo "Downloading $SQLITE..."
|
||||
wget -q https://github.com/fluencelabs/sqlite/releases/download/v0.2.0_w/$SQLITE -O ./wasm/$SQLITE
|
||||
fi
|
||||
|
||||
# Build fluid WASM module
|
||||
echo "Building to WASM..."
|
||||
docker-compose up --build
|
||||
cp fluid.wasm wasm/
|
||||
echo
|
||||
|
||||
# Run it all on 30000 port with default Fluence API
|
||||
echo "Running..."
|
||||
docker rm -f frun &>/dev/null || true
|
||||
echo 'docker run -d --name frun --rm -v "$(pwd)/wasm:/code" -p 30000:30000 fluencelabs/frun:latest'
|
||||
docker run -d --name frun --rm -v "$(pwd)/wasm:/code" -p 30000:30000 fluencelabs/frun:latest >/dev/null
|
||||
echo
|
||||
|
||||
# Wait for app to be initialized
|
||||
sleep 1 && (docker logs -f frun 2>&1 &) | grep -q initialized && sleep 1
|
||||
|
||||
|
||||
|
||||
############################################
|
||||
### *** --- === Sending post === --- *** ###
|
||||
############################################
|
||||
|
||||
# Assign json to a variable using heredoc technique
|
||||
JSON=$(cat <<JSON
|
||||
{"action":"Post","message":"I'm nice, you're nice, it's nice!","username":"random_joe"}
|
||||
JSON
|
||||
)
|
||||
|
||||
echo -e "Sending post: $JSON"
|
||||
# Send json as a request, and receive result
|
||||
RESPONSE=$(curl -s 'http://localhost:30000/apps/0/tx' --data $'sessionId/0\n'"$JSON" --compressed)
|
||||
RESPONSE=$(echo "$RESPONSE" | jq -r .result.data | base64 --decode 2>/dev/null || echo "$RESPONSE")
|
||||
# Parse json or print response as is
|
||||
echo "$RESPONSE" | jq . 2>/dev/null || echo "$RESPONSE"
|
||||
|
||||
|
||||
##############################################
|
||||
### *** --- === Fetching posts === --- *** ###
|
||||
##############################################
|
||||
|
||||
# Assign json to a variable using heredoc technique
|
||||
JSON=$(cat <<JSON
|
||||
{"action":"Fetch", "handle": "random_joe", "offset": 0, "count": 10}
|
||||
JSON
|
||||
)
|
||||
|
||||
echo -e "Fetching posts: $JSON"
|
||||
|
||||
# Send json as a request, and receive result
|
||||
RESPONSE=$(curl -s 'http://localhost:30000/apps/0/tx' --data $'sessionId/1\n'"$JSON" --compressed)
|
||||
RESPONSE=$(echo "$RESPONSE" | jq -r .result.data | base64 --decode 2>/dev/null || echo "$RESPONSE")
|
||||
|
||||
# Parse json or print response as is
|
||||
echo "$RESPONSE" | jq . 2>/dev/null || echo "$RESPONSE"
|
||||
|
||||
|
||||
# Remove frun container
|
||||
echo -e "Stopping..."
|
||||
docker rm -f frun >/dev/null
|
14
backend-c/sdk/syscalls_stubs.c
Normal file
14
backend-c/sdk/syscalls_stubs.c
Normal file
@ -0,0 +1,14 @@
|
||||
#include <stdio.h>
|
||||
|
||||
size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int __stdio_close(FILE *f) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
off_t __stdio_seek(FILE *_f, off_t _offset, int _value) {
|
||||
return 1;
|
||||
}
|
||||
|
@ -1,23 +1,11 @@
|
||||
#include "model.h"
|
||||
#include "../sdk/allocator.h"
|
||||
#include "../sdk/logger.h"
|
||||
#include "model.h"
|
||||
#include "../sdk/syscalls_stubs.c"
|
||||
#include "../libs/tiny-json/tiny-json.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int __stdio_close(FILE *f) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
off_t __stdio_seek(FILE *_f, off_t _offset, int _value) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *prepare_response(const char *response, int response_length) {
|
||||
const int RESPONSE_SIZE_BYTES = 4;
|
||||
@ -38,17 +26,16 @@ const char *fetch_posts_request(const json_t *json);
|
||||
bool isInited = 0;
|
||||
|
||||
const char *invoke(char *str, int length) {
|
||||
// initialize SQLite by creating schema
|
||||
if(0 == isInited) {
|
||||
create_scheme();
|
||||
isInited = 1;
|
||||
}
|
||||
|
||||
wasm_log(str, length);
|
||||
wasm_log("\n", 1);
|
||||
|
||||
json_t pool[10];
|
||||
const unsigned int pool_size = sizeof pool / sizeof *pool;
|
||||
|
||||
// try to parse json and extract action field
|
||||
const json_t *json = json_create(str, pool, pool_size);
|
||||
if(!json) {
|
||||
const char error[] = "Mailformed json given";
|
||||
@ -68,12 +55,14 @@ const char *invoke(char *str, int length) {
|
||||
|
||||
const char *action = json_getValue(action_json);
|
||||
|
||||
// use action to determine the desired activity
|
||||
const char *result = "";
|
||||
if(0 == strcmp(action, "Post")) {
|
||||
result = add_post_request(json);
|
||||
} else if(0 == strcmp(action, "Fetch")) {
|
||||
result = fetch_posts_request(json);
|
||||
} else {
|
||||
// no suitable action given
|
||||
char *error = (char *)malloc(1024);
|
||||
const int error_size = snprintf(error, 1024, "%s given as the action field, but only `Post` and `Fetch` are supported", action);
|
||||
result = prepare_response(error, error_size);
|
||||
@ -83,6 +72,7 @@ const char *invoke(char *str, int length) {
|
||||
}
|
||||
|
||||
const char *add_post_request(const json_t *json) {
|
||||
// try to extract username and message properties
|
||||
const json_t *username_json = json_getProperty(json, "username");
|
||||
const json_t *message_json = json_getProperty(json, "message");
|
||||
if(0 == message_json || 0 == username_json) {
|
||||
@ -105,6 +95,7 @@ const char *add_post_request(const json_t *json) {
|
||||
return prepare_response(error, sizeof error);
|
||||
}
|
||||
|
||||
// returns the updated post count to the client
|
||||
const char *count = get_posts_count();
|
||||
if(0 == count) {
|
||||
const char error[] = "get_posts_count failed";
|
||||
@ -118,6 +109,7 @@ const char *add_post_request(const json_t *json) {
|
||||
}
|
||||
|
||||
const char *fetch_posts_request(const json_t *json) {
|
||||
// try to extract username, offset and count fields
|
||||
const json_t *username_json = json_getProperty(json, "username");
|
||||
const json_t *offset_json = json_getProperty(json, "offset");
|
||||
const json_t *count_json = json_getProperty(json, "count");
|
||||
@ -142,6 +134,7 @@ const char *fetch_posts_request(const json_t *json) {
|
||||
|
||||
char *result = "";
|
||||
if(0 == username_json) {
|
||||
// if no username specified, jsut return all posts
|
||||
result = get_all_posts(offset, count);
|
||||
if(0 == result) {
|
||||
const char error[] = "get_all_posts failed";
|
||||
|
@ -18,7 +18,7 @@ char *add_post(const char *username, int username_length, const char *message, i
|
||||
const int request_size = username_length + message_length + 50;
|
||||
char *add_sql = (char *)malloc(request_size);
|
||||
|
||||
const int add_sql_length = snprintf(add_sql, request_size, "INSERT INTO messages VALUES(%s, %s)", message, username);
|
||||
const int add_sql_length = snprintf(add_sql, request_size, "INSERT INTO messages VALUES(\"%s\", \"%s\")", message, username);
|
||||
if(add_sql_length < 0) {
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user