Created
May 10, 2012 03:29
-
-
Save Nub/2650862 to your computer and use it in GitHub Desktop.
CocoaHTTPServer Router
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
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification | |
{ | |
// Initalize our http server | |
httpServer = [[HTTPServer alloc] init]; | |
[httpServer setType:@"_http._tcp."]; //Bounjour | |
[httpServer setPort:12345]; | |
[httpServer setConnectionClass:[HTTPRouter class]]; // User router | |
//Setup some sample routes | |
//Regex? o.O and query params | |
[HTTPRouter handleRoute:@"[tb]est" withBlock:^(HTTPMessage* message) | |
{ | |
return [NSString stringWithFormat:@"Test Params:%@", message.params]; | |
}]; | |
//Static route | |
[HTTPRouter handleRoute:@"test/hello" withBlock:^(HTTPMessage* message) | |
{ | |
return @"HAHAHAH WHAT ARE U DOING?"; | |
}]; | |
//Dynamic routes, stored as params :key | |
[HTTPRouter handleRoute:@"laughs/:times" withBlock:^(HTTPMessage* message) | |
{ | |
NSInteger times = [[message.params valueForKey:@"times"] integerValue]; | |
NSMutableString *response = [NSMutableString string]; | |
for (int i = 0; i < times; i++) | |
{ | |
[response appendString:@"HA"]; | |
} | |
return response; | |
}]; | |
// Start the server (and check for problems) | |
NSError *error; | |
BOOL success = [httpServer start:&error]; | |
NSAssert(success, @"Error starting HTTP Server: %@", error); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment