forked from ZekeSnider/Jared
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ZekeSnider#28 from ZekeSnider/zeke.http-server
Add REST API
- Loading branch information
Showing
336 changed files
with
37,603 additions
and
5,807 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import Foundation | ||
import Telegraph | ||
import JaredFramework | ||
|
||
class JaredWebServer: NSObject { | ||
static var DEFAULT_PORT = 3000 | ||
var defaults: UserDefaults! | ||
var server: Server! | ||
var port: Int! | ||
|
||
override init() { | ||
super.init() | ||
defaults = UserDefaults.standard | ||
server = Server() | ||
server.route(.POST, "message", JaredWebServer.handleMessageRequest) | ||
|
||
port = assignPort() | ||
|
||
defaults.addObserver(self, forKeyPath: "RestApiIsDisabled", options: .new, context: nil) | ||
updateServerState() | ||
} | ||
|
||
deinit { | ||
UserDefaults.standard.removeObserver(self, forKeyPath: "JaredIsDisabled") | ||
} | ||
|
||
// Attempt to pull the port number from the config | ||
func assignPort() -> Int { | ||
let filemanager = FileManager.default | ||
let appsupport = filemanager.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0] | ||
let supportDir = appsupport.appendingPathComponent("Jared") | ||
let configPath = supportDir.appendingPathComponent("config.json") | ||
do { | ||
// If config file does not exist, use default port | ||
guard filemanager.fileExists(atPath: configPath.path) else { | ||
return JaredWebServer.DEFAULT_PORT | ||
} | ||
|
||
//Read the JSON config file | ||
let jsonData = try! NSData(contentsOfFile: supportDir.appendingPathComponent("config.json").path, options: .mappedIfSafe) | ||
|
||
// If the JSON format is not as expected at all, use the default port | ||
guard let jsonResult = try? JSONSerialization.jsonObject(with: jsonData as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String:AnyObject] else { | ||
return JaredWebServer.DEFAULT_PORT | ||
} | ||
|
||
guard let serverConfig = jsonResult?["webserver"] as? [String : AnyObject] else { | ||
return JaredWebServer.DEFAULT_PORT | ||
} | ||
|
||
guard let configPort = serverConfig["port"] as? NSNumber else { | ||
return JaredWebServer.DEFAULT_PORT | ||
} | ||
|
||
return Int(truncating: configPort) | ||
} | ||
} | ||
|
||
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { | ||
if (keyPath == "RestApiIsDisabled") { | ||
updateServerState() | ||
} | ||
} | ||
|
||
func updateServerState() { | ||
if (defaults.bool(forKey: "RestApiIsDisabled")) { | ||
stop() | ||
} else { | ||
start() | ||
} | ||
} | ||
|
||
func start() { | ||
try? server.start(port: port) | ||
} | ||
|
||
func stop() { | ||
server.stop() | ||
} | ||
|
||
static func handleMessageRequest(request: HTTPRequest) -> HTTPResponse { | ||
// Attempt to decode the request body to the MessageRequest struct | ||
do { | ||
let parsedBody = try JSONDecoder().decode(MessageRequest.self, from: request.body) | ||
|
||
if let textBody = parsedBody.body as? TextBody { | ||
Jared.Send(textBody.message, to: parsedBody.recipient) | ||
return HTTPResponse() | ||
} | ||
else { | ||
return HTTPResponse(HTTPStatus(code: 400, phrase: "Bad Request"), headers: HTTPHeaders(), content: "Image body types are not supported yet.") | ||
} | ||
} catch { | ||
return HTTPResponse(HTTPStatus(code: 400, phrase: "Bad Request"), headers: HTTPHeaders(), content: error.localizedDescription) | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// MessageRequest.swift | ||
// Jared | ||
// | ||
// Created by Zeke Snider on 12/28/19. | ||
// Copyright © 2019 Zeke Snider. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import JaredFramework | ||
|
||
// Struct that defines what parameters are accepted in requests | ||
public struct MessageRequest: Decodable { | ||
public var body: MessageBody | ||
public var recipient: RecipientEntity | ||
|
||
enum CodingKeys : String, CodingKey { | ||
case body | ||
case recipient | ||
} | ||
|
||
enum ParameterError: Error { | ||
case runtimeError(String) | ||
} | ||
|
||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
|
||
if let textBody = try? container.decode(TextBody.self, forKey: .body) { | ||
self.body = textBody | ||
} else if let imageBody = try? container.decode(ImageBody.self, forKey: .body) { | ||
self.body = imageBody | ||
} else { | ||
throw ParameterError.runtimeError("the body parameter is incorrectly formatted") | ||
} | ||
|
||
if let person = try? container.decode(Person.self, forKey: .recipient) { | ||
self.recipient = person | ||
} else if let group = try? container.decode(Group.self, forKey: .recipient) { | ||
self.recipient = group | ||
} else { | ||
throw ParameterError.runtimeError("the recipient parameter is incorrectly formatted") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,8 @@ | |
"routes": { | ||
}, | ||
"webhooks": [ | ||
] | ||
], | ||
"webserver": { | ||
"port": 3000 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.