Skip to content

Commit

Permalink
Ade HTTPFilterPlugin support
Browse files Browse the repository at this point in the history
  • Loading branch information
enums committed Jun 26, 2017
1 parent a310fb2 commit 8a55b31
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 79 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 8 additions & 8 deletions Source/Pjango/Pjango/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ import Foundation

// Pjango

public var WORKSPACE_PATH = ""
public var PJANGO_WORKSPACE_PATH = ""

public var SERVER_PORT: UInt16 = 8080
public var PJANGO_SERVER_PORT: UInt16 = 8080

public var DEBUG_LOG = true
public var PJANGO_LOG_DEBUG = true

public var LOG_TO_FILE = true
public var PJANGO_LOG_TO_FILE = true

public var LOG_PATH = "pjango.log"
public var PJANGO_LOG_PATH = "pjango.log"

// Django
public var BASE_DIR = ""
public var PJANGO_BASE_DIR = ""

public var TEMPLATES_DIR = "templates"
public var PJANGO_TEMPLATES_DIR = "templates"

public var STATIC_URL = "static"
public var PJANGO_STATIC_URL = "static"

47 changes: 47 additions & 0 deletions Source/Pjango/Plugin/PCHTTPFilterPlugin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// PCHTTPFilterPlugin.swift
// Pjango-Dev
//
// Created by 郑宇琦 on 2017/6/26.
//
//

import Foundation
import PerfectHTTP

open class PCHTTPFilterPlugin: PCPlugin, HTTPRequestFilter, HTTPResponseFilter {

open var priority: HTTPFilterPriority {
return .low
}

open func requestFilter(req: HTTPRequest, res: HTTPResponse) -> Bool { return true }

open func responseFilterHeader(req: HTTPRequest, res: HTTPResponse) -> Bool { return true }

open func responseFilterBody(req: HTTPRequest, res: HTTPResponse) -> Bool { return true }

public func filter(request: HTTPRequest, response: HTTPResponse, callback: (HTTPRequestFilterResult) -> ()) {
if requestFilter(req: request, res: response) {
callback(.continue(request, response))
} else {
callback(.halt(request, response))
}
}

public func filterHeaders(response: HTTPResponse, callback: (HTTPResponseFilterResult) -> ()) {
if responseFilterHeader(req: response.request, res: response) {
callback(.continue)
} else {
callback(.done)
}
}

public func filterBody(response: HTTPResponse, callback: (HTTPResponseFilterResult) -> ()) {
if responseFilterBody(req: response.request, res: response) {
callback(.continue)
} else {
callback(.done)
}
}
}
34 changes: 34 additions & 0 deletions Source/Pjango/Plugin/PCLogFilterPlugin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// PCLogFilterPlugin.swift
// Pjango-Project
//
// Created by 郑宇琦 on 2017/6/21.
//
//

import Foundation
import PerfectHTTP

open class PCLogFilterPlugin: PCHTTPFilterPlugin {

open override func requestFilter(req: HTTPRequest, res: HTTPResponse) -> Bool {
let url = req.getFullUrl()
let method = req.method
let host = req.serverAddress.host
let port = req.serverAddress.port
_pjango_core_log.info("Req: [\(method)][\(host)][\(port)]: \(url)")
return true
}

open override func responseFilterHeader(req: HTTPRequest, res: HTTPResponse) -> Bool {
let code = res.status.code
let url = req.getFullUrl()
let method = req.method
let host = req.serverAddress.host
let port = req.serverAddress.port
_pjango_core_log.info("Res: [\(code)][\(method)][\(host)][\(port)]: \(url)")
return true
}


}
File renamed without changes.
File renamed without changes.
12 changes: 0 additions & 12 deletions Source/Pjango/Runtime/PjangoDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ public protocol PjangoDelegate {
func registerModels() -> [PCModel]?

func setDB() -> PCDataBase?

func setRequestFilter() -> [(HTTPRequestFilter, HTTPFilterPriority)]?

func setResponseFilter() -> [(HTTPResponseFilter, HTTPFilterPriority)]?
}

public extension PjangoDelegate {
Expand All @@ -38,12 +34,4 @@ public extension PjangoDelegate {

func setDB() -> PCDataBase? { return nil }

func setRequestFilter() -> [(HTTPRequestFilter, HTTPFilterPriority)]? {
return [(PCLogFilter.init(), .high)]
}

func setResponseFilter() -> [(HTTPResponseFilter, HTTPFilterPriority)]? {
return [(PCLogFilter.init(), .high)]
}

}
31 changes: 16 additions & 15 deletions Source/Pjango/Runtime/PjangoRuntime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ public class PjangoRuntime {

//Pjango

TEMPLATES_DIR = "\(WORKSPACE_PATH)/\(TEMPLATES_DIR)"
LOG_PATH = "\(WORKSPACE_PATH)/\(LOG_PATH)"
PJANGO_TEMPLATES_DIR = "\(PJANGO_WORKSPACE_PATH)/\(PJANGO_TEMPLATES_DIR)"
PJANGO_LOG_PATH = "\(PJANGO_WORKSPACE_PATH)/\(PJANGO_LOG_PATH)"

//Django

BASE_DIR = "\(WORKSPACE_PATH)/\(BASE_DIR)"
STATIC_URL = "\(WORKSPACE_PATH)/\(STATIC_URL)"
PJANGO_BASE_DIR = "\(PJANGO_WORKSPACE_PATH)/\(PJANGO_BASE_DIR)"
PJANGO_STATIC_URL = "\(PJANGO_WORKSPACE_PATH)/\(PJANGO_STATIC_URL)"
}

internal static func _pjango_runtime_setUrls(delegate: PjangoDelegate) {
Expand All @@ -91,7 +91,14 @@ public class PjangoRuntime {

_pjango_runtime_plugin = delegate.registerPlugins() ?? []

_pjango_runtime_plugin.forEach { $0.run() }
_pjango_runtime_plugin.forEach {
if let filter = $0 as? PCHTTPFilterPlugin {
_pjango_runtime_server.setRequestFilters([(filter, filter.priority)])
_pjango_runtime_server.setResponseFilters([(filter, filter.priority)])
} else {
$0.run()
}
}

}

Expand Down Expand Up @@ -138,7 +145,7 @@ public class PjangoRuntime {
var leftConfig = [PCUrlConfig]()

_pjango_runtime_urls_list.forEach {
if $0.host == nil || $0.host == "" {
if $0.host == nil || $0.host == "" || $0.host == "default" {
allConfig.append($0)
} else {
leftConfig.append($0)
Expand Down Expand Up @@ -191,15 +198,9 @@ public class PjangoRuntime {
}

let routes = Routes.init(routeList)
let server = HTTPServer.init()
server.documentRoot = STATIC_URL
server.serverPort = SERVER_PORT
server.addRoutes(routes)

server.setRequestFilters(delegate.setRequestFilter() ?? [])
server.setResponseFilters(delegate.setResponseFilter() ?? [])

_pjango_runtime_server = server
_pjango_runtime_server.documentRoot = PJANGO_STATIC_URL
_pjango_runtime_server.serverPort = PJANGO_SERVER_PORT
_pjango_runtime_server.addRoutes(routes)
}

}
40 changes: 0 additions & 40 deletions Source/Pjango/Server/Filter/PCLogFilter.swift

This file was deleted.

6 changes: 3 additions & 3 deletions Source/Pjango/Utility/PCLog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class PCLog {
}

public func debug(_ msg: String) {
if DEBUG_LOG {
if PJANGO_LOG_DEBUG {
_pjango_core_log_msg("[\(Date.init().stringValue)][\(tag)][DEBUG]: \(msg)")
}
}
Expand All @@ -45,10 +45,10 @@ public class PCLog {

internal func _pjango_core_log_msg(_ msg: String) {
print(msg)
if LOG_TO_FILE {
if PJANGO_LOG_TO_FILE {
_pjang_core_log_queue.async {
if logFile == nil {
let file = File.init(LOG_PATH)
let file = File.init(PJANGO_LOG_PATH)
do {
try file.open(.readWrite)
logFile = file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public typealias PCViewParam = Dictionary<String, Any>
open class PCView: PCObject {

internal var _pjango_core_view_template_path: String {
return "\(TEMPLATES_DIR)/\(templateName ?? "")"
return "\(PJANGO_TEMPLATES_DIR)/\(templateName ?? "")"
}

internal var _pjango_core_view_param: PCViewParam {
Expand Down
File renamed without changes.

0 comments on commit 8a55b31

Please sign in to comment.