A companion repository for my Rust Talk.

Overview

Building a microservice in rust

This project is a companion to my talk at ConFoo about building a rust project.

You should checkout a fully fleshed out starter template for building microservices at tarkalabs/muservice-rs.

If you just cloned the repo you'd want to get to the step-0 branch.

git checkout step-0

Step 1

This step adds the dependencies like tokio, anyhow and tracing_subscriber. This sets the project up for non-blocking IO and adds logging and error handling libraries.

This also configures the binary to log if RUST_LOG=debug environment variable is set.

RUST_LOG=debug cargo run

Step 2

This steps refactors the application into a library and a binary. This allows multiple binary endpoints that may use the same underlying crate for things like the SQS worker or the HTTP handler.

Step 3

This step adds axum and sets up a hello world endpoint.

  • Setup and start server - ./src/server.rs
  • Router - ./src/router.rs
  • Error - ./src/error.rs

Step 4

This step adds serde and serde_json for serializing structs. We need this for reading the request body and returning JSON.

  • Path Parameters - ./src/router/path_handler.rs
  • Query Parameters - ./src/router/query_handler.rs
  • JSON body handler - ./src/router/json_handler.rs

Step 5

This steps adds database handling to our service. Here we are using postgresql with sqlx. We are also going to add chrono for our date types and async_trait to build a repository trait and an implementation for it using the concrete database. This will help us test the router with just a mock implementation of the trait while the DAO tests can hit the actual database.

We'll need an environment variable called DATABASE_URL for cargo to build and run type checked SQL. You can opt out of it but I prefer it.

I've set my DATABASE_URL to postgres:///testdb

  • DAO - ./src/dao/mod.rs
  • Tasks - ./src/dao/tasks.rs
  • App State - ./src/app/mod.rs
  • API - ./src/router/tasks_api.rs

Run git checkout step-06 to get to the next step.

Step 6

In this step we accept an Authorization header. If the authorization header is not present or if it is incorrect we present a 401. If not we show the claims as a JSON object.

  • Secure Route - ./src/router/auth_endpoint.rs
  • Include Secret - ./src/app/mod.rs and ./src/router.rs
http localhost:3000/api/secure Authorization:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI0MiIsIm5hbWUiOiJWYWdtaSIsImlhdCI6MTUxNjIzOTAyMiwiZXhwIjoxODc3MjI0Njk2fQ.ePx-Gzd6-PsZ2vKn7v5QmpgAt8vQ8c5AgXLsHR1AFR0
  • Bootstrap Rust project
  • Add tokio and anyhow and run as an async binary
  • Library and binary structure and setup tracing
  • Add axum and setup a hello world endpoint
  • Explore different extractors for Path, Body and Query Params
  • Add database support with sqlx and postgres
  • Authentication based on a JWT token
You might also like...
Repository with my Advent of Code 2021 puzzle solutions ๐ŸŽ„
Repository with my Advent of Code 2021 puzzle solutions ๐ŸŽ„

๐ŸŽ„ Advent of Code 2021 ๐ŸŽ„ I decided to stick with Rust this year and try to improve a bit on it, I basically haven't used it since last year's AoC, so

Official repository for v3's backend

RoChat This repository holds the official source code for RoChat's new backend. Previously, the old backend was written in PHP. The newer one, coming

The official command-line interface for the makedeb Package Repository

mpr-cli This is the repository for the MPR CLI, the official command-line interface for the makedeb Package Repository. Installation Users have a few

A git sub-command to view your git repository in the web browser
A git sub-command to view your git repository in the web browser

git-view A git sub-command to view your git repository in the web browser! About Are you also frustrated from moving your hands away from the keyboard

Copy files from Git repository to local.

gitcp Copy files from Git repository to local. Install We are planning to add some installers support in the future. e.g. homebrew winget debian packa

๐Ÿš€ Yet another repository management with auto-attaching profiles.

๐Ÿš€ ghr Yet another repository management with auto-attaching profiles. ๐Ÿ”ฅ Motivation ghq is the most famous solution to resolve stress of our reposito

Repository for solving adventofcode.com puzzles

Advent Of Code ๐ŸŽ„ This is a repository for any Otovista that wants to participate in the advent of code christmas calendar challenges. Advent of Code

This is a placeholder repository for Ludum Dare 52. We'll add more soon!

Ludum Dare 52 This is a placeholder repository for Ludum Dare 52. How to play TBD Developing Only Rust (edition 2021) is required to develop and build

a command-line tool that transforms a Git repository into a minimal format for ChatGPT queries
a command-line tool that transforms a Git repository into a minimal format for ChatGPT queries

gprepo /dส’iหpiหหˆษนi:pi:oสŠ/ a command-line tool that transforms a Git repository into a minimal format for ChatGPT queries. Features Excludes LICENSE an

Comments
  • Bump axum from 0.6.7 to 0.6.8

    Bump axum from 0.6.7 to 0.6.8

    Bumps axum from 0.6.7 to 0.6.8.

    Release notes

    Sourced from axum's releases.

    axum - v0.6.8

    • fixed: Fix Allow missing from routers with middleware (#1773)
    • added: Add KeepAlive::event for customizing the event sent for SSE keep alive (#1729)

    #1729: tokio-rs/axum#1729 #1773: tokio-rs/axum#1773

    Commits

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    dependencies 
    opened by dependabot[bot] 0
Owner
Vagmi
I write code and like working with awesome folks.
Vagmi
A CLI companion tool for paste.misterio.me, allowing you to easily upload and manage your pastes

This is a CLI companion tool for paste.misterio.me, allowing you to easily upload and manage your pastes, as well as download any pastes you want.

Gabriel Fontes 1 Jan 26, 2022
A terminal-based companion program for ComfyUI.

Yara Yara is a companion tool for ComfyUI, based in the terminal. It can: Pause queue generations by saving/loading them to files Cancel queued genera

null 3 Jul 27, 2023
Accompanying code for my talk "No free lunch: Limits of Wasm as a bridge from Rust to JS" presented @ EuroRust2022 in Berlin

No free lunch: Limits of Wasm as a bridge from Rust to JS Accompanying code for the talk I presented at EuroRust 2022 in Berlin, Germany Slides for th

Alberto Schiabel 11 Dec 30, 2022
This repository presents a numbers vizualizer in a polar base. This small project has been entirely made in Rust !

NumbersRepresentation This repository presents a numbers vizualizer in a polar base. This small project has been entirely made in Rust ! This is an id

Lilian 'S3l4h' Schall 3 Apr 12, 2022
A command line tool, manage your hundreds of repository, written with Rust

A command line tool, manage your hundreds of repository, written with Rust

Axetroy 4 Aug 16, 2022
๐Ÿฆ€ Temporary repository for the rewrite of Smoothie in Rust

Warning As with a lot of projects, this one is also a WIP, expect broken code ?? smoothie-rs Temporary repository hosting the code for Smoothie's futu

Couleur Tweak Tips 6 Jan 22, 2023
Client for spotify's apt repository in Rust for Arch Linux

spotify-launcher Spotify has a free linux client but prohibits re-distribution, so this is a freely distributable opensource program that manages a sp

null 93 Jun 20, 2023
This repository brings together my studies in the Rust language.

Studying_Rust This repository brings together my studies in the Rust language. Study schedule in 90 days start date: 7/24 end date: 10/24 Each topic w

Dรฉbora Marim 5 Aug 8, 2023
RustRedOps is a repository dedicated to gathering and sharing advanced techniques and malware for Red Team, with a specific focus on the Rust programming language. (In Construction)

RustRedOps In Construction.... The project is still under development Overview RustRedOps is a repository that houses various tools and projects relat

Joรฃo Victor 17 Dec 14, 2023
Repository containing assets for the Holium CLI tutorial

Welcome to getting-started ?? In this repository you can find all necessary assets for the Holium CLI tutorial ?? Homepage The tutorial that reference

Polyphene 2 Mar 11, 2022