Skip to content

Instantly share code, notes, and snippets.

View soyart's full-sized avatar

Prem Phansuriyanon soyart

View GitHub Profile
@soyart
soyart / mux.go
Created September 17, 2021 18:13
Simple HTTP server in Go with gorilla/mux
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/google/uuid"
"github.com/gorilla/mux"
)
@soyart
soyart / bubbleSort.js
Created September 19, 2021 17:25
Bubble sort in JS
const array = [2, 100, 4, 5, 7, 21, 13];
const bubbleSortInPlace0 = (arr) => {
for (let i = 0; i < arr.length - 1; i++) {
for (let j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j+1]) {
[arr[j], arr[j+1]] = [arr[j+1], arr[j]]
};
};
};
@soyart
soyart / express.js
Created September 19, 2021 17:48
Simple Express HTTP server
const express = require('express');
const app = express();
const router = express.Router();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
const sendHi = async (req, res) => {
res.status(200).send("Hi").end();
};
@soyart
soyart / redis.js
Last active October 1, 2021 19:32
Redis cache with Express
const express = require('express');
const axios = require('axios');
const redis = require('redis');
const cors = require('cors');
const PORT = 8000;
const DEFAULT_EXPIRATION = 3600;
const API_URL = "https://jsonplaceholder.typicode.com/photos"
const app = express();
@soyart
soyart / rest.go
Last active October 9, 2021 14:18
Basic REST API server in Go using gorilla/mux
package main
import (
"bytes"
"encoding/json"
"log"
"net/http"
"github.com/google/uuid"
"github.com/gorilla/mux"
@soyart
soyart / rate_limit.go
Last active January 20, 2022 14:12
Check rate limit in Go
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
@soyart
soyart / go_priority_queue.go
Created February 15, 2022 07:37
Go priority queue with uint256 priority
package main
import (
"container/heap"
"fmt"
"github.com/holiman/uint256"
)
// An Item is something we manage in a priority queue.
@soyart
soyart / graceful_shutdown.go
Created April 2, 2022 11:18
Graceful shutdown in Go using context.Context and chan os.Signal
package main
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"sync"
"syscall"
#![allow(dead_code)]
use std::fmt;
// Tuple struct
struct RGBColor(u8, u8, u8);
// Manually implementing std::fmt::Debug
impl fmt::Debug for RGBColor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RGBColor") // Must be exactly "RGBColor" in this case
#![allow(dead_code)]
use std::fmt;
enum OrderStatus {
Null,
Created,
Cancelled,
Successful,
}