sneakycrow

dev

This week I started writing a file sharing software for the web. I wanted to start creating Entry posts, to track progress and issues I’m having.

note: I’m going to post some code below, but it’s not all the steps required to get up and running

I’m building it in Rust using the actix framework with diesel for interacting with the DB on the backend.

And on the frontend I’m just planning on using something fast. Probably Vue.js with Bulma CSS.

I started off by initializing the project with Rust’s cargo, and then bringing in the packages I knew I needed:

  • serde
  • actix-web
  • diesel
  • dotenv

Next, I started writing the main.rs file. I need to get the API running, so I initialized a “server” via the actix-web readme.

extern crate actix_web;
use actix_web::{server, App, HttpRequest, Responder};

fn greet(req: &HttpRequest) -> impl Responder {
    let to = req.match_info().get("name").unwrap_or("World");
    format!("Hello {}!", to)
}

fn main() {
    server::new(|| {
        App::new()
            .resource("/", |r| r.f(greet))
            .resource("/{name}", |r| r.f(greet))
    })
    .bind("127.0.0.1:8000")
    .expect("Can not bind to port 8000")
    .run()
}

Now that that was running, I know I needed a database. I’m preferential to PostgreSQL, so I created a docker compose file to get that running.

version: '3.1'

services:
  postgres:
    image: postgres:latest
    restart: always
    ports:
      - 5432:5432
    environment:
      POSTGRES_PASSWORD: postgres

note: I opted to use a compose file because I’m more familiar with them and I imagine I might add another service down the line

Then, I started structuring the data for the SQL table. The files aren’t actually going to be stored in the DB. I’m probably going to use Digital Ocean’s spaces for that. I do want the SQL rows to have some data for displaying on the frontend. Spaces can handle the actual downloads.

CREATE TABLE file_links (
  id UUID PRIMARY KEY,
  published BOOLEAN NOT NULL DEFAULT FALSE,
  storage_location TEXT NOT NULL,
  title TEXT,
  description TEXT,
  downloads INTEGER DEFAULT 0
)

And that’s as far as I got for now. My next steps that I want to take are these:

  • Create an endpoint for requesting all file_links from the DB
  • Integrate a SQL Query into that endpoint
  • Create Data for testing the endpoint

#dev