Skip to content

Commit

Permalink
Merge pull request #867 from gofractally/use-sites-for-storesys
Browse files Browse the repository at this point in the history
Use sites for storesys
  • Loading branch information
James-Mart authored Sep 27, 2024
2 parents 13fb8f4 + 503c04a commit 7d40088
Show file tree
Hide file tree
Showing 68 changed files with 3,266 additions and 912 deletions.
57 changes: 30 additions & 27 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ add_rs_component(services/user/Invite/plugin:InvitesPlugin invite.wasm wasm32-wa
add_rs_component(services/user/Tokens/plugin:TokensPlugin tokens.wasm wasm32-wasi )
add_rs_component(services/user/Supervisor/plugin:TestsPlugin test.wasm wasm32-wasi )
add_rs_component(services/user/ClientData/plugin:ClientDataPlugin clientdata.wasm wasm32-wasi )
add_rs_component(services/user/Sites/plugin:SitesPlugin sites.wasm wasm32-wasi )

set(WASM_PSIBASE_BYPRODUCTS
${CMAKE_CURRENT_SOURCE_DIR}/services/user/XAdmin/ui/wasm-psibase/wasm-psibase_bg.js
Expand Down Expand Up @@ -379,14 +380,14 @@ psibase_package(
WASM ${CMAKE_CURRENT_BINARY_DIR}/Transact.wasm
FLAGS allowSudo allowWriteNative
SERVER r-transact
DATA ${COMPONENT_BIN_DIR}/${TransactPlugin_OUTPUT_FILE} /plugin.wasm
SERVICE r-transact
WASM ${CMAKE_CURRENT_BINARY_DIR}/RTransact.wasm
FLAGS allowWriteSubjective
DATA ${COMPONENT_BIN_DIR}/${TransactPlugin_OUTPUT_FILE} /plugin.wasm
POSTINSTALL ${CMAKE_CURRENT_SOURCE_DIR}/services/system/Transact/src/postinstall.json
DEPENDS wasm
DEPENDS ${TransactPlugin_DEP}
PACKAGE_DEPENDS "Accounts(^${PSIBASE_VERSION})" "CpuLimit(^${PSIBASE_VERSION})" "HttpServer(^${PSIBASE_VERSION})"
PACKAGE_DEPENDS "Accounts(^${PSIBASE_VERSION})" "CpuLimit(^${PSIBASE_VERSION})" "HttpServer(^${PSIBASE_VERSION})" "Sites(^${PSIBASE_VERSION})"
)

psibase_package(
Expand All @@ -397,13 +398,13 @@ psibase_package(
SERVICE accounts
WASM ${CMAKE_CURRENT_BINARY_DIR}/Accounts.wasm
SERVER r-accounts
DATA ${COMPONENT_BIN_DIR}/${AccountsPlugin_OUTPUT_FILE} /plugin.wasm
INIT
SERVICE r-accounts
WASM ${CMAKE_CURRENT_BINARY_DIR}/RAccounts.wasm
DATA ${COMPONENT_BIN_DIR}/${AccountsPlugin_OUTPUT_FILE} /plugin.wasm
DEPENDS wasm
DEPENDS ${AccountsPlugin_DEP}
PACKAGE_DEPENDS "AuthAny(^${PSIBASE_VERSION})" "HttpServer(^${PSIBASE_VERSION})"
PACKAGE_DEPENDS "AuthAny(^${PSIBASE_VERSION})" "HttpServer(^${PSIBASE_VERSION})" "Sites(^${PSIBASE_VERSION})"
)

psibase_package(
Expand All @@ -430,16 +431,16 @@ psibase_package(
DESCRIPTION "Auth service using Botan to verify signatures"
SERVICE auth-sig
WASM ${CMAKE_CURRENT_BINARY_DIR}/AuthSig.wasm
DATA ${COMPONENT_BIN_DIR}/${AuthPlugin_OUTPUT_FILE} /plugin.wasm
SERVER r-auth-sig
SERVICE r-auth-sig
WASM ${CMAKE_CURRENT_BINARY_DIR}/RAuthSig.wasm
DATA ${COMPONENT_BIN_DIR}/${AuthPlugin_OUTPUT_FILE} /plugin.wasm
SERVICE verify-sig
WASM ${CMAKE_CURRENT_BINARY_DIR}/VerifySig.wasm
FLAGS isAuthService
DEPENDS wasm
DEPENDS ${AuthPlugin_DEP}
PACKAGE_DEPENDS "HttpServer(^${PSIBASE_VERSION})"
PACKAGE_DEPENDS "HttpServer(^${PSIBASE_VERSION})" "Sites(^${PSIBASE_VERSION})"
)

set(COMMON_API ${CMAKE_CURRENT_SOURCE_DIR}/services/user/CommonApi)
Expand Down Expand Up @@ -470,7 +471,7 @@ psibase_package(
DEPENDS ${CommonApiCommonLib_js_DEP}
DEPENDS ${ComponentParser_DEP}
DEPENDS wasm
PACKAGE_DEPENDS "HttpServer(^${PSIBASE_VERSION})"
PACKAGE_DEPENDS "HttpServer(^${PSIBASE_VERSION})" "Sites(^${PSIBASE_VERSION})"
)

psibase_package(
Expand All @@ -489,7 +490,7 @@ psibase_package(
VERSION ${PSIBASE_VERSION}
DESCRIPTION "Insecure auth service that allows any access"
SERVICE auth-any
WASM ${CMAKE_CURRENT_BINARY_DIR}/AuthAny.wasm
WASM ${CMAKE_CURRENT_BINARY_DIR}/AuthAny.wasm
DEPENDS wasm
)

Expand All @@ -499,8 +500,8 @@ psibase_package(
VERSION ${PSIBASE_VERSION}
DESCRIPTION "Service that manages CPU billing"
SERVICE cpu-limit
WASM ${CMAKE_CURRENT_BINARY_DIR}/CpuLimit.wasm
FLAGS isSubjective canSetTimeLimit
WASM ${CMAKE_CURRENT_BINARY_DIR}/CpuLimit.wasm
FLAGS isSubjective canSetTimeLimit
DEPENDS wasm
)

Expand All @@ -510,7 +511,7 @@ psibase_package(
VERSION ${PSIBASE_VERSION}
DESCRIPTION "Psibase documentation"
SERVICE docs
DATA ${CMAKE_CURRENT_BINARY_DIR}/book/html /
DATA ${CMAKE_CURRENT_BINARY_DIR}/book/html /
PACKAGE_DEPENDS "Sites(^${PSIBASE_VERSION})" "Accounts(^${PSIBASE_VERSION})"
DEPENDS doc ${CMAKE_CURRENT_BINARY_DIR}/book/html/index.html
)
Expand All @@ -521,8 +522,8 @@ psibase_package(
VERSION ${PSIBASE_VERSION}
DESCRIPTION "Network homepage"
SERVICE homepage
DATA GLOB ${CMAKE_CURRENT_SOURCE_DIR}/services/user/Homepage/ui/dist/* /
POSTINSTALL ${CMAKE_CURRENT_SOURCE_DIR}/services/user/Homepage/postinstall.json
DATA GLOB ${CMAKE_CURRENT_SOURCE_DIR}/services/user/Homepage/ui/dist/* /
POSTINSTALL ${CMAKE_CURRENT_SOURCE_DIR}/services/user/Homepage/postinstall.json
PACKAGE_DEPENDS "Sites(^${PSIBASE_VERSION})" "Accounts(^${PSIBASE_VERSION})"
DEPENDS ${Homepage_js_DEP}

Expand All @@ -534,7 +535,7 @@ psibase_package(
VERSION ${PSIBASE_VERSION}
DESCRIPTION "Service that does nothing"
SERVICE nop
WASM ${CMAKE_CURRENT_BINARY_DIR}/Nop.wasm
WASM ${CMAKE_CURRENT_BINARY_DIR}/Nop.wasm
DEPENDS wasm
)

Expand All @@ -544,9 +545,11 @@ psibase_package(
VERSION ${PSIBASE_VERSION}
DESCRIPTION "Provides web hosting to non-service accounts"
SERVICE sites
WASM ${CMAKE_CURRENT_BINARY_DIR}/Sites.wasm
SERVER sites
WASM ${CMAKE_CURRENT_BINARY_DIR}/Sites.wasm
SERVER sites
DATA ${COMPONENT_BIN_DIR}/${SitesPlugin_OUTPUT_FILE} /plugin.wasm
DEPENDS wasm
DEPENDS ${SitesPlugin_DEP}
PACKAGE_DEPENDS "HttpServer(^${PSIBASE_VERSION})"
)

Expand Down Expand Up @@ -587,8 +590,8 @@ psibase_package(
VERSION ${PSIBASE_VERSION}
DESCRIPTION "All compiled code is uploaded to the chain through this service"
SERVICE setcode
WASM ${CMAKE_CURRENT_BINARY_DIR}/SetCode.wasm
FLAGS allowWriteNative
WASM ${CMAKE_CURRENT_BINARY_DIR}/SetCode.wasm
FLAGS allowWriteNative
DEPENDS wasm
)

Expand All @@ -603,7 +606,7 @@ psibase_package(
SERVER r-events
SERVICE r-events
WASM ${CMAKE_CURRENT_BINARY_DIR}/REvents.wasm
POSTINSTALL ${CMAKE_CURRENT_SOURCE_DIR}/services/user/Events/src/postinstall.json
POSTINSTALL ${CMAKE_CURRENT_SOURCE_DIR}/services/user/Events/src/postinstall.json
DEPENDS wasm
PACKAGE_DEPENDS "Transact(^${PSIBASE_VERSION})" "HttpServer(^${PSIBASE_VERSION})"
)
Expand All @@ -614,20 +617,20 @@ psibase_package(
VERSION ${PSIBASE_VERSION}
DESCRIPTION "Block explorer"
SERVICE explorer
WASM ${CMAKE_CURRENT_BINARY_DIR}/Explorer.wasm
SERVER explorer
DATA ${CMAKE_CURRENT_SOURCE_DIR}/services/user/Explorer/ui/dist /
WASM ${CMAKE_CURRENT_BINARY_DIR}/Explorer.wasm
SERVER explorer
DATA ${CMAKE_CURRENT_SOURCE_DIR}/services/user/Explorer/ui/dist /
DEPENDS Explorer_js ${CMAKE_CURRENT_SOURCE_DIR}/services/user/Explorer/ui/dist/index.html
DEPENDS wasm
PACKAGE_DEPENDS "HttpServer(^${PSIBASE_VERSION})"
PACKAGE_DEPENDS "HttpServer(^${PSIBASE_VERSION})" "Sites(^${PSIBASE_VERSION})"
)

psibase_package(
OUTPUT ${SERVICE_DIR}/Invite.psi
NAME Invite
VERSION ${PSIBASE_VERSION}
DESCRIPTION "This service facilitates the creation and redemption of invites"
PACKAGE_DEPENDS "Accounts(^${PSIBASE_VERSION})" "HttpServer(^${PSIBASE_VERSION})" "Nft(^${PSIBASE_VERSION})" "Tokens(^${PSIBASE_VERSION})" "AuthSig(^${PSIBASE_VERSION})"
PACKAGE_DEPENDS "Accounts(^${PSIBASE_VERSION})" "HttpServer(^${PSIBASE_VERSION})" "Nft(^${PSIBASE_VERSION})" "Tokens(^${PSIBASE_VERSION})" "AuthSig(^${PSIBASE_VERSION})" "Sites(^${PSIBASE_VERSION})"
SERVICE invite
WASM ${CMAKE_CURRENT_BINARY_DIR}/Invite.wasm
SERVER invite
Expand Down Expand Up @@ -676,15 +679,15 @@ psibase_package(
NAME Tokens
VERSION ${PSIBASE_VERSION}
DESCRIPTION "Token service"
PACKAGE_DEPENDS "Nft(^${PSIBASE_VERSION})" "HttpServer(^${PSIBASE_VERSION})" "Events(^${PSIBASE_VERSION})"
PACKAGE_DEPENDS "Nft(^${PSIBASE_VERSION})" "HttpServer(^${PSIBASE_VERSION})" "Events(^${PSIBASE_VERSION})" "Sites(^${PSIBASE_VERSION})"
SERVICE tokens
WASM ${CMAKE_CURRENT_BINARY_DIR}/Tokens.wasm
DATA GLOB ${CMAKE_CURRENT_SOURCE_DIR}/services/user/Tokens/ui/dist/* /
DATA ${COMPONENT_BIN_DIR}/${TokensPlugin_OUTPUT_FILE} /plugin.wasm
SERVER r-tokens
INIT
SERVICE r-tokens
WASM ${CMAKE_CURRENT_BINARY_DIR}/RTokens.wasm
DATA GLOB ${CMAKE_CURRENT_SOURCE_DIR}/services/user/Tokens/ui/dist/* /
DATA ${COMPONENT_BIN_DIR}/${TokensPlugin_OUTPUT_FILE} /plugin.wasm
DEPENDS ${Tokens_js_DEP} ${CMAKE_CURRENT_SOURCE_DIR}/services/user/Tokens/ui/dist/index.html
DEPENDS wasm
)
Expand Down
14 changes: 1 addition & 13 deletions dev/DemoApp1/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ mod service {
result: i32,
}

#[table(record = "WebContentRow", index = 1)]
struct WebContentTable;

#[action]
pub fn add(a: i32, b: i32) -> i32 {
let res = a + b;
Expand Down Expand Up @@ -84,16 +81,7 @@ mod service {
#[action]
#[allow(non_snake_case)]
fn serveSys(request: HttpRequest) -> Option<HttpReply> {
None.or_else(|| serve_content(&request, &WebContentTable::new()))
.or_else(|| serve_graphql(&request, Query))
None.or_else(|| serve_graphql(&request, Query))
.or_else(|| serve_graphiql(&request))
}

#[action]
#[allow(non_snake_case)]
fn storeSys(path: String, contentType: String, content: HexBytes) {
check(get_sender() == get_service(), "unauthorized");
let table = WebContentTable::new();
store_content(path, contentType, content, &table).unwrap();
}
}
2 changes: 1 addition & 1 deletion dev/demoapp1-build-and-deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ cd ./ui
rm -rf node_modules
rm -rf dist
yarn --mutex network && yarn build
psibase upload -r $account_name ./dist / -S $account_name
psibase upload -r ./dist / -S $account_name


8 changes: 3 additions & 5 deletions doc/psidk/src/development/front-ends/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
A more in depth guide will be added later, but this is a rough outline of the steps required to host your app front-end:

1. Create a psibase account
2. Deploy a service to the account
3. Implement the [psibase::ServerInterface] and [psibase::StorageInterface]
4. Register the account as a web-server by calling the `registerServer` action on the [http-server](../../default-apps/http-server.md) service. This gives the account a domain name according to the rules specified in the [routing](#virtual-hosting) docs.
5. Develop your user interface, using [HTTP requests](./reference/http-requests.md), [JS libraries](./reference/js-libraries.md) as needed, as well as [plugins](../../specifications/app-architecture/plugins.md) to interact with your service or third-party apps.
6. Bundle your front-end if needed and upload it to your service for storage and serving.
2. Create a psibase package including a service, [plugin](../../specifications/app-architecture/plugins.md), and UI
3. Install the package to your subdomain
4. [Optional] Register an http server that implements the [psibase::ServerInterface] to handle custom RPC/REST/GraphQl queries.

## Virtual hosting

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ TaPoS must be attached to every transaction submitted to a psibase network. In a
### Common files

`common-api` serves files stored in its tables. Chain operators may add files using the `storeSys` action (`psibase upload`).
Booting the network with either the psibase CLI tool or with the x-admin app will automatically install this default set of files:
There are special rules in the standard psibase HTTP server that allow for common files to be accessed at a special path on any subdomain. Any files added to the `common-api` subdomain by the infrastructure providers can be served from any subdomain through this mechanism.

Default common files:

| Path | Description |
| ----------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Another account that will serve content for this account.

#### DATA &lt;path&gt; &lt;dest&gt;

Adds web content that will be installed to the service using `storeSys`. If path is a directory it will be added recursively.
Adds web content that will be installed to the service's namespace within the the `sites` app, accessible from the service's subdomain. If path is a directory it will be added recursively.

```
DATA index.html /index.html
Expand All @@ -93,7 +93,7 @@ DATA dist /

#### DATA GLOB &lt;path&gt;... &lt;dir&gt;

Adds web content that will be installed to the service using `storeSys`. Globs in the source paths will be processed and all matching files will be copied into the destination directory.
Adds web content that will be installed to the service's namespace within the the `sites` app, accessible from the service's subdomain. Globs in the source paths will be processed and all matching files will be copied into the destination directory.

```
DATA GLOB dist/*.html /
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,21 @@

## Routing

```svgbob
+-------------+ +---------+ +---------+
| http-server | | | | HTTP |
| service |<---- | psinode |<---- | Request |
| | | | | |
+-------------+ +---------+ +---------+
|
|
v
+--------------+ +-----------------+
/ \ yes | Common |
/ target begins \ ------> | service's |
\ with "/common?" / | serveSys action |
\ / +-----------------+
+--------------+ ^
| no |
| +-----------+
v |
+----------------+ | +-----------------+
/ \ no | | sites |
/ on a subdomain? \ ---+ | service's |
\ / | serveSys action |
\ / +-----------------+
+----------------+ ^
| yes |
| +----------------+
v |
+------------+ no | +-----------------+
/ \ ---+ | registered |
/ registered? \ | service's |
\ / yes +-->| serveSys action |
\ / -------+ +-----------------+
+------------+
```mermaid
flowchart TD
200[200 OK]
404[404 Not Found]
A[HTTP Request]
B[psinode]
C[http-server service]
D[sites service's serveSys action]
A --> B --> C --> D --> E{{was site data found?}} -->|yes| 200
E -->|no| G{{target begins with '/common'?}} -->|yes| H['common-api' service's serveSys action]
G -->|no| I{{on a subdomain?}} -->|no| 404
I -->|yes| J{{Has registered server?}} -->|no| 404
J -->|yes| L[registered server's serveSys action]
```

`psinode` passes most HTTP requests to the [SystemService::HttpServer] service, which then routes requests to the appropriate service's [serveSys](#psibaseserverinterfaceservesys) action (see diagram). The services run in RPC mode; this prevents them from writing to the database, but allows them to read data they normally can't. See [psibase::DbId].
Expand All @@ -57,17 +39,15 @@ A service doesn't have to serve HTTP requests itself; it may delegate this to an

## HTTP Interfaces

Services which serve HTTP implement these interfaces:
Services which serve HTTP implements this interface:

- [psibase::ServerInterface] (required)
- [psibase::HttpRequest]
- [psibase::HttpReply]
- [psibase::StorageInterface] (optional)

{{#cpp-doc ::psibase::ServerInterface}}
{{#cpp-doc ::psibase::HttpRequest}}
{{#cpp-doc ::psibase::HttpReply}}
{{#cpp-doc ::psibase::StorageInterface}}

## Helpers

Expand All @@ -77,9 +57,6 @@ These help implement basic functionality:
- [psibase::serveActionTemplates]
- [psibase::servePackAction]
- [psibase::serveSchema]
- [psibase::WebContentRow]
- [psibase::storeContent]
- [psibase::serveContent]
- [psibase::serveGraphQL]
- [psibase::makeConnection]
- [psibase::PageInfo]
Expand Down Expand Up @@ -119,9 +96,6 @@ std::optional<psibase::HttpReply> serveSys(psibase::HttpRequest request)
{{#cpp-doc ::psibase::serveActionTemplates}}
{{#cpp-doc ::psibase::servePackAction}}
{{#cpp-doc ::psibase::serveSchema}}
{{#cpp-doc ::psibase::WebContentRow}}
{{#cpp-doc ::psibase::storeContent}}
{{#cpp-doc ::psibase::serveContent}}
{{#cpp-doc ::psibase::serveGraphQL}}
{{#cpp-doc ::psibase::makeConnection}}
{{#cpp-doc ::psibase::PageInfo}}
Expand Down
2 changes: 1 addition & 1 deletion doc/psidk/src/development/services/rust-service/package.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The available fields are:
- `plugin`: May be present on any crate that builds a service. The value is a crate that should be built with `cargo component` and uploaded as `/plugin.wasm`
- `flags`: [Flags](../../../specifications/data-formats/package.md#serviceservicejson) for the service.
- `postinstall`: An array of actions to run when the package is installed. May be specified on the top-level crate or on any service. Actions from a single `postinstall` will be run in order. The order of actions from multiple crates is unspecified.
- `data`: An array of `{src, dst}` paths to upload to the service. `src` is relative to the location of Cargo.toml. If `src` is a directory, its contents will be included recursively.
- `data`: An array of `{src, dst}` paths to upload to the service's subdomain. `src` is relative to the location of Cargo.toml. If `src` is a directory, its contents will be included recursively.
- `dependencies`: Additional packages, not build by cargo, that the package depends on.

Example:
Expand Down
Loading

0 comments on commit 7d40088

Please sign in to comment.