本文整理汇总了Scala中com.twitter.finatra.http.Controller类的典型用法代码示例。如果您正苦于以下问题:Scala Controller类的具体用法?Scala Controller怎么用?Scala Controller使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Controller类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: QwebmonController
//设置package包名称以及导入依赖的类
package com.criteo.qwebmon
import com.twitter.finagle.http.Request
import com.twitter.finatra.http.Controller
class QwebmonController(drivers: Map[String, DbDriver]) extends Controller {
get("/refresh/:target") { r: Request =>
val targetName = r.getParam("target")
drivers.get(targetName).map { driver =>
val runningQueries = driver.runningQueries
DbStatus(runningQueries, runningQueries.length)
}.getOrElse(response.badRequest(s"invalid target supplied: $targetName"))
}
get("/") { r: Request =>
response.ok.file("/index.html")
}
}
case class DbStatus(runningQueries: Seq[RunningQuery], runningQueryCount: Int)
示例2: ServerStatsController
//设置package包名称以及导入依赖的类
package br.com.verde.serverstats
import com.twitter.finatra.http.Controller
import com.google.inject.Inject
import com.twitter.finatra.validation._
import com.google.inject.Singleton
import com.twitter.finagle.http.Request
import java.lang.management.ManagementFactory
import java.lang.management.OperatingSystemMXBean
import java.io.File
@Singleton
class ServerStatsController @Inject() extends Controller {
val runtime = Runtime.getRuntime
val osBean = ManagementFactory.getOperatingSystemMXBean
val runtimeBean = ManagementFactory.getRuntimeMXBean
val memoryBean = ManagementFactory.getMemoryMXBean
val megabyte = 1024 * 1024
val second = 1000 // milisecond
get("/serverStats") {
request: Request =>
val jvmTotalRam = runtime.maxMemory
val jvmFreeRam = runtime.freeMemory
val availableProcessors = osBean.getAvailableProcessors
val freeSpace = File.listRoots.head.getFreeSpace
val serverSystemLoad = osBean.getSystemLoadAverage
val serverUptime = runtimeBean.getUptime
val param = request.getParam("onlyRam")
if (param == null) {
response
.ok
.json(s"""{ "jvm_total_ram": "${jvmTotalRam / megabyte} Mb",
"jvm_free_ram": "${jvmFreeRam / megabyte} Mb",
"free_space": "${freeSpace / megabyte} Mb",
"server_system_load": "${serverSystemLoad} %",
"server_uptime": "${serverUptime / second} s",
"available_processors": "${availableProcessors}" }""")
} else {
response
.ok
.json(s"""{ "jvm_total_ram": "${jvmTotalRam / megabyte} Mb",
"jvm_free_ram": "${jvmFreeRam / megabyte} Mb" }""")
}
}
}
示例3: WishListsController
//设置package包名称以及导入依赖的类
package me.kamkor.wishlists.controllers
import javax.inject.{Inject, Singleton}
import com.twitter.finagle.http.Request
import com.twitter.finatra.http.Controller
import com.twitter.inject.Logging
import me.kamkor.wishlists.domain.http.WishListPutRequest
import me.kamkor.wishlists.repository.WishListsRepository
@Singleton
class WishListsController @Inject()(
wishListsRepository: WishListsRepository
) extends Controller with Logging {
get("/:tenant/wishlists/:id") { request: Request =>
wishListsRepository.get(request.getParam("tenant"), request.getParam("id"))
}
put("/:tenant/wishlists/:id") { request: WishListPutRequest =>
wishListsRepository.update(request.tenant, request.toDomain()) map (_ => response.noContent)
}
delete("/:tenant/wishlists/:id") { request: Request =>
wishListsRepository.delete(request.getParam("tenant"), request.getParam("id")) map (_ => response.noContent)
}
}
示例4: LoginController
//设置package包名称以及导入依赖的类
package com.example
import java.util.concurrent.TimeUnit
import com.example.db.DB
import com.twitter.finagle.http.{Cookie, Request}
import com.twitter.finatra.http.Controller
import com.twitter.finatra.request.{FormParam, QueryParam}
import com.twitter.util.Await
import scala.concurrent.duration._
class LoginController extends Controller {
post("/login") { request: Request =>
val username = request.params("loginName")
val password = request.params("loginPass")
if (username == "god" && password == "multipass") {
val f = DB.createToken(1).map { token =>
info(s"token generated: $token")
response
.ok("You are logged in! That's very cool:)")
.cookie("auth_token", token)
}
Await.result(f)
}
else response.unauthorized()
}
}
示例5: HealthController
//设置package包名称以及导入依赖的类
package io.sokol.finatra.protobuf.integration.app
import com.twitter.finagle.http.Request
import com.twitter.finatra.http.Controller
import io.sokol.finatra.protobuf.messages.Protos.Health
import io.sokol.finatra.protobuf.messages.Protos.Health.Status
class HealthController extends Controller {
get("/marshal") { request: Request =>
Health.newBuilder()
.setStatus(Status.OK)
.build()
}
post("/unmarshal") { payload: Health =>
payload
}
}
示例6: FitmanApp
//设置package包名称以及导入依赖的类
import com.twitter.finagle.http.Request
import com.twitter.finatra.http.routing.HttpRouter
import com.twitter.finatra.http.{Controller, HttpServer}
object FitmanApp extends FitmanServer
class FitmanServer extends HttpServer {
override protected def configureHttp(router: HttpRouter): Unit = {
router.add(new HelloController)
}
}
class HelloController extends Controller {
get("/hello") { request: Request =>
"Fitman says hello"
}
}
示例7: QuoteController
//设置package包名称以及导入依赖的类
package net.songpon.controller
import javax.inject.Inject
import com.twitter.finagle.http.Request
import com.twitter.finatra.http.Controller
import net.songpon.service.QuoteServiceImpl
class QuoteController @Inject()(quoteService: QuoteServiceImpl) extends Controller {
get("/scalaquote/api/v1/quotes") { request : Request =>
try {
response.ok.json(quoteService.getQuotes())
} catch {
case e: Throwable => {
println(e.getMessage)
}
}
}
}
示例8: Test
//设置package包名称以及导入依赖的类
package com.example
import com.github.xiaodongw.swagger.finatra.SwaggerSupport
import com.twitter.finagle.http.Request
import com.twitter.finatra.http.Controller
import io.swagger.models.{Info, Swagger}
import com.twitter.util.Future
class Test {
def doThing : Future[String] = {
Future value "This is a string"
}
}
case class Pong(pong: String)
object FinatraSwaggerDocument extends Swagger
class PingController extends Controller with SwaggerSupport {
FinatraSwaggerDocument.info(new Info()
.description("Simple ping service with both get and post methods")
.version("0.0.1")
.title("Finagle Http Example Server")
)
implicit protected val swagger = FinatraSwaggerDocument
get("/ping",
swagger { o =>
o.summary("Get service for pings")
.tag("Ping")
.responseWith[Pong](200, "the pong message payload")
.responseWith(404, "there is no pong")
}
) { request: Request =>
info("get ping")
Future.value(Pong("pong"))
}
post("/ping",
swagger { o =>
o.summary("Post service for ping")
.tag("Ping")
.bodyParam[String]("a stringy payload")
.responseWith[Pong](200, "the pong message payload")
.responseWith(420, "you need to enhance your calm")
.responseWith(404, "something is not right")
}
) { request: Request =>
info("post ping")
val contentString = request.getContentString()
Future.value(Pong(s"$contentString ping"))
}
}
示例9: ManagementController
//设置package包名称以及导入依赖的类
package uk.ac.wellcome.platform.reindexer.controllers
import javax.inject.{Inject, Singleton}
import com.twitter.finagle.http.Request
import com.twitter.finatra.http.Controller
import uk.ac.wellcome.platform.reindexer.models.{JobStatus, ReindexStatus}
@Singleton
class ManagementController @Inject()() extends Controller {
get("/management/healthcheck") { request: Request =>
val currentStatus = ReindexStatus.currentStatus
val respond = currentStatus match {
case ReindexStatus(JobStatus.Init, _, _) => response.ok.json _
case ReindexStatus(JobStatus.Working, _, _) => response.ok.json _
case ReindexStatus(JobStatus.Success, _, _) => response.created.json _
case ReindexStatus(JobStatus.Failure, _, _) =>
response.internalServerError.json _
}
respond(currentStatus.toMap)
}
}
示例10: ManagementController
//设置package包名称以及导入依赖的类
package uk.ac.wellcome.platform.api.controllers
import javax.inject.{Inject, Singleton}
import com.sksamuel.elastic4s.http.HttpClient
import com.sksamuel.elastic4s.http.ElasticDsl._
import com.twitter.finagle.http.Request
import com.twitter.finatra.http.Controller
import uk.ac.wellcome.utils.GlobalExecutionContext.context
@Singleton
class ManagementController @Inject()(
elasticClient: HttpClient
) extends Controller {
get("/management/healthcheck") { request: Request =>
response.ok.json(Map("message" -> "ok"))
}
get("/management/clusterhealth") { request: Request =>
elasticClient
.execute { clusterHealth() }
.map(health => response.ok.json(health.status))
}
}
示例11: ContextController
//设置package包名称以及导入依赖的类
package uk.ac.wellcome.platform.api.controllers
import com.twitter.inject.annotations.Flag
import com.twitter.finagle.http.Request
import com.twitter.finatra.http.Controller
import javax.inject.{Inject, Singleton}
@Singleton
class ContextController @Inject()(
@Flag("api.prefix") apiPrefix: String
) extends Controller {
prefix(apiPrefix) {
get("/context.json") { request: Request =>
response.ok.file("context.json")
}
}
}
示例12: SwaggerController
//设置package包名称以及导入依赖的类
package uk.ac.wellcome.platform.api.controllers
import com.twitter.inject.annotations.Flag
import com.twitter.finagle.http.Request
import com.twitter.finatra.http.Controller
import io.swagger.util.Json
import io.swagger.models.{Info, Scheme}
import javax.inject.{Inject, Singleton}
import uk.ac.wellcome.platform.api.ApiSwagger
@Singleton
class SwaggerController @Inject()(
@Flag("api.scheme") apiScheme: String,
@Flag("api.prefix") apiPrefix: String,
@Flag("api.version") apiVersion: String,
@Flag("api.host") apiHost: String
) extends Controller {
val scheme = apiScheme match {
case "https" => Scheme.HTTPS
case _ => Scheme.HTTP
}
prefix(apiPrefix) {
get("/swagger.json") { request: Request =>
ApiSwagger.info(
new Info()
.description("Search our collections")
.version(apiVersion)
.title("Catalogue"))
ApiSwagger.scheme(scheme)
ApiSwagger.host(apiHost)
ApiSwagger.basePath(apiPrefix)
ApiSwagger.produces("application/json")
ApiSwagger.produces("application/ld+json")
response.ok.json(Json.mapper.writeValueAsString(ApiSwagger))
}
}
}
示例13: AuthenticationController
//设置package包名称以及导入依赖的类
package com.writ.api.controllers
import javax.inject.Inject
import com.twitter.finatra.http.Controller
import com.writ.api.domain.http.{LoginRequest, RegisterUserRequest}
import com.writ.api.services.AuthenticationService
class AuthenticationController @Inject()(authenticationService: AuthenticationService)
extends Controller {
post("/register") { request: RegisterUserRequest =>
authenticationService.createUser(request) handle {
case _: Exception => response.conflict("User already exists")
}
}
post("/login") { request: LoginRequest =>
authenticationService.login(request) handle {
case _: Exception => response.unauthorized("Invalid login details")
}
}
}
示例14: Frontend
//设置package包名称以及导入依赖的类
package com.twitter.diffy
import javax.inject.Inject
import com.twitter.diffy.proxy.Settings
import com.twitter.finatra.http.Controller
import com.twitter.finagle.http.Request
class Frontend @Inject()(settings: Settings) extends Controller {
case class Dashboard(
serviceName: String,
serviceClass: String,
apiRoot: String,
excludeNoise: Boolean,
relativeThreshold: Double,
absoluteThreshold: Double)
get("/") { req: Request =>
response.ok.view(
"dashboard.mustache",
Dashboard(
settings.serviceName,
settings.serviceClass,
settings.apiRoot,
req.params.getBooleanOrElse("exclude_noise", false),
settings.relativeThreshold,
settings.absoluteThreshold)
)
}
get("/css/:*") { request: Request =>
response.ok.file(request.path)
}
get("/scripts/:*") { request: Request =>
response.ok.file(request.path)
}
}
示例15: TestsController
//设置package包名称以及导入依赖的类
package fr.quidquid.my_api.controllers
import java.sql.ResultSet
import com.twitter.finagle.http.Request
import com.twitter.finatra.http.Controller
import org.jooq.{Result, Record}
import fr.quidquid.my_api.DBSupport._
import scala.collection.JavaConversions._
import scala.collection.mutable.ListBuffer
class TestsController extends Controller {
get("/ping") { request: Request =>
"pong"
}
get("/users") { request: Request =>
val sql = "SELECT * FROM users"
val rs: ResultSet = ds.getConnection.createStatement().executeQuery(sql)
val result: Result[Record] = db.fetch(rs)
var list = new ListBuffer[User]()
for (r: Record <- result) {
val id: Int = r.getValue("id").asInstanceOf[Int]
val email: String = r.getValue("email").asInstanceOf[String]
val pass: String = r.getValue("password").asInstanceOf[String]
list += User(id, email, pass)
}
response.ok.json(list)
}
}