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

Commit

Permalink
fix: change service initialization order (#442)
Browse files Browse the repository at this point in the history
  • Loading branch information
Neil Hardman authored and tunniclm committed Feb 9, 2018
1 parent 2a03dcf commit 4e909b9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
3 changes: 2 additions & 1 deletion refresh/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ module.exports = Generator.extend({
// Initialization code to add to Application.swift by code block
// eg this.appInitCode.services.push('try initializeServiceCloudant()')
this.appInitCode = {
metrics: undefined,
capabilities: [],
services: [],
service_imports: [],
Expand All @@ -297,7 +298,7 @@ module.exports = Generator.extend({
}
if (this.metrics) {
this.modules.push('"SwiftMetrics"')
this.appInitCode.capabilities.push('initializeMetrics(app: self)')
this.appInitCode.metrics = 'initializeMetrics(router: router)'
this.dependencies.push('.package(url: "https://github.com/RuntimeTools/SwiftMetrics.git", from: "2.0.0"),')
}
if (this.usecase) {
Expand Down
9 changes: 5 additions & 4 deletions refresh/templates/common/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public let health = Health()
<% if (appInitCode.services.length > 0) { -%>

class ApplicationServices {
// Service references
// Initialize services
<% appInitCode.service_variables.forEach(function(variable) { -%>
<%- variable %>
<% }); -%>
Expand All @@ -53,8 +53,11 @@ public class App {
<% } -%>

public init() throws {
<% if (appInitCode.metrics) { -%>
// Run the metrics initializer
<%- appInitCode.metrics %>
<% } -%>
<% if (appInitCode.services.length > 0) { -%>

// Services
services = try ApplicationServices(cloudEnv: cloudEnv)
<% } -%>
Expand All @@ -68,13 +71,11 @@ public class App {
<% }); -%>
<% } -%>
<% if (appInitCode.middlewares.length > 0) { -%>

// Middleware
<% appInitCode.middlewares.forEach(function(middleware) { -%>
<%- middleware %>
<% }); -%>
<% } -%>

// Endpoints
<% appInitCode.endpoints.forEach(function(endpoint) { -%>
<%- endpoint %>
Expand Down
7 changes: 4 additions & 3 deletions refresh/templates/common/Metrics.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Kitura
import SwiftMetrics
import SwiftMetricsDash
import SwiftMetricsPrometheus
Expand All @@ -7,11 +8,11 @@ var swiftMetrics: SwiftMetrics?
var swiftMetricsDash: SwiftMetricsDash?
var swiftMetricsPrometheus: SwiftMetricsPrometheus?

func initializeMetrics(app: App) {
func initializeMetrics(router: Router) {
do {
let metrics = try SwiftMetrics()
let dashboard = try SwiftMetricsDash(swiftMetricsInstance: metrics, endpoint: app.router)
let prometheus = try SwiftMetricsPrometheus(swiftMetricsInstance: metrics, endpoint: app.router)
let dashboard = try SwiftMetricsDash(swiftMetricsInstance: metrics, endpoint: router)
let prometheus = try SwiftMetricsPrometheus(swiftMetricsInstance: metrics, endpoint: router)

swiftMetrics = metrics
swiftMetricsDash = dashboard
Expand Down
9 changes: 6 additions & 3 deletions test/lib/common_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,17 +328,20 @@ exports.itCreatedMetricsFilesWithExpectedContent = function () {
it('metrics boilerplate contains expected content', function () {
var metricsFile = `${exports.applicationSourceDir}/Metrics.swift`
assert.fileContent([
[metricsFile, 'import Kitura'],
[metricsFile, 'import SwiftMetrics'],
[metricsFile, 'import SwiftMetricsDash'],
[metricsFile, 'import SwiftMetricsPrometheus'],
[metricsFile, 'swiftMetrics: SwiftMetrics'],
[metricsFile, 'func initializeMetrics(app: App)'],
[metricsFile, 'func initializeMetrics(router: Router)'],
[metricsFile, 'SwiftMetrics()'],
[metricsFile, 'try SwiftMetricsDash(']
[metricsFile, 'try SwiftMetricsDash('],
[metricsFile, 'try SwiftMetricsPrometheus(']
])
})

it('application initializes metrics', function () {
assert.fileContent(exports.applicationSourceFile, 'initializeMetrics(app: self)')
assert.fileContent(exports.applicationSourceFile, 'initializeMetrics(router: router)')
})
}

Expand Down

0 comments on commit 4e909b9

Please sign in to comment.