Skip to content

Commit

Permalink
0.2.8
Browse files Browse the repository at this point in the history
  • Loading branch information
lihaoyi committed Sep 16, 2019
1 parent 84ea971 commit b83eec0
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 15 deletions.
3 changes: 3 additions & 0 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import $file.example.twirl.build
import $file.example.variableRoutes.build
import $file.example.websockets.build
import $file.example.websockets2.build
import $file.example.websockets3.build

trait CaskModule extends ScalaModule with PublishModule{
def scalaVersion = "2.13.0"
Expand Down Expand Up @@ -115,6 +116,7 @@ object example extends Module{
object variableRoutes extends $file.example.variableRoutes.build.AppModule with LocalModule
object websockets extends $file.example.websockets.build.AppModule with LocalModule
object websockets2 extends $file.example.websockets2.build.AppModule with LocalModule
object websockets3 extends $file.example.websockets2.build.AppModule with LocalModule
}

def publishVersion = T.input($file.ci.version.publishVersion)
Expand Down Expand Up @@ -158,6 +160,7 @@ def uploadToGithub(authKey: String) = T.command{
$file.example.variableRoutes.build.millSourcePath,
$file.example.websockets.build.millSourcePath,
$file.example.websockets2.build.millSourcePath,
$file.example.websockets3.build.millSourcePath,
)
for(example <- examples){
val f = T.ctx().dest
Expand Down
2 changes: 1 addition & 1 deletion cask/src/cask/main/Routes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ trait Routes{
metadata0 = routes
}

def log: cask.util.Logger
implicit def log: cask.util.Logger
}
1 change: 0 additions & 1 deletion cask/src/cask/router/Decorators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cask.router

import cask.internal.Conversion
import cask.model.{Request, Response}
import cask.router.{ArgReader, EntryPoint, Result}

/**
* A [[Decorator]] allows you to annotate a function to wrap it, via
Expand Down
2 changes: 0 additions & 2 deletions cask/util/src/cask/util/BatchActor.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package cask.util

import cask.util.Logger

import scala.collection.mutable
import scala.concurrent.ExecutionContext

Expand Down
4 changes: 2 additions & 2 deletions docs/pages/1 - Cask: a Scala HTTP micro-framework.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ via the following coordinates:
```scala
// Mill
ivy"com.lihaoyi::cask:0.2.7"
ivy"com.lihaoyi::cask:0.2.8"
// SBT
"com.lihaoyi" %% "cask" % "0.2.7"
"com.lihaoyi" %% "cask" % "0.2.8"
```
The `./mill` command is just a wrapper around the
Expand Down
4 changes: 1 addition & 3 deletions example/compress2/app/src/Compress2.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package app

import cask.util.Logger

case class Compress2()(implicit val log: Logger) extends cask.Routes{
case class Compress2()(implicit val log: cask.Logger) extends cask.Routes{
override def decorators = Seq(new cask.decorators.compress())

@cask.get("/")
Expand Down
4 changes: 1 addition & 3 deletions example/compress3/app/src/Compress3.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package app

import cask.util.Logger

case class Compress3()(implicit val log: Logger) extends cask.Routes{
case class Compress3()(implicit val log: cask.Logger) extends cask.Routes{

@cask.get("/")
def hello() = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package app

import cask.util.Logger

case class MinimalRoutes()(implicit val log: Logger) extends cask.Routes{
case class MinimalRoutes()(implicit val log: cask.Logger) extends cask.Routes{
@cask.get("/")
def hello() = {
"Hello World!"
Expand Down
21 changes: 21 additions & 0 deletions example/websockets3/app/src/Websockets3.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package app

case class Websockets3()(implicit val log: cask.Logger) extends cask.Routes{
@cask.websocket("/connect/:userName")
def showUserProfile(userName: String): cask.WebsocketResult = {
if (userName != "haoyi") cask.Response("", statusCode = 403)
else cask.WsHandler { channel =>
cask.WsActor {
case cask.WsActor.Text("") => channel.send(cask.WsActor.Close())
case cask.WsActor.Text(data) =>
channel.send(cask.WsActor.Text(userName + " " + data))
}
}
}

initialize()
}

object Websockets3Main extends cask.Main{
val allRoutes = Seq(Websockets3())
}
75 changes: 75 additions & 0 deletions example/websockets3/app/test/src/ExampleTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package app

import java.util.concurrent.atomic.AtomicInteger

import org.asynchttpclient.ws.{WebSocket, WebSocketListener, WebSocketUpgradeHandler}
import utest._

object ExampleTests extends TestSuite{


def withServer[T](example: cask.main.Main)(f: String => T): T = {
val server = io.undertow.Undertow.builder
.addHttpListener(8080, "localhost")
.setHandler(example.defaultHandler)
.build
server.start()
val res =
try f("http://localhost:8080")
finally server.stop()
res
}

val tests = Tests{
test("Websockets") - withServer(Websockets3Main){ host =>
@volatile var out = List.empty[String]
val client = org.asynchttpclient.Dsl.asyncHttpClient();
try{

// 4. open websocket
val ws: WebSocket = client.prepareGet("ws://localhost:8080/connect/haoyi")
.execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(
new WebSocketListener() {

override def onTextFrame(payload: String, finalFragment: Boolean, rsv: Int) {
out = payload :: out
}

def onOpen(websocket: WebSocket) = ()

def onClose(websocket: WebSocket, code: Int, reason: String) = ()

def onError(t: Throwable) = ()
}).build()
).get()

// 5. send messages
ws.sendTextFrame("hello")
ws.sendTextFrame("world")
ws.sendTextFrame("")
Thread.sleep(100)
out ==> List("haoyi world", "haoyi hello")

var error: String = ""
val cli2 = client.prepareGet("ws://localhost:8080/connect/nobody")
.execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(
new WebSocketListener() {

def onOpen(websocket: WebSocket) = ()

def onClose(websocket: WebSocket, code: Int, reason: String) = ()

def onError(t: Throwable) = {
error = t.toString
}
}).build()
).get()

assert(error.contains("403"))

} finally{
client.close()
}
}
}
}
17 changes: 17 additions & 0 deletions example/websockets3/build.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import mill._, scalalib._


trait AppModule extends ScalaModule{
def scalaVersion = "2.13.0"
def ivyDeps = Agg[Dep](
)
object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.1",
ivy"com.lihaoyi::requests::0.2.0",
ivy"org.asynchttpclient:async-http-client:2.5.2"
)
}
}
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ courtesy of EJ Technologies

## Changelog

### 0.2.8

- Make `Routes#log` implicit

### 0.2.7

- Cross-publish `cask.util` for Scala.js
Expand Down

0 comments on commit b83eec0

Please sign in to comment.