Skip to content

Commit

Permalink
fix url dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
enums committed Jun 27, 2017
1 parent 8a55b31 commit 21b931d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 38 deletions.
12 changes: 12 additions & 0 deletions Source/Pjango/Pjango/Global.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Global.swift
// Pjango-Dev
//
// Created by 郑宇琦 on 2017/6/27.
//
//

import Foundation

public let PJANGO_HOST_DEFAULT = "_pjango_default"

22 changes: 9 additions & 13 deletions Source/Pjango/Pjango/Pjango.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,19 @@ import PerfectHTTP

public typealias PCUrlHandle = RequestHandler

public func pjangoUrl(_ url: String, host: String? = nil, name: String? = nil, handle: @escaping (() -> PCUrlHandle)) -> PCUrlConfig {
return pjangoUrl(url, host: host, name: name, handle: handle())
public func pjangoUrl(_ url: String, name: String? = nil, handle: @escaping (() -> PCUrlHandle)) -> PCUrlConfig {
return pjangoUrl(url, name: name, handle: handle())
}

public func pjangoUrl(_ url: String, host: String? = nil, name: String? = nil, handle: @escaping PCUrlHandle) -> PCUrlConfig {
return PCUrlConfig(url: url, handle: handle, host: host, name: name)
public func pjangoUrl(_ url: String, name: String? = nil, handle: @escaping PCUrlHandle) -> PCUrlConfig {
return PCUrlConfig(url: url, handle: handle, name: name)
}

public func pjangoUrlReverse(name: String) -> String? {
guard let config = PjangoRuntime._pjango_runtime_urls_name2config[name] else {
public func pjangoUrlReverse(host: String, name: String) -> String? {
guard let config = PjangoRuntime._pjango_runtime_urls_name2config["\(host)@\(name)"] else {
return nil
}
if let host = config.host {
return "http://\(host)/\(config.url)"
} else {
return "/\(config.url)"
}
return "http://\(host)/\(config.url)"
}

public func pjangoUrlConfigReverse(name: String) -> PCUrlConfig? {
Expand All @@ -43,8 +39,8 @@ public func pjangoHttpResponse(_ handle: @escaping RequestHandler) -> PCUrlHandl
return handle
}

public func pjangoHttpRedirect(name: String) -> PCUrlHandle? {
guard let url = pjangoUrlReverse(name: name) else {
public func pjangoHttpRedirect(host: String = "default", name: String) -> PCUrlHandle? {
guard let url = pjangoUrlReverse(host: host, name: name) else {
return nil
}
return pjangoHttpRedirect(url: url)
Expand Down
51 changes: 29 additions & 22 deletions Source/Pjango/Runtime/PjangoRuntime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class PjangoRuntime {

public static var _pjango_runtime_urls_name2config = Dictionary<String, PCUrlConfig>()

public static var _pjango_runtime_urls_list = Array<PCUrlConfig>()
public static var _pjango_runtime_urls_list = Array<(String, PCUrlConfig)>()

public static var _pjango_runtime_models_name2meta = Dictionary<String, PCMetaModel>()

Expand Down Expand Up @@ -77,11 +77,10 @@ public class PjangoRuntime {
guard configList.count > 0 else {
return
}
for var config in configList {
config.host = host
_pjango_runtime_urls_list.append(config)
for config in configList {
_pjango_runtime_urls_list.append((host, config))
if let name = config.name {
_pjango_runtime_urls_name2config[name] = config
_pjango_runtime_urls_name2config["\(host)@\(name)"] = config
}
}
}
Expand Down Expand Up @@ -140,32 +139,34 @@ public class PjangoRuntime {
internal static func _pjango_runtime_setServer(delegate: PjangoDelegate) {


var allConfig = [PCUrlConfig]()
//url->config
var defaultConfig = [String: PCUrlConfig]()

var leftConfig = [PCUrlConfig]()
//(posts, config)
var userConfig = [(String, PCUrlConfig)]()

_pjango_runtime_urls_list.forEach {
if $0.host == nil || $0.host == "" || $0.host == "default" {
allConfig.append($0)
_pjango_runtime_urls_list.forEach { (host, config) in
if host == PJANGO_HOST_DEFAULT {
defaultConfig[config.url] = config
} else {
leftConfig.append($0)
userConfig.append((host, config))
}
}

//[host: [url: config]] -> [url: [(host, config)]]
//[host: [url: config]]
var hostUrlConfig = [String: [String: PCUrlConfig]]()

leftConfig.forEach { config in
if let host = config.host {
if hostUrlConfig[host] == nil {
hostUrlConfig[host] = [String: PCUrlConfig]()
}
hostUrlConfig[host]![config.url] = config
userConfig.forEach { (host, config) in
if hostUrlConfig[host] == nil {
hostUrlConfig[host] = [String: PCUrlConfig]()
}
hostUrlConfig[host]![config.url] = config
}

//[url: [(host, config)]]
var urlHostConfig = [String: [(String, PCUrlConfig)]]()

for (host, urlConfig) in hostUrlConfig {
for (url, config) in urlConfig {
if urlHostConfig[url] == nil {
Expand All @@ -176,8 +177,8 @@ public class PjangoRuntime {
}
}

var routeList = allConfig.map { config in
Route.init(uri: config.url) { req, res in
var routeList = defaultConfig.map { (url, config) in
Route.init(uri: url) { req, res in
config.handle(req, res)
res.completed()
}
Expand All @@ -186,9 +187,15 @@ public class PjangoRuntime {
for (url, configList) in urlHostConfig {
let route = Route.init(uri: url) { req, res in
guard let index = configList.index(where: { $0.0 == req.header(.host) }) else {
res.status = .notFound
res.completed()
return
if let dc = defaultConfig[url] {
dc.handle(req, res)
res.completed()
return
} else {
res.status = .notFound
res.completed()
return
}
}
let (_, config) = configList[index]
config.handle(req, res)
Expand Down
4 changes: 1 addition & 3 deletions Source/Pjango/Server/PCUrlConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ public struct PCUrlConfig {

public var url: String
public var handle: RequestHandler
public var host: String?
public var name: String?

public init(url: String, handle: @escaping RequestHandler, host: String? = nil, name: String? = nil) {
public init(url: String, handle: @escaping RequestHandler, name: String? = nil) {
self.url = url
self.handle = handle
self.host = host
self.name = name
}

Expand Down

0 comments on commit 21b931d

Please sign in to comment.