Coder Social home page Coder Social logo

rest-api-rust's Introduction

rest-api-rust

RESTful API in Rust

Prerequisites

Before getting started, ensure you have the following prerequisites installed:

  • Rust
  • MySQL database

Getting Started

Setting up the Database

  1. Create a MySQL database for your project:

    CREATE DATABASE your_database_name;
  2. Copy the .env.sample file to .env and update the database connection details:

    DATABASE_URL=mysql://username:password@localhost/your_database_name

    Replace username, password, and your_database_name with your MySQL credentials.

Generating Migration

Run the following command to generate a migration for the links table:

cargo run --bin migration

This will create a new migration file in the migrations directory.

Applying Migration

Apply the migration to create the links table:

cargo run --bin migration -- migrate

Starting the Project

  1. Install crates:

    cargo build
  2. Run the project:

    cargo run

The project will start, and you can interact with the API.

Endpoints

The backend provides two endpoints:

  • GET /links: Retrieve a list of shared links.

    Success Response:

    [
      {
        "id": 1,
        "link": "https://www.sk.com/about",
        "count": 58,
        "created_at": "2024-02-03T23:05:37Z",
        "updated_at": "2024-02-03T23:07:16Z"
      },
      {
        "id": 2,
        "link": "https://www.sk.com",
        "count": 18,
        "created_at": "2024-02-03T23:16:12Z",
        "updated_at": "2024-02-03T23:16:22Z"
      },
      {
        "id": 3,
        "link": "https://www.sk.com/newsletter",
        "count": 36,
        "created_at": "2024-02-03T23:16:35Z",
        "updated_at": "2024-02-03T23:55:07Z"
      }
    ]

    Error Response:

    {
      "error": "error that occurred"
    }
  • POST /links: Add a new link to the collection.

    Request Body Schema:

    {
      "link": "https://awesome-link.com"
    }

    Success Response:

    {
      "id": 123,
      "link": "https://example.com",
      "count": 1,
      "created_at": "2024-02-03T12:34:56Z",
      "updated_at": "2024-02-03T12:34:56Z"
    }

    Error Response:

    {
      "error": "Failed to share the link. Please try again."
    }

Project Structure

  • src/: Source code directory.

  • migrations/: Directory containing database migration files.

  • Database Schema SQL:

    CREATE TABLE links (
        id INT AUTO_INCREMENT PRIMARY KEY,
        link VARCHAR(255) NOT NULL,
        count INT DEFAULT 1 NOT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    );

    Adjust the schema according to your requirements.

rest-api-rust's People

Contributors

skartikey avatar

Stargazers

Samet B. avatar

Watchers

 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.