Coder Social home page Coder Social logo

rust_rest_example's Introduction

Restful rusty rocket example

Example application to test the use of the rust web framework rocket by building a simple rest API with JWT Auth. Since it bothered me to find so many example applications online with either hard-coded API keys or user databases with plaintext passwords i decided to do a simple one myself to try out a few techniques. Application is by no means complete or secure, this is just some example and notes on several things. Obligatory don't copy-paste this and use it in production or anywhere public.

Feedback is welcome and appreciated.

Features:

  • rocket
  • diesel
  • rust crypto (crates crypt-mac, argon2)
  • Basic REST API (GET, POST)
    • Login Endpoint
    • JWT Authentication
    • example data endpoint

Open TODOs

  • Improve Documentation
  • Clean up code
  • Reorganize dependencies and improve modularity

Database Connection - Diesel

Setup

Spin up your favourite diesel-compatible db engine. I use postgreSQL in this example.

Make sure to have set the database connection in the DATABASE_URL environment variable like this export DATABASE_URL=postgres://<user>:<password>@localhost/<database>

And do the following:

  1. Generate project filesdiesel setup
  2. Generate migration files diesel migration generate <name>
  3. Edit migration files
  4. Apply migration diesel migration run

Optionally run diesel migration redo to test if your down.sql file works, too.

psql -U root webtest

Environment variables

APPLICATION_SECRET - A secret application key used for jwt. Should be random and strong
DATABASE_URL - The url to the postgresql database

Curl commands

Some curl commands to test the API locally

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"name":"Bike","coolness":100,"wattage":8000,"description":"The coolest thing in the world"}' \
  http://localhost:8000/api/vehicle

curl --header "Content-Type: application/json" \
  --request PUT \
  --data '{"id":2, "name":"Roadbike","coolness":101,"wattage":8000,"description":"The coolest thing in the world"}' \
  http://localhost:8000/api/vehicle/2

curl --header "Authentication: XYZ-token" --request GET -I http://localhost:8000/

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"username":"leon", "password": "xyz"}' \
  http://localhost:8000/login

Reading List

rust_rest_example's People

Contributors

leonholub avatar

Stargazers

Temirzhan Yussupov avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.