Bootstrapping a web server in Scala

后端 未结 8 1704
走了就别回头了
走了就别回头了 2021-02-01 10:32

The following is possible using Python:

$ apt-get install python
$ easy_install Flask
$ cat > hello.py
from flask import Flask
app = Flask(__name__)

@app.rou         


        
8条回答
  •  迷失自我
    2021-02-01 11:04

    This uses the HttpServer class that is built-in in JDK6. Feel free to suggest improvements, I'm new to Scala.

    package org.test.simplehttpserver
    
    import java.net.InetSocketAddress
    import com.sun.net.httpserver.{HttpExchange, HttpHandler, HttpServer}
    import collection.mutable.HashMap
    
    abstract class SimpleHttpServerBase(val socketAddress: String = "127.0.0.1",
                                        val port: Int = 8080,
                                        val backlog: Int = 0) extends HttpHandler {
      private val address = new InetSocketAddress(socketAddress, port)
      private val server = HttpServer.create(address, backlog)
      server.createContext("/", this)
    
      def redirect(url: String) =
        
          
              
          
          
            You are being redirected to:
            
              {url}
            
          
        
    
      def respond(exchange: HttpExchange, code: Int = 200, body: String = "") {
        val bytes = body.getBytes
        exchange.sendResponseHeaders(code, bytes.size)
        exchange.getResponseBody.write(bytes)
        exchange.getResponseBody.write("\r\n\r\n".getBytes)
        exchange.getResponseBody.close()
        exchange.close()
      }
    
      def start() = server.start()
    
      def stop(delay: Int = 1) = server.stop(delay)
    }
    
    abstract class SimpleHttpServer extends SimpleHttpServerBase {
      private val mappings = new HashMap[String, () => Any]
    
      def get(path: String)(action: => Any) = mappings += path -> (() => action)
    
      def handle(exchange: HttpExchange) = mappings.get(exchange.getRequestURI.getPath) match {
        case None => respond(exchange, 404)
        case Some(action) => try {
          respond(exchange, 200, action().toString)
        } catch {
          case ex: Exception => respond(exchange, 500, ex.toString)
        }
      }
    }
    
    class HelloApp extends SimpleHttpServer {
      var count = 0
    
      get("/") {
        "There's nothing here"
      }
    
      get("/hello") {
        "Hello, world!"
      }
    
      get("/markup") {
        
          
            Test Title
          
          
            Test Body
          
        
      }
    
      def countPage = 
        
          Test Title
        
        
          Count:
          {count}++
          --
          Reset
        
      
    
      get("/count") {
        countPage
      }
    
      get("/resetCount") {
        count = 0
        redirect("/count")
      }
    
      get("/increaseCount") {
        count = count + 1
        redirect("/count")
      }
    
      get("/decreaseCount") {
        count = count - 1
        redirect("/count")
      }
    
      get("/error") {
        throw new RuntimeException("Bad bad error occurred")
      }
    }
    
    object Main {
    
      def main(args: Array[String]) {
        val server = new HelloApp()
        server.start()
      }
    }
    

提交回复
热议问题