{"id":21659645,"url":"https://github.com/second-state/microservice-rust-mysql","last_synced_at":"2025-10-10T02:41:59.395Z","repository":{"id":62073911,"uuid":"545849339","full_name":"second-state/microservice-rust-mysql","owner":"second-state","description":"A template project for building a database-driven microservice in Rust and run it in the WasmEdge sandbox.","archived":false,"fork":false,"pushed_at":"2025-03-27T09:19:04.000Z","size":69,"stargazers_count":366,"open_issues_count":5,"forks_count":57,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-07-18T00:36:21.363Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/second-state.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-10-05T04:40:02.000Z","updated_at":"2025-06-29T17:41:16.000Z","dependencies_parsed_at":"2023-02-18T20:16:09.145Z","dependency_job_id":"413c99af-461d-4575-8c77-8e2889112240","html_url":"https://github.com/second-state/microservice-rust-mysql","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/second-state/microservice-rust-mysql","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/second-state%2Fmicroservice-rust-mysql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/second-state%2Fmicroservice-rust-mysql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/second-state%2Fmicroservice-rust-mysql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/second-state%2Fmicroservice-rust-mysql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/second-state","download_url":"https://codeload.github.com/second-state/microservice-rust-mysql/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/second-state%2Fmicroservice-rust-mysql/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279002541,"owners_count":26083403,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-10T02:00:06.843Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-25T09:31:20.266Z","updated_at":"2025-10-10T02:41:59.356Z","avatar_url":"https://github.com/second-state.png","language":"Rust","funding_links":[],"categories":["Rust"],"sub_categories":[],"readme":"# Secure \u0026 lightweight microservice with a database backend\n\nIn this repo, we demonstrate a microservice written in Rust, and connected to a MySQL database. It supports CRUD operations on a database table via a HTTP service interface. The microservice is compiled into WebAssembly (Wasm) and runs in the WasmEdge Runtime, which is a secure and lightweight alternative to natively compiled Rust apps in Linux containers. The WasmEdge Runtime can be managed and orchestrated by container tools such as the Docker, Podman, as well as almost all flavors of Kubernetes. It also works with microservice management frameworks such as Dapr. Checkout [this article](https://blog.logrocket.com/rust-microservices-server-side-webassembly/) or [this video](https://www.youtube.com/watch?v=VSqMPFr7SEs) to learn how the Rust code in this microservice works.\n\n## Quickstart with Docker\n\nThe easiest way to get started is to use a version of Docker Desktop or Docker CLI with Wasm support.\n\n* [Install Docker Desktop + Wasm (Beta)](https://docs.docker.com/desktop/wasm/)\n* [Install Docker CLI + Wasm](https://github.com/chris-crone/wasm-day-na-22/tree/main/server)\n\nThen, you just need to type one command.\n\n```bash\nexport BUILDX_NO_DEFAULT_ATTESTATIONS=1\ndocker compose up\n```\n\nThis will build the Rust source code, run the Wasm server, and startup a MySQL backing database. It also starts a basic STATIC web interface (available at http://localhost:8090). See the [Dockerfile](Dockerfile) and [docker-compose.yml](docker-compose.yml) files. You can jump directly to the [CRUD tests](#crud-tests) section to interact with the web service.\n\nHowever, if you want to build and run the microservice app step by step on your own system. Follow the detailed instructions below.\n\n## Prerequisites\n\n* [Install Rust](https://www.rust-lang.org/tools/install).\n* [Install WasmEdge](https://wasmedge.org/book/en/quick_start/install.html).\n* [Install and start the MySQL database](https://dev.mysql.com/doc/mysql-installation-excerpt/8.0/en/).\n\nOn Linux, you can use the following commands to install Rust and WasmEdge.\n\n```bash\n# Install Rust\ncurl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh\nsource $HOME/.cargo/env\n# Install WebAssembly target for Rust\nrustup target add wasm32-wasi\n\n# Install WasmEdge\ncurl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | bash -s -- -e all\nsource $HOME/.wasmedge/env\n```\n\n## Build\n\nUse the following command to build the microservice. A WebAssembly bytecode file (`wasm` file) will be created.\n\n```bash\ncargo build --target wasm32-wasi --release\n```\n\nYou can run the AOT compiler on the `wasm` file. It could significantly improvement the performance of compute-intensive applications. This microservice, however, is a network intensitive application. Our use of async HTTP networking (Tokio and hyper) and async MySQL connectors are crucial for the performance of this microservice.\n\n```bash\nwasmedgec target/wasm32-wasi/release/order_demo_service.wasm order_demo_service.wasm\n```\n\n## Run\n\nYou can use the `wasmedge` command to run the `wasm` application. It will start the server. Make sure that you pass the MySQL connection string as the env variable to the command. \n\n```bash\nwasmedge --env \"DATABASE_URL=mysql://user:passwd@127.0.0.1:3306/mysql\" order_demo_service.wasm\n```\n\n## CRUD tests\n\nOpen another terminal, and you can use the `curl` command to interact with the web service.\n\nWhen the microservice receives a GET request to the `/init` endpoint, it would initialize the database with the `orders` table.\n\n```bash\ncurl http://localhost:8080/init\n```\n\nWhen the microservice receives a POST request to the `/create_order` endpoint, it would extract the JSON data from the POST body and insert an `Order` record into the database table.\nFor multiple records, use the `/create_orders` endpoint and POST a JSON array of `Order` objects.\n\n```bash\ncurl http://localhost:8080/create_orders -X POST -d @orders.json\n```\n\nWhen the microservice receives a GET request to the `/orders` endpoint, it would get all rows from the `orders` table and return the result set in a JSON array in the HTTP response.\n\n```bash\ncurl http://localhost:8080/orders\n```\n\nWhen the microservice receives a POST request to the `/update_order` endpoint, it would extract the JSON data from the POST body and update the `Order` record in the database table that matches the `order_id` in the input data.\n\n```bash\ncurl http://localhost:8080/update_order -X POST -d @update_order.json\n```\n\nWhen the microservice receives a GET request to the `/delete_order` endpoint, it would delete the row in the `orders` table that matches the `id` GET parameter.\n\n```bash\ncurl http://localhost:8080/delete_order?id=2\n```\n\nThat's it. Feel free to fork this project and use it as a template for your own lightweight microservices!\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsecond-state%2Fmicroservice-rust-mysql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsecond-state%2Fmicroservice-rust-mysql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsecond-state%2Fmicroservice-rust-mysql/lists"}