Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    jodersky
    @jodersky
    we could of course also have a nested method trie for every path try, but that IMO seems more complex than augmenting the existing dispatch trie
    Anatolii Kmetiuk
    @anatoliykmetyuk
    Hi everyone, is there any chance anyone can look into: com-lihaoyi/cask#50 ?
    megri
    @megri
    @jodersky I've made a PR for #51 with the shape DispatchTrie[Map[String, …]]. It's the old structure inverted basically.
    Martin Ceronio
    @mydoghasworms

    Hi! I am new to both Scala and Cask. I am creating a small solution using Cask to run on a local machine and need a way to start and stop the server on an ad-hoc basis.

    Based on the examples in the Cask documentation, it seems that Cask is designed to always start automatically (either by extending cask.MainRoutes with the route definitions or extending cask.Main to include multiple route definitions).

    Is there a way I can embed Cask into an application, from which I can control starting and stopping it as required?

    Li Haoyi
    @lihaoyi
    @mydoghasworms look at what the Cask test suite does, and do that
    Martin Ceronio
    @mydoghasworms
    Thanks @lihaoyi
    gaminricks87
    @gaminricks87:matrix.org
    [m]

    :point_up: Edit: @lihaoyi: Hi! I am new to Scala and have been looking everywhere for something like Flask in Scala and Cask seems like the best minimalistic option.
    Is something like what the below code does possible in Cask and if so how can I implement it? Please help!

    app = Flask(name)
    @app.route("/get_emp_info", methods = ['POST'])
    def get_employee_record():
    input_data = json.loads(request.get_json())
    out_data = input_data .to_dict(orient='records')
    return jsonify(out_data)

    if name == "main":
    app.run(host='0.0.0.0', port=6123)

    Even if you can provide some link that does something very close to this, that'd be helpful. I'd basically want to be able to run it as a Scala RESTful application.

    2 replies
    gaminricks87
    @gaminricks87:matrix.org
    [m]
    Hi is it possible to set port number and host in the minimal application example type of program?
    Also why does reading a json into a spark DF and returning a spark DF back as a json string take so long with the read option json... Is there any alternatives?
    jodersky
    @jodersky
    @gaminricks87:matrix.org check out some of the examples on how cask can read JSON. You can override port and host in Main (https://github.com/com-lihaoyi/cask/blob/f3fd29a9206cee465812a80fe6bd04c6c63500c2/cask/src/cask/main/Main.scala#L36-L37). Unfortunately I can't help about the issue with Spark dataframes
    gaminricks87
    @gaminricks87:matrix.org
    [m]
    Thank you @jodersky and @Quafadas

    request: cask.Request

    How can we read this if it is a json? like in python requests.get_json()

    jodersky
    @jodersky

    You can access the request's body and give it to your JSON parser, for example

    @cask.get("/")
      def foo(req: cask.Request) = {
        val json = upickle.default.read[ujson.Value](req.bytes)
        ...
      }

    However, there are more idiomatic ways to read JSON in cask. The two that come to mind are:

    1. if you're using ujson, you can follow the example here https://com-lihaoyi.github.io/cask/#receiving-form-encoded-or-json-data
    2. for an alternate json library, you could implement your own endpoints https://com-lihaoyi.github.io/cask/#custom-endpoints
    gaminricks87
    @gaminricks87:matrix.org
    [m]
    @jodersky: Appreciate the quick response.
    I'm unable to access the request's body as you have mentioned above. Getting only readAllBytes() option
    gaminricks87
    @gaminricks87:matrix.org
    [m]
    I can access req.data which returns an object like this : io.undertow.io.UndertowInputStream@6d81f169
    jodersky
    @jodersky
    strange, the bytes method is implemented right in the request class https://github.com/com-lihaoyi/cask/blob/master/cask/src/cask/model/Params.scala#L20
    gaminricks87
    @gaminricks87:matrix.org
    [m]
    for spark i require scala 2.12 and cask latest version i require scala 2.13
    I think bcoz of older version this is not working for me
    Simon Parten
    @Quafadas

    The cask documentatino suggests that a decorator could be used to automagically serialise responses as JSON.
    "
    but you could make it e.g. automatically serialize returned objects to JSON responses via your favorite library, or serialize
    "
    Now, I've gotten this far..

      class writeToDoList(val path: String, val methods: Seq[String])
        extends cask.HttpEndpoint[Seq[Todos], Seq[String]]{    
    
        def wrapFunction(ctx: cask.Request, delegate: Delegate) = {
          delegate(Map()).map{num =>
            cask.Response(upickle.default.write(num))
          }
        }
    
        def wrapPathSegment(s: String) = Seq(s)
    
        type InputParser[T] = cask.endpoints.QueryParamReader[T]
      }

    But obviously, the end goal is extends cask.HttpEndpoint[T, Seq[String]], unfortunately, I can't seem to get the implicit in scope.

    The compiler says "implementation limitation, argument lists more than length 1 are ignored", if I try to add an implicit argument to the decorator class.
    Any suggestions?
    Otherwise, I'd have to write a decorator per return type :-/...

    pingbat
    @pingbat
    Are there any examples of using Cask with a ScalaJS frontend?
    pingbat
    @pingbat
    And with autowire and boopickle if possible! :-)
    Li Haoyi
    @lihaoyi
    @Quafadas IIRC how it works for uPickle is that it takes some type and then an implicit conversion to that type, and the implicit conversion takes the typeclass
    presumably you can do the same thing?
    Simon Parten
    @Quafadas
    @lihaoyi thankyou. I had to squint at this on and off for quite some time, but I think I have what I want now based on your hint.
    k0ala
    @k0ala
    hi all, I have a noob question about cask. I modified the MinimalApplication (0.7.8) to run on port 8484, and can only connect to it on localhost (from within the Ubuntu 20.04 VM where cask is running), but not from outside. However, if I run another http server (such as a with python -m http.server 8484) I can connect fine. Any hints?
    k0ala
    @k0ala
    Ah, I notice that if I override host to "0.0.0.0" I can access from the outside. Not sure if that's recommended, but at least it allows me to try out cask which was my objective :)
    k0ala
    @k0ala
    I am trying to create a route to upload an image file, but so far it's not working: @cask.postForm("/upload") def uploadFile(image: cask.FormFile) = { image.fileName }
    This yields an error when trying to post: scala> requests.post("http://localhost:8080/upload", data=java.nio.file.Paths.get("cat1.jpg")) requests.RequestFailedException: Request to http://localhost:8080/upload failed with status code 400 Unable to parse form data: java.lang.NullPointerException: Cannot invoke "io.undertow.server.handlers.form.FormDataParser.parseBlocking()" because the return value of "io.undertow.server.handlers.form.FormParserFactory.createParser(io.undertow.server.HttpServerExchange)" is null
    Any hints?
    1 reply
    Li Haoyi
    @lihaoyi
    @k0ala looking at the test suite https://github.com/com-lihaoyi/cask/blob/f3fd29a9206cee465812a80fe6bd04c6c63500c2/example/formJsonPost/app/test/src/ExampleTests.scala#L42-L48, cask.FormFile requires you to use the requests.MultiPart data structure to upload, rather than a raw data =
    1 reply
    k0ala
    @k0ala
    @lihaoyi thank you! I guess it doesn't help that I am an http and web noob (in addition to being a cask noob), I only know scala and decided it was time to learn how to make a website :) Btw, do you plan to refresh the "hands-on-scalajs" website sometime? It is very nicely written but outdated
    Li Haoyi
    @lihaoyi
    (the test suite has a lot of good stuff in general; basically every route in every example has a test case that calls it, and AFAIK all of them should pass)
    haha yeah I can tell, localhost vs 0.0.0.0 is something I'd expect non-scala-web-developers to know :)
    I don't have any concrete plans for updating hands-on-scala-js; I'm still a bit burnt out writing hands-on-scala-programming in 2019-2020 era, so not sure I'd be able to pick up a big project like that
    I wrote hands-on-scala-js when my curiosity was stronger, my standards were lower, and my non-work responsibilities were much fewer haha
    k0ala
    @k0ala
    yeah that's understandable, it blows my mind how many things you've produced
    Li Haoyi
    @lihaoyi
    it could definitely do with a refresh, e.g. using Mill instead of SBT, using Cask instead of Akka-HTTP, using DefinitelyScala facades, using some other OSS projects like Slinky,
    k0ala
    @k0ala
    that sounds awesome ;)
    Li Haoyi
    @lihaoyi
    nope
    k0ala
    @k0ala
    In the meantime I figured out how to upload an image :) I ended up using the raw cask.Request as you suggested. Now I am struggling with the notion of "being logged in", i.e. only allowing users access if they have successfully entered their password. If you have any tutorial/example on this, I am all ears!
    (I went through some AWS Cognito tutorial, so I could use that for the login part. What remains unclear to me is how to use the token or code that is returned by Cognito.)
    k0ala
    @k0ala
    I got a bit of education on JWT tokens, here is how I think it might work with Cognito: 1) user gets sent to Cognito login, receives a token. 2) scalaJS saves the token and adds it to every request header going forward. 3) cask validates the token at every request. Does that sound reasonable? Or am I heading in the wrong direction?
    k0ala
    @k0ala
    I guess I should ask this question in the scala-js/scala-js gitter instead
    lakinwecker
    @lakinwecker
    Good morning, hope everyone is doing well. I'm somewhat new to Scala development, but am tinkering around in using it for web stuff (coming from the python/django world). I'm using scalatags, and am replacing a very simple very ancient php site which has very little server side processing. I like the simplicity of Cask, but am running into issues doing simple form processing using it. For example, if I make a handler which expects a radio button value, but the user submits without the radio button, the resulting page is a 400, rather than something I can handle to display the appropriate error message. I realize that most people do this sort of form submission in JS these days, but I'd like to know how to handle these sorts of things within cask. Does anyone have a recommended method for taking a POST with a urlencoded body and doing some very basic validation (required fields, etc) and turning it into a case class instance or a list of error messages?
    I tried googling for some libraries, and didn't find much, and I've read the cask documentation and couldn't figure it out. Where I ended up was: https://gist.github.com/lakinwecker/b53b59f09c6653e93b14f9e4364c28a2 but if the cabins value is missing from the POST, then cask returns a 400 instead of calling this method with a None for the cabins option.
    Li Haoyi
    @lihaoyi
    @lakinwecker could you try adding a default value = None to your optional parameter see if that can make it happy
    Simon Miles
    @drsimonmiles

    Hello. I've been trying out Cask and got an HTTP server with a Scala.js front-end working without problems, and all seems great. I then wanted to try out Websockets but have got stuck. I tried copying verbatim the first example from the codebase as a starting point:
    https://github.com/com-lihaoyi/cask/blob/master/example/websockets/app/src/Websockets.scala
    but I get compilation errors.

    case cask.Ws.Text("") => channel.send(cask.Ws.Close())
    no implicit argument of type sourcecode.FileName was found for parameter fileName of method send in class BaseActor

    From looking at the code, I understand that WsActor inherits from Castor's BaseActor (via SimpleActor) and the latter's send method requires an implicit sourcecode fileName and line. I don't understand what these could be in the context of Websockets (but I'm quite new to Websockets).
    I tried running the websockets example test from the Cask codebase, in case it was something odd in my own setup, but hit another problem doing that. I think it's a separate Mill-related question so I'll omit details.
    Any suggestion how I might be getting the compilation error above?

    Li Haoyi
    @lihaoyi
    are you on Scala 3?
    feels like there's some dependency scala version conflicts
    Simon Miles
    @drsimonmiles
    Ah yes, sorry I should have said that, I am using Scala 3