Skip to content

Instantly share code, notes, and snippets.

View tungd's full-sized avatar
🐳
Back to normal

Tung Dao tungd

🐳
Back to normal
View GitHub Profile
define PROXY_FN
export async function onRequest(context) {
const { request, params, env } = context;
const url = new URL(request.url);
url.host = env.API_HOST;
url.pathname = params.path.join('') === 'json' ? `/api/json` : params.path.join('/');
if (request.method === 'POST' && !request.headers.get('Content-Type')) {
const headers = Object.fromEntries(request.headers.entries())
return fetch(url, {
method: request.method,
@tungd
tungd / build.gradle.kts
Last active February 12, 2019 06:32
Configuration file for microservice using Gradle, Spring Boot, Spring Cloud, Spring Webflux, Srping Data JPA, OpenJPA (for entity class generation)
group = "com.tungdao.sample"
extra["spring.version"] = "5.1.3.RELEASE"
extra["spring.boot.version"] = "2.1.1.RELEASE"
extra["spring.cloud.version"] = "Greenwich.RC2"
plugins {
java
idea
id("org.springframework.boot") version "2.1.1.RELEASE"
@tungd
tungd / index.js
Last active November 1, 2018 07:59
Sequelize insert demo
const Sequelize = require('sequelize')
const sequelize = new Sequelize({
dialect: 'sqlite',
storage: 'db.sqlite3'
})
const Product = sequelize.define('product', {
name: Sequelize.STRING
})
@tungd
tungd / index.js
Last active October 11, 2018 10:43
const xlsx = require('xlsx')
const table = xlsx.readFile('sample.xlsx', { cellStyles: true })
const sheet = table.Sheets[table.SheetNames[0]];
console.log(sheet)
// sheet['C10'] = { t: 's', v: 'Test update value' }
const addCell = (sheet, cell, value) => {
sheet[cell] = Object.assign({}, sheet[cell], value)
@tungd
tungd / answers.py
Last active September 24, 2018 04:01
from functools import reduce
# count
reduce(lambda i, _: i + 1, [1, 2, 3, 4, 5, 6, 2, 3], 0)
# max
reduce(lambda a, b: a if a > b else b, [1, 2, 3, 4, 5, 6, 2, 3])
# min
reduce(lambda a, b: a if a < b else b, [1, 2, 3, 4, 5, 6, 2, 3])
@tungd
tungd / RaiseFund.sol
Last active August 1, 2018 06:19
Example fund raising contract
contract RaiseFund {
address public owner;
uint public target;
uint public total;
bool public ended;
struct Donation {
@tungd
tungd / Example.hs
Created July 24, 2018 09:14
Example using database pool with `ReaderT`
#!/usr/bin/env stack
-- stack --resolver lts-12.0 --install-ghc runghc --package mtl --package resource-pool --package mysql-simple
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleContexts #-}
import Control.Monad.Reader
import Data.Pool
import Database.MySQL.Simple
@tungd
tungd / RactiveComponentPlugin.js
Created July 14, 2018 09:50
FuseBox RactiveComponentPlugin (WIP)
const Ractive = require('ractive')
const rcu = require('rcu')
const typescript = require('typescript')
rcu.init(Ractive)
class RactivePlugin {
constructor(options) {
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Servant.Selda where
import Data.Pool
import Database.Selda
import Database.Selda.Backend
import Database.Selda.PostgreSQL (pgOpen')
import RIO
import Servant
@tungd
tungd / parser.py
Created December 5, 2017 04:51
Parser combinator to skip error.
from parsy import Parser, Result
class Text(object):
'''Structure to contain all the parts that the parser does not understand.
A better name would be Whitespace
'''
def __init__(self, text=''):
self.text = text