Initial Commit
This commit is contained in:
0
Sources/App/Controllers/.gitkeep
Normal file
0
Sources/App/Controllers/.gitkeep
Normal file
7
Sources/App/JSONDecode.swift
Normal file
7
Sources/App/JSONDecode.swift
Normal file
@@ -0,0 +1,7 @@
|
||||
import Foundation
|
||||
import Vapor
|
||||
|
||||
struct Config: Codable {
|
||||
let title: String
|
||||
let services: [String:[[String:String]]]
|
||||
}
|
||||
17
Sources/App/configure.swift
Normal file
17
Sources/App/configure.swift
Normal file
@@ -0,0 +1,17 @@
|
||||
import Vapor
|
||||
import Leaf
|
||||
import LeafErrorMiddleware
|
||||
|
||||
// configures your application
|
||||
public func configure(_ app: Application) throws {
|
||||
// Serve files from /Public
|
||||
app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))
|
||||
app.middleware.use(LeafErrorMiddleware())
|
||||
|
||||
// Configure Leaf
|
||||
LeafOption.caching = app.environment.isRelease ? .default : .bypass
|
||||
app.views.use(.leaf)
|
||||
|
||||
// Register routes
|
||||
try routes(app)
|
||||
}
|
||||
7
Sources/App/isItUp.swift
Normal file
7
Sources/App/isItUp.swift
Normal file
@@ -0,0 +1,7 @@
|
||||
import Foundation
|
||||
import Vapor
|
||||
|
||||
struct isItUp: Codable {
|
||||
let isitdown: Bool
|
||||
let response_code: Int
|
||||
}
|
||||
21
Sources/App/routes.swift
Normal file
21
Sources/App/routes.swift
Normal file
@@ -0,0 +1,21 @@
|
||||
import Vapor
|
||||
import Foundation
|
||||
import Leaf
|
||||
|
||||
func routes(_ app: Application) throws {
|
||||
// Render home page when root domain is loaded
|
||||
app.get { req -> EventLoopFuture<View> in
|
||||
let fileData = try String(contentsOfFile: "\(app.directory.resourcesDirectory)/config.json").data(using: .utf8)
|
||||
let config: Config = try! JSONDecoder().decode(Config.self, from: fileData!)
|
||||
return req.view.render("home", ["config": config])
|
||||
}
|
||||
|
||||
app.get("status", ":url") { req -> EventLoopFuture<[String: String]> in
|
||||
let url = URL(string: req.parameters.get("url")!)
|
||||
return req.client.get("https://isitdown.site/api/v3/\(url!)").flatMapThrowing { res in
|
||||
try res.content.decode(isItUp.self)
|
||||
}.map { json in
|
||||
["down": String(json.isitdown), "code": String(json.response_code)]
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user