从中间件 Vapor 中排除一些视图
Exclude some views from middleware Vapor
我已经创建了 Vapor 项目。我已经注册了两个视图和两个 API,如下所示。
drop.get { req in
return try drop.view.make("index.html")
}
drop.get("home") { req in
return try drop.view.make("home.html")
}
// Register the GET request routes
drop.get("appname") { request in
return "Welcome to Swift Web service";
}
drop.get("appversion") { request in
return "v1.0";
}
中间件代码:
// Added the Middleware version for request and response
final class VersionMiddleware: Middleware {
// Interact with request and response
func respond(to request: Request, chainingTo next: Responder) throws -> Response {
//Middleware is the perfect place to catch errors thrown from anywhere in the application.
do {
// Exclude the views from middleware
if ( request.uri.path != "/") {
// The request has a 'Version' named token that equals "API \(httpVersion)"
guard request.headers["access_token"] == "\(publicAPIKey)" else {
throw Abort.custom(
status: .badRequest,
message: "Sorry, Wrong web service authendication!!"
) // The request will be aborted.
}
}
let response = try next.respond(to: request)
response.headers["Version"] = "API \(httpVersion)"
return response
} catch {
throw Abort.custom(
status: .badRequest,
message: "Sorry, we were unable to query the Web service."
)
}
}
}
添加了中间件配置:
// Configure the middleware
drop.addConfigurable(middleware: VersionMiddleware() as Middleware, name: "VersionMiddleware")
My Questions are:
Whenever user try to load home.html it should validate Middleware
conditions and if we load index.html server will exclude the
middleware validation.
Same as in API's: Whenever user try to load "/appname" it should
validate Middleware conditions and if we load "appversion" server will
exclude the middleware validation.
我使用 request.uri.path != "/" 完成了此操作。我们还有其他方法可以在 Vapor 中配置它吗?
你可以group
一组路由并在那里分配中间件
drop.group(VersionMiddleware()) { group in
drop.get("home") { req in
return try drop.view.make("home.html")
}
// Register the GET request routes
drop.get("appname") { request in
return "Welcome to Swift Web service";
}
}
drop.get { req in
return try drop.view.make("index.html")
}
drop.get("appversion") { request in
return "v1.0";
}
并且不调用 addConfigurable
方法
我已经创建了 Vapor 项目。我已经注册了两个视图和两个 API,如下所示。
drop.get { req in
return try drop.view.make("index.html")
}
drop.get("home") { req in
return try drop.view.make("home.html")
}
// Register the GET request routes
drop.get("appname") { request in
return "Welcome to Swift Web service";
}
drop.get("appversion") { request in
return "v1.0";
}
中间件代码:
// Added the Middleware version for request and response
final class VersionMiddleware: Middleware {
// Interact with request and response
func respond(to request: Request, chainingTo next: Responder) throws -> Response {
//Middleware is the perfect place to catch errors thrown from anywhere in the application.
do {
// Exclude the views from middleware
if ( request.uri.path != "/") {
// The request has a 'Version' named token that equals "API \(httpVersion)"
guard request.headers["access_token"] == "\(publicAPIKey)" else {
throw Abort.custom(
status: .badRequest,
message: "Sorry, Wrong web service authendication!!"
) // The request will be aborted.
}
}
let response = try next.respond(to: request)
response.headers["Version"] = "API \(httpVersion)"
return response
} catch {
throw Abort.custom(
status: .badRequest,
message: "Sorry, we were unable to query the Web service."
)
}
}
}
添加了中间件配置:
// Configure the middleware
drop.addConfigurable(middleware: VersionMiddleware() as Middleware, name: "VersionMiddleware")
My Questions are:
Whenever user try to load home.html it should validate Middleware conditions and if we load index.html server will exclude the middleware validation.
Same as in API's: Whenever user try to load "/appname" it should validate Middleware conditions and if we load "appversion" server will exclude the middleware validation.
我使用 request.uri.path != "/" 完成了此操作。我们还有其他方法可以在 Vapor 中配置它吗?
你可以group
一组路由并在那里分配中间件
drop.group(VersionMiddleware()) { group in
drop.get("home") { req in
return try drop.view.make("home.html")
}
// Register the GET request routes
drop.get("appname") { request in
return "Welcome to Swift Web service";
}
}
drop.get { req in
return try drop.view.make("index.html")
}
drop.get("appversion") { request in
return "v1.0";
}
并且不调用 addConfigurable
方法