Skip to content

Instantly share code, notes, and snippets.

@yohangz
Created February 1, 2018 12:41
Show Gist options
  • Save yohangz/bc7d62529d91ff6afb42a8b171dd5d18 to your computer and use it in GitHub Desktop.
Save yohangz/bc7d62529d91ff6afb42a8b171dd5d18 to your computer and use it in GitHub Desktop.
package controllers
import javax.inject._
import play.api.Configuration
import play.api.http.HttpErrorHandler
import play.api.mvc._
/**
* Frontend controller managing all static resource associate routes.
* @param assets Assets controller reference.
* @param cc Controller components reference.
*/
@Singleton
class FrontendController @Inject()(assets: Assets, errorHandler: HttpErrorHandler, config: Configuration, cc: ControllerComponents) extends AbstractController(cc) {
def index: Action[AnyContent] = assets.at("index.html")
def assetOrDefault(resource: String): Action[AnyContent] = if (resource.startsWith(config.get[String]("apiPrefix"))){
Action.async(r => errorHandler.onClientError(r, NOT_FOUND, "Not found"))
} else {
if (resource.contains(".")) assets.at(resource) else index
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment