Functions / methods in Scala. How does this work?

后端 未结 4 1670
旧巷少年郎
旧巷少年郎 2021-02-14 03:04

I am new to Scala and have a hard time understanding all the ways of declaring and using functions. Can someone please explain, step by step, what is going on here?

I am

4条回答
  •  既然无缘
    2021-02-14 03:53

    It might help if you see the code snippet like this:

    import akka.http.scaladsl.server.Directives._

    def route: Route = path("hello") {
        get {
          complete("Hello, World!")
        }
      }
    

    I added the Route type to show you that you are simply building a route using a syntax provided by Akka HTTP that allows you to define common matching criteria at a higher level, and nest specific criteria inside that section. Here you are using the routing DSL functionality in Akka HTTP. The routing DSL brings some implicits into scope. Using the path method ensures that you are able to handle requests coming into the host for the path host/hello, which means your host can now handle get request for the path /hello. The code body inside of the path directive represents additional matching criteria to check when we have a proper path match. The complete method knows how to convert to HttpResponse. Here you are completing with "hello world", a plain text.

    You might have additional HTTP methods standard requests like post, put, delete, as the case may be. Or even custom HTTP methods.

    This is a convenient DSL for handling HTTP requests in Akka-HTTP. Check the Akka-HTTP doc here

提交回复
热议问题