Skip to content

Instantly share code, notes, and snippets.

case class User(name:String)
trait MyApi {
// Binding http path parameters (e.g., :name) to method arguments
@Endpoint(method = HttpMethod.GET, path = "/user/:name")
def getUser(name: String): User = User(name)
}
trait DbService {
private val connectionPool = bind[ConnectionPool]
.onStart { dbConfig: DbConfig => startConnectionPool(dbConfig) }
.onShutdown { _.close }
def query(sql:String) = { connectionPool.execute(sql) ... }
}
import wvlet.airframe._
trait MyApp {
private val threadPool = bind[ThreadPool]
private val dbService = bind[DbService]
...
threadPool.submit(dbService.query("select ..."))
}
import wvlet.airframe._
// Read configurations from a YAML file
val yaml = readConfig("config.yml")
// Configure Application
val d = newDesign
.bind[ServiceConfig].toInstance(new ServiceConfig(yaml.get(...), yaml.get(..))
.bind[WebAppConfig].toInstance(new WebAppConfig(...))
// Initialize Services
// New Services
class WebApp(webAppConfig:WebAppConfig, component:WebComponent)
class WebComponent(service:Service)
// Pack configuration set in a class
case class ConfigSet(serviceConfig:ServiceConfig, webAppConfig:WebAppConfig)
// Read configurations from a YAML file
def readConfig: ConfigSet = {
val yaml = readConfig("config.yml")
case class ServiceConfig(host:String, port:Int)
class Service(config:ServiceConfig) {
// ...
}
// Building a service instance
val config = new ServiceConfig("localhost", "8080")
val service = new Service(config)
logger.resetHandler(new LogRotationHandler(
fileName = "your-app.log",
maxNumberOfFiles = 100, // rotate up to 100 log files
maxSizeInBytes = 100 * 1024 * 1024 // 100MB
AppLogFormatter // Any log formatter you like
))
import org.scalatest._
import wvlet.log.LogFormatter.SourceCodeLogFormatter
trait Spec extends WordSpec with Matchers with LogSupport {
override def run(testName: Option[String], args: Args): Status = {
// Add source code location to the debug logs
Logger.setDefaultFormatter(SourceCodeLogFormatter)
// Periodically scan log level file
Logger.scheduleLogLevelScan
import org.slf4j.Logger
import org.slf4j.LoggerFactory
object MyApp {
val logger = LoggerFactory.getLogger(classOf[HelloWorld])
logger.info("Hello World")
}
import wvlet.log.LogSupport
object MyApp extends LogSupport {
info("Hello airframe-log!")
warn("This is a warning message")
debug("debug messsages will not be shown by default")
}