Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kouphax
Last active December 16, 2015 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kouphax/5407275 to your computer and use it in GitHub Desktop.
Save kouphax/5407275 to your computer and use it in GitHub Desktop.
Basic Sample of how to avoid the now deprecated ServerConfig. `Main.scala` has all the stuff for running the app and `dev.scala` is the config you pass in.
import com.twitter.ostrich.admin.{AdminServiceFactory, TimeSeriesCollectorFactory, StatsFactory}
import se.yobriefca.HelloWorldServiceConfig
new HelloWorldServiceConfig {
port = 3009
admin = Some(AdminServiceFactory(9990,
statsNodes = List(new StatsFactory(
reporters = List(new TimeSeriesCollectorFactory())
))
))
}
package se.yobriefca
import com.twitter.ostrich.admin.{AdminServiceFactory, RuntimeEnvironment}
import com.twitter.finagle.builder.{ServerBuilder, Server}
import com.twitter.finagle.http.Http
import java.net.InetSocketAddress
import com.twitter.finagle.stats.OstrichStatsReceiver
import com.twitter.finagle.Service
import org.jboss.netty.handler.codec.http._
import com.twitter.util.{Eval, Config, Future}
import com.twitter.ostrich.stats.Stats
import com.twitter.ostrich.admin.{Service => OstrichService }
class HelloWorldServiceConfig extends Config[HelloWorldServiceOrchestrator] {
var port = required[Int]
var admin: Option[AdminServiceFactory] = None
def apply(runtime: RuntimeEnvironment): HelloWorldServiceOrchestrator = {
admin.map(_(runtime))
this.apply()
}
def apply(): HelloWorldServiceOrchestrator = {
new HelloWorldServiceOrchestrator(this)
}
}
class HelloWorldServiceOrchestrator(config: HelloWorldServiceConfig) extends OstrichService {
private var server: Server = _
def start() {
server = ServerBuilder()
.codec(Http())
.bindTo(new InetSocketAddress(config.port))
.name("")
.reportTo(new OstrichStatsReceiver)
.build(new Service[HttpRequest, HttpResponse]{
def apply(request: HttpRequest): Future[HttpResponse] = {
Stats.incr("ClientRequests")
val response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK)
Future.value(response)
}
})
}
def shutdown() {
server.close()
}
}
object Main extends App {
val runtime = RuntimeEnvironment(this, args)
val server = new Eval().apply[HelloWorldServiceConfig](runtime.configFile)(runtime)
server.start()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment