Skip to content

Instantly share code, notes, and snippets.

View jkyamog's full-sized avatar

Jun Yamog jkyamog

View GitHub Profile
.greenid-so-content #greenid-container .btn-primary {
background-color: #0000FF;
color: white;
text-transform: lowercase;
padding: 15px;
width: 100%;
font-size: 17px;
border: none;
-webkit-border-radius: 0;
-moz-border-radius: 0;
(def.factory app.Product [$resource]
($resource "/products/:id" (obj :id "@id")))
(def.controller app.ProductController [$scope Product]
(! $scope.products (. Product query))
(! $scope.updateProduct (fn [product]
(. product $save)))
(! $scope.deleteProduct (fn [product]
(. product $remove
(fn []
(defroutes product-routes
(context "/products" []
(GET "/" [] {:body (get-products)})
(GET "/index.html" [] (layout/render "product.html"))
(POST "/" {product :body-params} {:body (create-product product)})
(context "/:id" [id]
(GET "/" [] {:body (retrieve-product id)})
(POST "/" {product :body-params} {:body (update-product id product)})
(DELETE "/" [] (delete-product id)))))
(defentity products)
(defn get-products []
(select products))
(defn create-product [product]
(insert products (values product)))
(defn retrieve-product [id]
(select products
@jkyamog
jkyamog / gist:9924325
Created April 1, 2014 22:21
Auditing on play
object AuditLogger {
implicit def actionToSimpleFuture[T](action: Action[T])(implicit request: Request[T]): Future[SimpleResult] = {
action.apply(request)
}
def audit[M](audit: Audit)(f: => Future[M])(implicit ctx: ExecutionContext): Future[M] = {
val future = f // do the call by name only once, otherwise another future is created
future.onComplete {
case Success(_) => insert(audit)
case Failure(_) => // don't care if future has failed, just let is bubble up
@jkyamog
jkyamog / gist:9756527
Created March 25, 2014 07:09
getting angular.js resource to work with purnam/gyr
(def.module app [ngResource])
(def.service app.productFactory [$resource]
($resource "/products"))
(def.controller app.productController [$scope productFactory]
(! $scope.products (. productFactory query)))
@jkyamog
jkyamog / gist:9616210
Created March 18, 2014 08:59
handling 414
event.getCause match {
// IO exceptions happen all the time, it usually just means that the client has closed the connection before fully
// sending/receiving the response.
case e: IOException => nettyExceptionLogger.trace("Benign IO exception caught in Netty", e)
event.getChannel.close()
case e: TooLongFrameException => nettyExceptionLogger.warn("Handling TooLongFrameException", e)
val response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.REQUEST_URI_TOO_LONG)
response.setHeader("connection", "close")
val dme = new DownstreamMessageEvent(ctx.getChannel, Channels.future(ctx.getChannel), response, ctx.getChannel.getRemoteAddress)
ctx.sendDownstream(dme);
case class TestRequestHeader(headers: Headers, method: String = "GET", uri: String = "/", path: String = "", remoteAddress: String = "127.0.0.1", version: String = "HTTP/1.1", id: Long = 666, tags: Map[String, String] = Map.empty[String, String], queryString: Map[String, Seq[String]] = Map(), secure: Boolean = false) extends RequestHeader
val rh = TestRequestHeader(headers = new Headers {
val data = Seq(play.api.http.HeaderNames.CONTENT_TYPE -> Seq("""multipart/form-data; boundary=----68766594719920667221395339924"""), play.api.http.HeaderNames.CONTENT_LENGTH -> Seq("380"))
})
val multipartFormData = BodyParsers.parse.multipartFormData
val testStr = """Content-Type: multipart/form-data; boundary=---------------------------68766594719920667221395339924
Content-Length: 380
def uploadFile(docId: String) = {
import scalax.io._
val output = Resource.fromFile("target/test-upload.txt")
output.write("hello world")(Codec.UTF8)
val testFile = new File("target/test-upload.txt")
val file = TemporaryFile(testFile)
val data = new MultipartFormData(Map(),
@jkyamog
jkyamog / gist:6999185
Created October 15, 2013 21:47
Batch updates on reactive mongo sometimes fails.
val batchSize = 100 // for some reason we need to throttle down the import, putting everything in sometimes causes a future already completed
Async {
fetch("/instrument").map { instruments =>
Logger.debug(s"importing ${instruments.size} instruments")
instruments.grouped(batchSize).foreach { batch =>
res.batchInsert(Enumerator.enumerate(batch))
}