Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init version #2

Merged
merged 5 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Configuration for labeler - https://github.com/actions/labeler

🧾 ic_common: src/ic_common/**/*
⛳ ic_test_common: src/ic_test_common/**/*
Actions: .github/workflows/**/*
45 changes: 45 additions & 0 deletions .github/release-drafter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name-template: "v$RESOLVED_VERSION 🌈"
tag-template: "v$RESOLVED_VERSION"
categories:
- title: "💥 Breaking Changes"
label: "💥 Breaking Change"

- title: "🌟 New Features"
label: "🌟 New Feature"

- title: "⭐ Features"
label: "⭐ Feature"

- title: "🐞 Bug Fixes"
label: "🐞 Bug Fix"

- title: "🛠 Improvements"
label: "🛠 Improvement"

- title: "🌀 External Changes"
label: "🔌 3rd Party Plugin"

- title: "📖 Documentation"
label: "📖 Docs"

- title: "🌍 Localization"
label: "🌍 i18n"
change-template: "- $TITLE @$AUTHOR (#$NUMBER)"
change-title-escapes: '\<*_&' # You can add # and @ to disable mentions, and add ` to disable code blocks.
exclude-labels:
- 'skip-changelog'
version-resolver:
major:
labels:
- "major"
minor:
labels:
- "minor"
patch:
labels:
- "patch"
default: patch
template: |
## Changes

$CHANGES
11 changes: 11 additions & 0 deletions .github/workflows/labeler.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: "Pull Request Labeler"
on:
- pull_request_target

jobs:
triage:
runs-on: ubuntu-latest
steps:
- uses: actions/labeler@v3
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
36 changes: 36 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: build

on:
push:
branches:
- main

pull_request:
branches:
- main
- develop

jobs:
build:
runs-on: ubuntu-latest
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: true
container:
image: docker.io/kentsday/dfx_rust:bullseye
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: install canister
run: |
cd $GITHUB_WORKSPACE/src
chmod a+x ./sh_start.sh
chmod a+x ./sh_deploy.sh
chmod a+x ./sh_test.sh
./sh_start.sh
./sh_test.sh
dfx stop
- name: unit test
run: |
cd $GITHUB_WORKSPACE/src
cargo test
29 changes: 29 additions & 0 deletions .github/workflows/release-drafter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Release Drafter

on:
push:
# branches to consider in the event; optional, defaults to all
branches:
- main
# pull_request event is required only for autolabeler
pull_request:
# Only following types are handled by the action, but one can default to all as well
types: [opened, reopened, synchronize]

jobs:
update_release_draft:
runs-on: ubuntu-latest
steps:
# (Optional) GitHub Enterprise requires GHE_HOST variable set
#- name: Set GHE_HOST
# run: |
# echo "GHE_HOST=${GITHUB_SERVER_URL##https:\/\/}" >> $GITHUB_ENV

# Drafts your next Release notes as Pull Requests are merged into "master"
- uses: release-drafter/release-drafter@v5
# (Optional) specify config name to use, relative to .github/. Default: release-drafter.yml
# with:
# config-name: my-config.yml
# disable-autolabeler: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
2 changes: 2 additions & 0 deletions src/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM docker.io/kentsday/dfx_rust:bullseye
COPY config.toml /usr/local/cargo/
5 changes: 5 additions & 0 deletions src/.devcontainer/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'ustc'
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"
44 changes: 44 additions & 0 deletions src/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "Rust",
"build": {
"dockerfile": "Dockerfile",
"args": {
// Use the VARIANT arg to pick a Debian OS version: buster, bullseye
// Use bullseye when on local on arm64/Apple Silicon.
"VARIANT": "bullseye"
}
},
"runArgs": [
"--cap-add=SYS_PTRACE",
"--security-opt",
"seccomp=unconfined"
],
// Set *default* container specific settings.json values on container create.
"settings": {
"lldb.executable": "/usr/bin/lldb",
// VS Code don't watch files under ./target
"files.watcherExclude": {
"**/target/**": true
},
"rust-analyzer.checkOnSave.command": "clippy",
"files.exclude": {
".dfx": true
}
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"mutantdino.resourcemonitor",
"tamasfe.even-better-toml",
"serayuzgur.crates"
],
"mounts": [
"target=/workspaces/ic_common/src/.dfx,type=volume",
"target=/workspaces/ic_common/src/target,type=volume"
]
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "rustc --version",
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
// "remoteUser": "vscode"
}
Empty file added src/.dfx/icx-proxy-pid
Empty file.
1 change: 1 addition & 0 deletions src/.dfx/pid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3071
1 change: 1 addition & 0 deletions src/.dfx/replica-configuration/replica-pid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3101
1 change: 1 addition & 0 deletions src/.dfx/state/replicated_state/ic.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"registry_client":{"local_store":"/workspaces/ic_common/src/.dfx/state/replicated_state/ic_registry_local_store"},"transport":{"node_ip":"0.0.0.0","p2p_flows":[{"flow_tag":1234,"server_port":0,"queue_size":1024}]},"state_manager":{"state_root":"/workspaces/ic_common/src/.dfx/state/replicated_state/node-100/state"},"hypervisor":{"persistence_type":"sigsegv","create_funds_whitelist":"","max_instructions_for_message_acceptance_calls":5000000000,"subnet_memory_capacity":322122547200,"max_canister_memory_size":12884901888,"max_cycles_per_canister":null,"default_provisional_cycles_balance":100000000000000,"default_freeze_threshold":2592000,"max_controllers":10},"http_handler":{"write_port_to":"/workspaces/ic_common/src/.dfx/replica-configuration/replica-1.port","listen_addr":"127.0.0.1:0","allow_ipv6_my_users_have_no_privacy":null,"show_root_key_in_status":true,"clients_x509_cert":null},"metrics":null,"artifact_pool":{"consensus_pool_path":"/workspaces/ic_common/src/.dfx/state/replicated_state/node-100/ic_consensus_pool","ingress_pool_size_threshold":null,"consensus_pool_backend":"rocksdb"},"consensus":null,"crypto":{"crypto_root":"/workspaces/ic_common/src/.dfx/state/replicated_state/node-100/crypto"},"logger":{"node_id":100,"dc_id":200,"level":"warning","format":"text_full","debug_overrides":[],"sampling_rates":{},"enabled_tags":[],"target":"Stdout","block_on_overflow":true},"nodemanager_logger":null,"message_routing":null,"malicious_behaviour":null,"firewall":null,"registration":null,"nns_registry_replicator":null}
1 change: 1 addition & 0 deletions src/.dfx/webserver-port
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8000
10 changes: 10 additions & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Generated by Cargo
# will have compiled files and executables
**/target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
**/Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk
6 changes: 6 additions & 0 deletions src/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[workspace]
members = [
"ic_common",
"ic_test_common",
"canisters/my_example"
]
26 changes: 26 additions & 0 deletions src/canisters/my_example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "my_example"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["cdylib"]

[dependencies]
ic-cdk = "0.3.3"
ic-cdk-macros = "0.3.3"
candid = "0.7.8"
serde = "1.0.127"
serde_bytes = "0.11"
thiserror = "1.0"
ic_common = { path = "../../ic_common" }
log = "0.4"
url = "2.2.2"

[dev-dependencies]
env_logger = "0.9.0"
ic_test_common = { path = "../../ic_test_common" }
rstest = "0.11.0"
async-std = { version = "1.5", features = ["attributes"] }
33 changes: 33 additions & 0 deletions src/canisters/my_example/src/actor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use candid::{candid_method, Principal};
use ic_cdk_macros::*;

use ic_common::errors::{ActorResult, to_actor_result};

use crate::models::User;
use crate::service::UserService;

#[query(name = "get_user_by_id")]
#[candid_method(query, rename = "get_user_by_id")]
fn get_user_by_id(id: Principal) -> ActorResult<User> {
let service = UserService::new();
let result = service.get_user_by_id(&id);
to_actor_result(result)
}

// add new user with given id, name and email
#[update(name = "add_user")]
#[candid_method(update, rename = "add_user")]
fn add_user(id: Principal, name: String, email: String) -> ActorResult<()> {
let service = UserService::new();
let result = service.add_user(&id, &name, &email);
to_actor_result(result)
}


candid::export_service!();

#[query(name = "__get_candid_interface_tmp_hack")]
#[candid_method(query, rename = "__get_candid_interface_tmp_hack")]
fn __export_did_tmp_() -> String {
__export_service()
}
32 changes: 32 additions & 0 deletions src/canisters/my_example/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use thiserror::Error;

use ic_common::errors::{ErrorInfo, IServiceError, ServiceResult};

#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Error)]
pub(crate) enum UserServiceError {
#[error("User not found")]
UserNotFound,
#[error("User already exists")]
UserAlreadyExists,
}

impl UserServiceError {
pub fn code(&self) -> u32 {
match self {
UserServiceError::UserNotFound => 1,
UserServiceError::UserAlreadyExists => 2,
}
}
}


impl IServiceError for UserServiceError {
fn to_error_info(&self) -> ErrorInfo {
ErrorInfo {
code: self.code(),
message: self.to_string(),
}
}
}

pub(crate) type UserServiceResult<T> = ServiceResult<T, UserServiceError>;
29 changes: 29 additions & 0 deletions src/canisters/my_example/src/http.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use candid::{candid_method, Principal};
use ic_cdk_macros::*;
use log::info;

use ic_common::http::{HttpRequest, HttpResponse};

use crate::service::UserService;

#[query]
#[candid_method(query, rename = "http_request")]
fn http_request(req: HttpRequest) -> HttpResponse {
info!("request: {:?}", req);
let id = req.get_query_value("id");

if id.is_none() {
return HttpResponse::string(400, "id is required");
}
let principal = Principal::from_text(id.unwrap());
if principal.is_err() {
return HttpResponse::string(400, "id is invalid");
}

let service = UserService::new();
let user = service.get_user_by_id(&principal.unwrap());
if user.is_err() {
return HttpResponse::string(400, "user not found");
}
HttpResponse::string(200, format!("{}", user.unwrap()).as_str())
}
7 changes: 7 additions & 0 deletions src/canisters/my_example/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod actor;
mod http;
mod models;
mod service;
mod startup;
mod state;
mod errors;
Loading