Minimal User Interface (C++)
psidk can provide a minimal UI to your services. This UI can help get you started developing your own services, but isn't suitable for end users.
Here is the service definition. Place example.cpp and CMakeLists.txt in an empty folder.
example.cpp
#include <psibase/psibase.hpp>
struct ExampleService
{
int32_t add(int32_t a, int32_t b) { return a + b; }
int32_t multiply(int32_t a, int32_t b) { return a * b; }
// This action serves HTTP requests
std::optional<psibase::HttpReply> serveSys(psibase::HttpRequest request)
{
// serveSimpleUI serves UI files to the browser and
// provides an RPC interface for preparing transactions.
return serveSimpleUI<ExampleService, true>(request);
}
};
PSIO_REFLECT(ExampleService, //
method(add, a, b),
method(multiply, a, b),
method(serveSys, request))
PSIBASE_DISPATCH(ExampleService)
CMakeLists.txt
# All cmake projects need these
cmake_minimum_required(VERSION 3.16)
project(example)
# Generate compile_commands.json to aid vscode and other editors
set(CMAKE_EXPORT_COMPILE_COMMANDS on)
# Libraries for building services and tests
find_package(psibase REQUIRED)
# Build example.wasm service
add_executable(example example.cpp)
target_link_libraries(example Psibase::service)
# Create a package
psibase_package(
NAME MinimalUI
VERSION 1.0.0
DESCRIPTION "An example service with the minimal UI"
# The account that the service will run on
SERVICE example
# The CMake target that builds the service
TARGET example
# The service that will handle HTTP requests.
SERVER example
)
The SERVER option registers an RPC service to handle HTTP requests. In this simple case, example will handle its own requests. See the registerServer docs for more details.
Building
This will create MinimalUI.psi:
mkdir build
cd build
cmake `psidk-cmake-args` ..
make -j $(nproc)
Deploying the service
This will deploy the service to the chain:
psibase install ./MinimalUI.psi
Trying the service
If you're running a test chain locally, then it will typically be at http://psibase.localhost:8080/. To try the service, prefix your host with your service name (e.g. http://example.psibase.localhost:8080/).
How it works
- psinode forwards most http requests to the
http-serverservice. http-serverlooks at the request domain. If it begins with the name of a registered service, it calls that service'sserveSysaction to process the request.- psibase::serveSimpleUI handles the following requests:
GET /returns a minimal html file which loads the javascript to generate the UI.GET /action_templatesreturns a template json structure (below). This lets the UI know which actions are available and sample values for their arguments. This isn't a schema; it's only suitable for simple cases.POST /pack_action/addaccepts the arguments for theaddaction as a JSON object, converts it to binary, and returns the result.
For more detail, see Web Services.
/action_templates result
{
"add": {
"a": 0,
"b": 0
},
"multiply": {
"a": 0,
"b": 0
},
"serveSys": { ... }
}