~ the web app is splitted in two objects: one for configuration, one for creation
authorAlexandre Bertails <bertails@w3.org>
Wed, 24 Aug 2011 14:14:32 -0400
changeset 59 27e9c73988a4
parent 58 fb3a6b1085f1
child 60 66c931fabb4f
~ the web app is splitted in two objects: one for configuration, one for creation
src/main/scala/Main.scala
--- a/src/main/scala/Main.scala	Wed Aug 24 14:13:01 2011 -0400
+++ b/src/main/scala/Main.scala	Wed Aug 24 14:14:32 2011 -0400
@@ -18,8 +18,8 @@
 import java.net.{URL, URLDecoder}
 import org.slf4j.{Logger, LoggerFactory}
 
-object VirtualTrainerApp {
-
+object ResourceManager {
+  
   val logger:Logger = LoggerFactory.getLogger(this.getClass)
 
   val clazz:Class[_] = this.getClass
@@ -69,55 +69,74 @@
     dir
   }
   
+}
+
+import ResourceManager.fromClasspath
+
+object VirtualTrainerApp {
+  
+  val logger:Logger = LoggerFactory.getLogger(this.getClass)
+
+  val echo = unfiltered.filter.Planify {
+    case Path(Seg(p :: Nil)) => ResponseString(p)
+  }
+
+  val engine = {
+      val templates = fromClasspath("templates/")
+      logger.debug("Template directory extracted at " + templates.getAbsolutePath)
+      val templateDirs = List(templates)
+      val scalateMode = "production"
+      new TemplateEngine(templateDirs, scalateMode)
+  }
+
+  val tracker = unfiltered.filter.Planify {
+    case req @ Path(Seg("tracker" :: Nil)) =>
+      Ok ~> ContentType("application/xhtml+xml") ~> Scalate(req, "geolocation.ssp")(engine)
+  }
+  
+  val recordAPI = unfiltered.filter.Planify {
+    case req @ POST(Path(Seg("user" :: user :: "run" :: Nil))) & Host(host) => {
+      val json:MongoDBObject = com.mongodb.util.JSON.parse(Body.string(req)).asInstanceOf[com.mongodb.DBObject]
+      val record = grater[Record].asObject(json)
+      RecordDAO.insert(record) match {
+        case Some(id) => {
+          val path = "%s://%s/user/%s/run/%s" format (req.underlying.getScheme, host, user, id.toString)
+          Created ~> Location(path) ~> ResponseString("object available at %s" format path)
+        }
+        case None => {
+          InternalServerError ~> ResponseString("couldn't create object")
+        }
+      }
+    }
+    case req @ Path(Seg("user" :: user :: "run" :: id :: Nil)) => req match {
+      case GET(_) => {
+        RecordDAO.findOneByID(id) match {
+          case Some(record) => Ok ~> ContentType("application/json") ~> ResponseString(record.asJSON)
+          case None => NotFound
+        }
+      }
+      case _ => MethodNotAllowed ~> Allow("GET") ~> ResponseString("MethodNotAllowed...")
+    }
+  }
+
+}
+
+object VirtualTrainerAppMain {
+
+  val logger:Logger = LoggerFactory.getLogger(this.getClass)
 
   /**
    * 
    */
   def main(args: Array[String]) {
     
+    val logger:Logger = LoggerFactory.getLogger(this.getClass)
+
     val port = args.toList.headOption map { _.toInt } getOrElse 2719
     
-    val echo = unfiltered.filter.Planify {
-      case Path(Seg(p :: Nil)) => ResponseString(p)
-    }
-    
-    val engine = {
-      val templates = fromClasspath("templates/")
-      logger.debug("Template directory extracted at " + templates.getAbsolutePath)
-      val templateDirs = List(templates)
-      val scalateMode = "production"
-      new TemplateEngine(templateDirs, scalateMode)
-    }
-
-    val app = unfiltered.filter.Planify {
-       case req @ Path(Seg("tracker" :: Nil)) =>
-         Ok ~> ContentType("application/xhtml+xml") ~> Scalate(req, "geolocation.ssp")(engine)
-       case req @ POST(Path(Seg("user" :: user :: "run" :: Nil))) & Host(host) => {
-         val json:MongoDBObject = com.mongodb.util.JSON.parse(Body.string(req)).asInstanceOf[com.mongodb.DBObject]
-         val record = grater[Record].asObject(json)
-         RecordDAO.insert(record) match {
-           case Some(id) => {
-             val path = "%s://%s/user/%s/run/%s" format (req.underlying.getScheme, host, user, id.toString)
-             Created ~> Location(path) ~> ResponseString("object available at %s" format path)
-           }
-           case None => {
-             InternalServerError ~> ResponseString("couldn't create object")
-           }
-         }
-       }
-       case req @ Path(Seg("user" :: user :: "run" :: id :: Nil)) => req match {
-         case GET(_) => {
-           RecordDAO.findOneByID(id) match {
-             case Some(record) => Ok ~> ContentType("application/json") ~> ResponseString(record.asJSON)
-             case None => NotFound
-           }
-         }
-         case _ => MethodNotAllowed ~> Allow("GET") ~> ResponseString("MethodNotAllowed...")
-       }
-    }
-
     import javax.servlet._
     import javax.servlet.http._
+    import VirtualTrainerApp._
     
     unfiltered.jetty.Http(port).filter {
       new Filter {
@@ -135,7 +154,7 @@
       ctx.resources(fromClasspath("scripts/").toURI.toURL)
     }.context("/styles"){ ctx:ContextBuilder =>
       ctx.resources(fromClasspath("styles/").toURI.toURL)
-    }.filter(app).filter(echo).run()
+    }.filter(tracker).filter(recordAPI).filter(echo).run()
 
     
   }