Created
September 4, 2013 06:08
-
-
Save fjolnir/6433280 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#import <Foundation/Foundation.h> | |
#import <HTTPKit/HTTP.h> | |
#import <dispatch/dispatch.h> | |
int main(int argc, const char * argv[]) | |
{ | |
@autoreleasepool { | |
HTTP *http = [HTTP new]; | |
http.enableDirListing = YES; | |
// Simple "Hello you!" pong | |
[http handleGET:@"/hello/*" | |
with:^(HTTPConnection *connection, NSString *name) { | |
return [NSString stringWithFormat:@"Hello %@!", name]; | |
}]; | |
// Simplified login example | |
[http handleGET:@"/login" | |
with:^(HTTPConnection *connection) { | |
return @"<form method=\"post\" action=\"/login\">" | |
@"<label for=\"username\">Name:</label>" | |
@"<input name=\"username\" type=\"text\">" | |
@"<label for=\"password\">Password:</label>" | |
@"<input name=\"password\" type=\"password\">" | |
@"<input type=\"submit\" value=\"Sign in\">" | |
@"</form>"; | |
}]; | |
[http handlePOST:@"/login" with:^(HTTPConnection *connection) { | |
NSLog(@"logging in user: %@ with password: %@", | |
[connection requestBodyVar:@"username"], | |
[connection requestBodyVar:@"password"]); | |
return @"Welcome! I trust you so I didn't even check your password."; | |
}]; | |
// WebSocket | |
[http handleWebSocket:^id (HTTPConnection *connection) { | |
if(!connection.isOpen) { | |
NSLog(@"Socket closed"); | |
return nil; | |
} | |
NSLog(@"WebSocket message '%@' from %ld", connection.requestBody, connection.remoteIp); | |
if([connection.requestBody isEqual:@"exit"]) | |
[connection close]; | |
return [connection.requestBody capitalizedString]; | |
}]; | |
[http listenOnPort:8081 onError:^(id reason) { | |
NSLog(@"Error starting server: %@", reason); | |
exit(1); | |
}]; | |
dispatch_main(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment