|
Play Framework/Scala example source code file (ApplicationSecretGenerator.scala)
The ApplicationSecretGenerator.scala Play Framework example source code/* * Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com> */ package play.sbtplugin import java.security.SecureRandom import sbt._ /** * Provides tasks for generating and updating application secrets */ object ApplicationSecretGenerator { def generateSecret = { val random = new SecureRandom() (1 to 64).map { _ => (random.nextInt(75) + 48).toChar }.mkString.replaceAll("\\\\+", "/") } def generateSecretTask = Def.task[String] { val secret = generateSecret Keys.streams.value.log.info("Generated new secret: " + secret) secret } private val ApplicationSecret = """\s*application\.secret\s*[=:].*""".r def updateSecretTask = Def.task[File] { val secret: String = play.PlayImport.PlayKeys.generateSecret.value val baseDir: File = Keys.baseDirectory.value val log = Keys.streams.value.log val secretConfig = s"""application.secret="$secret"""" val applicationConf = Option(System.getProperty("config.file")).getOrElse("conf/application.conf") val appConfFile = new File(baseDir, applicationConf) if (appConfFile.exists()) { log.info("Updating application secret in " + appConfFile.getCanonicalPath) val lines = IO.readLines(appConfFile) val appSecret = lines.find(ApplicationSecret.pattern.matcher(_).matches()) val newLines = appSecret match { case Some(line) => log.info("Replacing old application secret: " + line) lines.map { case `line` => secretConfig case other => other } case None => log.warn("Did not find application secret in " + appConfFile.getCanonicalPath) log.warn("Adding application secret to start of file") secretConfig :: lines } IO.writeLines(appConfFile, newLines) appConfFile } else { log.error("Could not find configuration file at " + appConfFile.getCanonicalPath) throw new FeedbackProvidedException {} } } } Other Play Framework source code examplesHere is a short list of links related to this Play Framework ApplicationSecretGenerator.scala source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.