Skip to content

Instantly share code, notes, and snippets.

Avatar

Collins Muriuki collinsmuriuki

View GitHub Profile
@collinsmuriuki
collinsmuriuki / httparse.rs
Created Nov 10, 2020
Parse http request with httparse
View httparse.rs
use std::net::tcplistener;
use std::io::read;
use httparse;
fn main() {
let listener = tcplistener::bind("127.0.0.1:9000").unwrap();
println!("server running on http://localhost:9000");
loop {
@collinsmuriuki
collinsmuriuki / package.json
Last active Nov 13, 2020
Handy dandy TypeORM cli scripts that work for a Nest.js project with an ormconfig.ts module
View package.json
{
"scripts": {
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config src/ormconfig.ts",
"typeorm:generate": "npm run typeorm migration:generate -- --name",
"typeorm:create": "npm run typeorm migration:create -- --name",
"typeorm:revert": "npm run typeorm migration:revert",
"typeorm:run": "npm run typeorm migration:run"
}
}
View curl_gql.md

Curling a graphql endpoint

curl \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{"query": "{allCountriesStats {country}}"}' \
  https://coronagraphql.herokuapp.com/ | python -m json.tool
View purge_node_modules
# Linux/ Mac
cd documents
find . -name "node_modules" -type d -prune -print | xargs du -chs
# Windows
cd documents
FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" echo %d"
View keybase.md

Keybase proof

I hereby claim:

  • I am collinsmuriuki on github.
  • I am collinsmuriuki (https://keybase.io/collinsmuriuki) on keybase.
  • I have a public key ASBG7gHHWMBMvXmmjOhXqose9Q9ON5Sspv9f67-57fp8VAo

To claim this, I am signing this object:

@collinsmuriuki
collinsmuriuki / state_pattern.rs
Last active Sep 19, 2020
An implementation of the State pattern in Rust
View state_pattern.rs
pub struct Post {
state: Option<Box<dyn State>>,
content: String,
}
impl Post {
pub fn new() -> Post {
Post {
state: Some(Box::new(Draft {})),
content: String::new(),
@collinsmuriuki
collinsmuriuki / README.md
Last active Sep 11, 2020
How to check nodejs memory limit
View README.md

Overwriting default memory allocation

Set --max-old-space-size to the value in megabytes of how large you would like the heap size:

node --max-old-space-size=4096 v8.js
@collinsmuriuki
collinsmuriuki / mock_gui.rs
Created Sep 11, 2020
A mock GUI implementation to demonstrate Trait Objects
View mock_gui.rs
pub trait Draw {
fn draw(&self);
}
pub struct Screen {
pub components: Vec<Box<dyn Draw>>,
}
impl Screen {
pub fn run(&self) {
@collinsmuriuki
collinsmuriuki / mutex_and_arc.rs
Last active Sep 1, 2020
Mutex and Arc: How Mutex, a smart pointer is used to allow access to data from one thread at a time and introducing multiple ownership rules of Rc<T> in multithreaded scenarios using a similar smart pointer Arc<T>>
View mutex_and_arc.rs
use std::sync::{Mutex,Arc}; // mutual exclusion
use std::thread;
use std::rc::Rc;
// Mutex in single threaded scenarios
pub fn start_mutex() {
let m = Mutex::new(String::new());
{
let mut num = m.lock().unwrap();
@collinsmuriuki
collinsmuriuki / threads.rs
Last active Aug 28, 2020
Threads and channels
View threads.rs
use std::thread;
use std::time::Duration;
use std::sync::mpsc; // multiple producer, single consumer
pub fn threads1() {
thread::spawn(|| {
for i in 1..10 {
println!("hi number {} from the spawned thread!", i);
thread::sleep(Duration::from_millis(1));
}
You can’t perform that action at this time.