<commit_before>package colossus.examples

import akka.util.ByteString
import colossus.IOSystem
import colossus.core.{DataBuffer, ServerRef, WorkerRef}
import colossus.protocols.http._
import colossus.protocols.redis._
import colossus.service.{Callback, Service}
import java.net.InetSocketAddress

import UrlParsing._
import HttpMethod._
import Callback.Implicits._

import colossus.controller.IteratorGenerator


object HttpExample {

  /**
   * Here we're demonstrating a common way of breaking out the business logic
   * from the server setup, which makes functional testing easy
   */
  class HttpRoutes(redis: RedisCallbackClient, worker: WorkerRef) {
    
    def invalidReply(reply: Reply) = s"Invalid reply from redis $reply"    

    def handler: PartialFunction[HttpRequest, Callback[HttpResponse]] = {
      case req @ Get on Root => req.ok("Hello World!")

      case req @ Get on Root / "shutdown" => {
        worker.system.actorSystem.shutdown
        req.ok("bye")
      }

      case req @ Get on Root / "get"  / key => redis.get(ByteString(key)).map{x => req.ok(x.utf8String)}

      case req @ Get on Root / "set" / key / value => redis.set(ByteString(key), ByteString(value)).map{ x =>
        req.ok(x.toString)
      }

    }

  }


  def start(port: Int, redisAddress: InetSocketAddress)(implicit system: IOSystem): ServerRef = {
    Service.serve[Http]("http-example", port){context =>
      val redis = new RedisCallbackClient(context.clientFor[Redis](redisAddress.getHostName, redisAddress.getPort))
      //because our routes object has no internal state, we can share it among
      //connections.  If the class did have some per-connection internal state,
      //we'd just create one per connection
      val routes = new HttpRoutes(redis, context.worker)

      context.handle{connection => 
        connection.become(routes.handler)
      }
    }<commit_msg>fixing example<commit_after>package colossus.examples

import akka.util.ByteString
import colossus.IOSystem
import colossus.core.{DataBuffer, ServerRef, WorkerRef}
import colossus.protocols.http._
import colossus.protocols.redis._
import colossus.service.{Callback, ConnectionContext, Service}
import java.net.InetSocketAddress

import UrlParsing._
import HttpMethod._
import Callback.Implicits._

import colossus.controller.IteratorGenerator


object HttpExample {

  /**
   * Here we're demonstrating a common way of breaking out the business logic
   * from the server setup, which makes functional testing easy
   */
  class HttpRoutes(redis: RedisCallbackClient, worker: WorkerRef) {
    
    def invalidReply(reply: Reply) = s"Invalid reply from redis $reply"    

    def handler(connection: ConnectionContext[Http]): PartialFunction[HttpRequest, Callback[HttpResponse]] = {
      case req @ Get on Root => req.ok("Hello World!")

      case req @ Get on Root / "shutdown" => {
        worker.system.actorSystem.shutdown
        req.ok("bye")
      }

      case req @ Get on Root / "close" => {
        connection.gracefulDisconnect()
        req.ok("closing")
      }

      case req @ Get on Root / "get"  / key => redis.get(ByteString(key)).map{x => req.ok(x.utf8String)}

      case req @ Get on Root / "set" / key / value => redis.set(ByteString(key), ByteString(value)).map{ x =>
        req.ok(x.toString)
      }

    }

  }


  def start(port: Int, redisAddress: InetSocketAddress)(implicit system: IOSystem): ServerRef = {
    Service.serve[Http]("http-example", port){context =>
      val redis = new RedisCallbackClient(context.clientFor[Redis](redisAddress.getHostName, redisAddress.getPort))
      //because our routes object has no internal state, we can share it among
      //connections.  If the class did have some per-connection internal state,
      //we'd just create one per connection
      val routes = new HttpRoutes(redis, context.worker)

      context.handle{connection => 
        connection.become(routes.handler(connection))
      }
    }
{'fraction_non_alphanumeric': 0.10483870967741936, 'fraction_numerical': 0.0005040322580645161, 'mean_word_length': 3.414905450500556, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2370688', 'n_tokens_mistral': 1195, 'n_tokens_neox': 1160, 'n_words': 319}