Skip to content

Instantly share code, notes, and snippets.

🕶
😃

petitviolet petitviolet

View GitHub Profile
@petitviolet
petitviolet / FileServer.go
Last active Jan 21, 2019
static file server written in golang.
View FileServer.go
package main
import (
"log"
"net/http"
)
func main() {
log.Println("Server started. 0.0.0.0:8080")
http.ListenAndServe(":8080", http.HandlerFunc(fileHandler))
@petitviolet
petitviolet / GenericDiff.scala
Created Nov 17, 2018
generic diff of case class instances
View GenericDiff.scala
import shapeless._
import shapeless.labelled.FieldType
object ClassDiff extends App {
sealed abstract class Field(name: String)
case class FieldSame(name: String) extends Field(name)
case class FieldDiff[A](name: String, before: A, after: A) extends Field(name)
trait GenericDiff[HL <: HList] {
@petitviolet
petitviolet / main.go
Last active Sep 10, 2018
benchmark for select from MySQL
View main.go
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/google/uuid"
"os"
"strings"
)
@petitviolet
petitviolet / SkinnyORMUpsert.scala
Last active Aug 2, 2018
helper class for execution "upsert" (update or insert) query using Skinny-ORM.
View SkinnyORMUpsert.scala
import skinny.orm.feature.NoIdCUDFeature
object Implicits {
implicit class Upserter[T](val orm: NoIdCUDFeature[T]) extends AnyVal {
def upsert(condition: SQLSyntax)(update: (SQLSyntax, Any)*)(insert: (SQLSyntax, Any)*)(implicit s: DBSession): Unit = {
orm.updateBy(condition).withNamedValues(update: _*) match {
case 1 => ()
case 0 =>
orm.createWithNamedValues(insert: _*)
()
View LoggerProvider.scala
import org.slf4j.LoggerFactory
trait LoggerProvider {
protected lazy val logger = new xLogger(this.getClass.getName)
}
private object xLogger {
def apply(name: String): xLogger = new xLogger(name)
}
class xLogger private (name: String) {
private lazy val logger = LoggerFactory.getLogger(name)
View skinny-orm-insert.scala
case class MyObj()
def insert(c: MyObj, excludes: String*)(implicit s: DBSession): Id[MyObj] = {
import scala.reflect.runtime.{ universe => ru }
val clazz = c.getClass
def caseMap[T: ru.TypeTag: reflect.ClassTag](instance: T): Seq[(SQLSyntax, Any)] = {
val im = ru.runtimeMirror(clazz.getClassLoader).reflect(instance)
ru.typeOf[T]
.members
@petitviolet
petitviolet / nginx_deployment.yaml
Created Mar 11, 2018
sample Nginx configuration on Kubernetes using ConfigMap to configure nginx.
View nginx_deployment.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-conf
data:
nginx.conf: |
user nginx;
worker_processes 3;
error_log /var/log/nginx/error.log;
events {
@petitviolet
petitviolet / StringFormatter.scala
Created Feb 7, 2018
String converter for camelCase <-> snake_case
View StringFormatter.scala
object StringFormatter {
private val spacesPattern = "[-\\s]".r
private val firstPattern = "([A-Z]+)([A-Z][a-z])".r
private val secondPattern = "([a-z\\d])([A-Z])".r
private val replacementPattern = "$1_$2"
implicit class stringFormatter(val word: String) extends AnyVal {
def snakenize: String = {
spacesPattern
.replaceAllIn(secondPattern.replaceAllIn(
@petitviolet
petitviolet / defer.scala
Created Feb 7, 2018
Scala `defer` like Golang
View defer.scala
class Deferred() {
private var fs: Seq[() => Any] = Nil
def apply(f: => Any): Unit = {
fs = { () =>
f
} +: fs
}
def runDeferred(): Unit = fs.reverse foreach { _.apply }
@petitviolet
petitviolet / TaskPrac.scala
Last active Nov 10, 2017
Run scalaz Task in sequencial and parallel
View TaskPrac.scala
import scalaz._
import scalaz.concurrent.Task
object TaskPrac extends App {
private def task(i: Int) = Task {
println(s"sleeping: $i")
Thread.sleep(i * 100)
println(s"wakeup: $i")
i
}
You can’t perform that action at this time.