Skip to content
This repository has been archived by the owner on Feb 28, 2020. It is now read-only.

Commit

Permalink
feat(status_pages): create default success and error pages for starte…
Browse files Browse the repository at this point in the history
…rs (#443)
  • Loading branch information
dingwilson authored Feb 1, 2018
1 parent ea84dce commit df4a00b
Show file tree
Hide file tree
Showing 10 changed files with 293 additions and 2 deletions.
21 changes: 19 additions & 2 deletions refresh/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ module.exports = Generator.extend({
endpoints: []
}

if (this.web) this.appInitCode.middlewares.push('router.all(middleware: StaticFileServer())')
if (this.web || this.usecase) this.appInitCode.middlewares.push('router.all(middleware: StaticFileServer())')
if (this.appType === 'crud') {
this.appInitCode.endpoints.push('try initializeCRUDResources(cloudEnv: cloudEnv, router: router)')
this.dependencies.push('.package(url: "https://github.com/IBM-Swift/SwiftyJSON.git", from: "17.0.0"),')
Expand All @@ -300,7 +300,10 @@ module.exports = Generator.extend({
this.appInitCode.capabilities.push('initializeMetrics(app: self)')
this.dependencies.push('.package(url: "https://github.com/RuntimeTools/SwiftMetrics.git", from: "2.0.0"),')
}
if (this.usecase) this.appInitCode.endpoints.push('initializeAppRoutes(app: self)')
if (this.usecase) {
this.appInitCode.endpoints.push('initializeAppRoutes(app: self)')
this.appInitCode.endpoints.push('initializeErrorRoutes(app: self)')
}
},

ensureGeneratorIsCompatibleWithProject: function () {
Expand Down Expand Up @@ -994,6 +997,20 @@ module.exports = Generator.extend({
this.fs.write(this.destinationPath('public', '.keep'), '')
}

if (this.usecase) {
this.fs.copy(
this.templatePath('public'),
this.destinationPath('public')
)

this._ifNotExistsInProject(['Sources', this.applicationModule, 'Routes', 'ErrorRoutes.swift'], (filepath) => {
this.fs.copyTpl(
this.templatePath('common', 'ErrorRoutes.swift'),
filepath
)
})
}

if (this.appType !== 'crud') {
this.fs.copyTpl(
this.templatePath('common', 'README.scaffold.md'),
Expand Down
12 changes: 12 additions & 0 deletions refresh/templates/common/ErrorRoutes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import LoggerAPI

func initializeErrorRoutes(app: App) {
app.router.all { request, response, _ in
if response.statusCode == .unknown {
let path = request.urlURL.path
if path != "/" && path != "" {
try response.status(.notFound).redirect("/404.html")
}
}
}
}
22 changes: 22 additions & 0 deletions refresh/templates/public/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>

<head>
<title>IBM Cloud Web Starter</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link inline rel="stylesheet" href="css/default.css"></link>
</head>

<body>
<div>
<div id="error-container">
<div>
<div class="sad-cloud" ></div>
<h1>404!</h1>
<h2>Whoops! Looks like you got lost or couldn't find your page.</h2>
<h2>Click to go to the <a href="https://developer.ibm.com/swift/" target="_blank">overview page</a>.</h2>
</div>
</div>
</div>
</body>
</html>
22 changes: 22 additions & 0 deletions refresh/templates/public/500.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>

<head>
<title>IBM Cloud Web Starter</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link inline href="css/default.css" rel="stylesheet" />
</head>

<body>
<div>
<div id="error-container">
<div>
<div class="sad-cloud"></div>
<h1>Uh oh!</h1>
<h2>Whoops! The server couldn't handle your request.</h2>
<h2>Click to go to the <a href="https://developer.ibm.com/swift/" target="_blank">overview page</a>.</h2>
</div>
</div>
</div>
</body>
</html>
134 changes: 134 additions & 0 deletions refresh/templates/public/css/default.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions refresh/templates/public/images/404.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added refresh/templates/public/images/arrow-right.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added refresh/templates/public/images/cloud-header.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 67 additions & 0 deletions refresh/templates/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!DOCTYPE html>
<html>

<head>
<title>IBM Cloud Web Starter</title>
<link inline href="css/default.css" rel="stylesheet" />
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
<header id="flex-header">
<div class="cloud-header"></div>
<h1>Congratulations!</h1>

<h2>You are currently running a Swift app built for the IBM Cloud.</h2>
</script>
</header>
<main>
<ul>

<li>
<div class="right-arrow"></div>
<div>
<a target="_blank" href="https://console.bluemix.net/developer/appservice/dashboard">App Services on IBM Cloud</a>
</div>
</li>

<li>
<div class="right-arrow"></div>
<div>
<a target="_blank" href="https://developer.ibm.com/swift">Visit the Swift Developer Center</a>
</div>
</li>

<li>
<div class="right-arrow"></div>
<div>
<a target="_blank" href="https://swift-at-ibm-slack.mybluemix.net/">Join the discussion on Slack</a>
</div>
</li>

<li>
<div class="right-arrow"></div>
<div>
<a target="_blank" href="https://developer.ibm.com/swift/guides/">Checkout tutorials for Swift</a>
</div>
</li>

<li>
<div class="right-arrow"></div>
<div>
<a target="_blank" href="https://www.ibm.com/cloud/cli">Try out the IBM Cloud Developer Tools</a>
</div>
</li>

<li>
<div class="right-arrow"></div>
<div>
<a target="_blank" href="http://www.kitura.io/">Visit Kitura.io</a>
</div>
</li>

</ul>
</main>
</body>

</html>
16 changes: 16 additions & 0 deletions test/unit/refresh.js
Original file line number Diff line number Diff line change
Expand Up @@ -858,13 +858,29 @@ describe('Unit tests for swiftserver:refresh', function () {
runContext.cleanTestDirectory()
})

it('does initialize file serving middleware', function () {
assert.fileContent(applicationSourceFile, 'router.all(middleware: StaticFileServer())')
})

it('does copy public folder', function () {
assert.file(commonTest.webDir)
})

it('does initialize AppRoutes', function () {
assert.fileContent(applicationSourceFile, 'initializeAppRoutes(app: self)')
})

it('does create AppRoutes.swift route file', function () {
commonTest.itCreatedRoutes('AppRoutes')
})

it('does initialize ErrorRoutes', function () {
assert.fileContent(applicationSourceFile, 'initializeErrorRoutes(app: self)')
})

it('does create ErrorRoutes.swift route file', function () {
commonTest.itCreatedRoutes('ErrorRoutes')
})
})

describe('with docker', function () {
Expand Down

0 comments on commit df4a00b

Please sign in to comment.