Commit c145d76df958a7294eb02750b8b84aa8db55c03a

Authored by Arnold ago
1 parent 0a0ffb1f85
Exists in master

add initial version reply

Showing 6 changed files with 112 additions and 0 deletions Side-by-side Diff

  1 +organization := "com.piki"
  2 +
  3 +version := "0.2"
  4 +
  5 +scalaVersion := "2.11.6"
  6 +
  7 +scalacOptions := Seq("-unchecked", "-deprecation", "-encoding", "utf8")
  8 +
  9 +libraryDependencies ++= {
  10 + val akkaV = "2.3.9"
  11 + val sprayV = "1.3.3"
  12 + Seq(
  13 + "io.spray" %% "spray-can" % sprayV,
  14 + "io.spray" %% "spray-routing" % sprayV,
  15 + "io.spray" %% "spray-client" % sprayV,
  16 + "io.spray" %% "spray-testkit" % sprayV % "test",
  17 + "com.typesafe.akka" %% "akka-actor" % akkaV,
  18 + "com.typesafe.akka" %% "akka-testkit" % akkaV % "test",
  19 + "org.specs2" %% "specs2-core" % "2.3.11" % "test"
  20 + )
  21 +}
  22 +
  23 +Revolver.settings
project/build.properties View file @ c145d76
  1 +sbt.version=0.13.8
project/plugins.sbt View file @ c145d76
  1 +addSbtPlugin("io.spray" % "sbt-revolver" % "0.7.2")
  2 +addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.13.0")
src/main/resources/application.conf View file @ c145d76
  1 +akka {
  2 + loglevel = INFO
  3 +}
  4 +
  5 +spray.can.server {
  6 + request-timeout = 1s
  7 +}
src/main/scala/com/piki/push/Boot.scala View file @ c145d76
  1 +package com.piki.push
  2 +
  3 +import akka.actor.{ActorSystem, Props}
  4 +import akka.io.IO
  5 +import spray.can.Http
  6 +
  7 +/**
  8 + * Created by arnold on 2015. 4. 7..
  9 + */
  10 +object Boot extends App {
  11 +
  12 + implicit val system = ActorSystem("on-spray-can")
  13 +
  14 + val handler = system.actorOf(
  15 + Props[PushReplyServiceActor], "push-reply-service"
  16 + )
  17 +
  18 + IO(Http) ! Http.Bind(handler, interface = "192.168.20.41", port =8080)
  19 +}
src/main/scala/com/piki/push/PushReplyServiceActor.scala View file @ c145d76
  1 +package com.piki.push
  2 +
  3 +import akka.actor.{ActorSystem, ActorLogging, Actor}
  4 +import akka.io.IO
  5 +import akka.pattern.ask
  6 +import akka.util.Timeout
  7 +
  8 +import spray.can.Http
  9 +import spray.http._
  10 +import spray.http.HttpMethods._
  11 +import spray.httpx.RequestBuilding
  12 +import spray.httpx.unmarshalling.Deserialized
  13 +import spray.httpx.unmarshalling.Unmarshaller.unmarshal
  14 +
  15 +import scala.concurrent.Future
  16 +import scala.concurrent.duration._
  17 +
  18 +
  19 +object DwlogBuilder {
  20 + def get_open_log(uid:String, nid:String, time:Long=System.currentTimeMillis()):String = {
  21 + s"""PUSH|OPN|$time|$uid||{"nid":"$nid"}"""
  22 + }
  23 +}
  24 +
  25 +class PushReplyServiceActor extends Actor with ActorLogging with RequestBuilding {
  26 + def actorRefFactory = context
  27 +
  28 + def receive = {
  29 + case http: Http.Connected => sender ! Http.Register(self)
  30 +
  31 + case r@HttpRequest(PUT, Uri.Path("/push/replied"), headers, entity: HttpEntity.NonEmpty, _) => {
  32 + try {
  33 + val formData: Deserialized[FormData] = unmarshal[FormData](r.entity)
  34 + val formDataMap = formData.right.get.fields.map(x => (x._1, x._2)).toMap
  35 +
  36 + implicit val timeout: Timeout = Timeout(15.seconds)
  37 + implicit val system: ActorSystem = actorRefFactory.system
  38 + import system.dispatcher
  39 +
  40 +
  41 + val log: String = DwlogBuilder.get_open_log(
  42 + formDataMap.apply("uid"), formDataMap.apply("nid")
  43 + )
  44 +
  45 + val response: Future[HttpResponse] =
  46 + (IO(Http) ? Put("http://58.229.156.80:8081/log?dev=push", log))
  47 + .mapTo[HttpResponse]
  48 +
  49 + } catch {
  50 + case e: Exception => { log.error(e, "error")}
  51 + }
  52 +
  53 + sender ! HttpResponse(
  54 + status = 200
  55 + )
  56 + }
  57 +
  58 + case _: HttpRequest => sender ! HttpResponse(status = 200)
  59 + }
  60 +}