Skip to content

Instantly share code, notes, and snippets.

@benjaminjackman
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjaminjackman/04222754ba41512aa794 to your computer and use it in GitHub Desktop.
Save benjaminjackman/04222754ba41512aa794 to your computer and use it in GitHub Desktop.
//This is my general purpose client for the Javascript side
class AutowireClientAjax(val url: String)(implicit val ec: ExecutionContext = JSExecutionContext.queue)
extends SerAutowireClient {
override def doCall(req: Request): Future[Any] = {
val r = AutowireHelp.ClientSideReq.fromAutowire(req)
Ajax.post(
url = url,
data = r.toJsonCompact()
).map(_.responseText)
}
}
trait RestApi {
val stopWordsDB: StopWordsDB
val corpusDB: CorpusDB
val rawWordsDB: WordsDB
val filteredWordsDB: WordsDB
val rawStatsDB: CorpusStatsDB
val filteredStatsDB: CorpusStatsDB
}
class RestApiImpl(...) extends RestApi {
... implement trait methods in here
}
object AutowireServer extends autowire.Server[String, upickle.Reader, upickle.Writer] {
def write[Result: Writer](r: Result) = upickle.write(r)
def read[Result: Reader](p: String) = upickle.read[Result](p)
}
val apiImpl = new RestApiImpl(...)
//This requires a version of autowire that isn't or may not be released, you can experiment with it
//with this "biz.cgta" %% "autowire" % "0.2.7-M3"
val router = AutowireServer.innerRoute[RestApi](apiImpl)
post {
path("apis") { xs =>
extract(_.request.entity.asString) { body =>
respondWithHeaders(
RawHeader("Access-Control-Allow-Origin", "*"),
RawHeader("Access-Control-Allow-Headers", "Content-Type")) {
val r = (DESERIALIZE body TO autowire.Core.Request[???] WITH WHATEVER)
complete {
router(r)
}
}
}
} ~ path(Rest) { xs =>
complete {
PRINT | "ERROR"
s"BAD PATH $xs"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment