Skip to content

sumit03guha/rest_api-rocket-rust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A very basic web server built on top of the Rocket web framework using Rust

This web server is a very basic implementation of a REST API server built on top of the Rocket web framework using Rust, where the user can add, delete and get items from a To-Do list.

Useful links

Useful toolchain extensions

  • Recommended extension to install that watching and auto-reloading of the application through the use of cargo watch:

    cargo install cargo-watch

Useful VSCode extensions

Compiling and Starting the Server

  • To compile for development:

    cargo build

    This creates a binary in the target/debug/rest_api-rocket-rust directory.

  • If cargo-watch is installed, then the following command can be used to compile and start the server:

    cargo watch -x run

    which will create the corresponding binary in target/release:

    target/release/rest_api-rocket-rust

    This will create a file data.sqlite in the root directory of the project.
    The server will, by default, start an HTTP API server on port 8000 and will only be accessible as localhost.

Sending requests to the server

  • To send a request to the server, use the following command in your terminal after starting the server:

    curl localhost:8000

    This will return the following response:

    {
      "message": "Hello, world!"
    }
    curl localhost:8000/todo

    This will return the following response:

    {
      "items": []
    }

    This will empty for the first time, since we have not added any items to the list yet.

  • To add an item to the list, use the following command:

    curl localhost:8000/todo -X POST -d '"Item number 1"' -H "Content-Type: application/json"

    This will return the following response:

    {
      "message": "1 rows inserted"
    }
  • To get the list of items, use the following command:

    curl localhost:8000/todo

    This will return the following response:

    {
      "items": [
        {
          "id": 1,
          "item": "Item number 1"
        }
      ]
    }
  • To delete an item from the list, use the following command by specifying the id of the item:

    curl localhost:8000/todo/1 -X DELETE

    This will return the following response:

    {
      "message": "1 rows deleted"
    }

Releases

No releases published

Packages

No packages published

Languages