Skip to content

Instantly share code, notes, and snippets.

View louis030195's full-sized avatar

Louis Beaumont louis030195

View GitHub Profile
#![allow(proc_macro_derive_resolution_fallback)]
pub mod handler;
pub mod repository;
use mongodb::bson;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Cat {
#[serde(rename = "_id")] // Use MongoDB's special primary key field name when serializing
pub id: Option<bson::oid::ObjectId>,
#![allow(proc_macro_derive_resolution_fallback)]
use crate::cats::{Cat, InsertableCat};
use crate::mongo_connection::Conn;
use crate::r2d2_mongodb::mongodb::db::ThreadedDatabase;
use mongodb::{bson, coll::results::DeleteResult, doc, error::Error, oid::ObjectId};
const COLLECTION: &str = "cats";
pub fn all(connection: &Conn) -> Result<Vec<Cat>, Error> {
let cursor = connection.collection(COLLECTION).find(None, None).unwrap();
use crate::cats;
use crate::mongo_connection::Conn;
use cats::Cat;
use mongodb::{ doc, error::Error, oid::ObjectId};
use rocket_contrib::json::Json;
use rocket::{http::Status};
fn error_status(error: Error) -> Status {
match error {
Error::CursorNotFoundError => Status::NotFound,
use dotenv::dotenv;
use r2d2::PooledConnection;
use r2d2_mongodb::{ConnectionOptions, MongodbConnectionManager};
use rocket::http::Status;
use rocket::request::{self, FromRequest};
use rocket::{Outcome, Request, State};
use std::env;
use std::ops::Deref;
type Pool = r2d2::Pool<MongodbConnectionManager>;
#![feature(decl_macro, proc_macro_hygiene)]
#[macro_use]
extern crate rocket;
extern crate dotenv;
extern crate mongodb;
extern crate r2d2;
extern crate r2d2_mongodb;
extern crate rocket_contrib;
#[macro_use]
extern crate serde_derive;
// See https://rocket.rs/v0.4/guide/testing/#local-dispatching
#[cfg(test)]
mod test {
use mongodb::{doc, error::Error, oid::ObjectId};
use rocket::http::{ContentType, Status};
use rocket::local::Client;
use rocket_contrib::json::Json;
use rustlang_rocket_mongodb::cats::Cat;
use rustlang_rocket_mongodb::rocket;
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;
#[get("/")]
fn index() -> &'static str {
let coll = client.db("media").collection("movies");
coll.insert_one(doc!{ "title": "Back to the Future" }, None).unwrap();
coll.update_one(doc!{}, doc!{ "director": "Robert Zemeckis" }, None).unwrap();
coll.delete_many(doc!{}, None).unwrap();
{
"name": "myList",
"type": [
"array",
{
"count": ";",
"type": [
"container",
[
{
@louis030195
louis030195 / json_to_yaml.sh
Last active September 30, 2020 14:53
JSON to YAML, one liner bash
#!/bin/bash
python3 -m venv .my_super_env && # Yeah random name to avoid erasing local virtualenv
source .my_super_env/bin/activate &&
pip install -q pyyaml &&
python3 -c "import yaml, json, sys
sys.stdout.write(yaml.dump(json.load(sys.stdin)))" \
< $1 > $2 || true
# Remove the temporary venv in any case
rm -rf .my_super_env || true
@louis030195
louis030195 / wait_for_open_port_inside_docker.sh
Created April 14, 2020 12:40
Bash script to wait for an open port inside Docker container
CONTAINER_NAME=my-awesome-container
PORT=1337
while [ ! "$(docker exec -t -i $CONTAINER_NAME curl --write-out %{http_code} --silent --output /dev/null localhost:$PORT)" = "-ne 200" ] ; do
echo "Waiting for port $PORT to be opened ..."
sleep 20
done