Ladoga 是一个轻量级且易于使用的 HTTP 框架,使您能够使用 Objective-C 编写网络应用程序。它提供简单的 API,让您能够专注于业务逻辑,而无需花费时间在底层细节上。
开始使用 Ladoga 框架进行开发的简单方法是通过 CocoaPods 安装。只需将其添加到您的 Podfile
中
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
pod 'Ladoga'
基本示例很简单,因此您现在就可以开始制作您的网络应用程序了。
#import <Foundation/Foundation.h>
#import <Ladoga/Ladoga.h>
@interface MyServerExample : NSObject
- (void)start;
@end
#import "MyServerExample.h"
@implementation MyServerExample
- (void)start {
LDHTTPServer *server = [[LDHTTPServer alloc] initWithAddress:@"127.0.0.1"
andPort:8081];
LDHTTPRequestHandler *mainPageHandler = [[LDHTTPRequestHandler alloc] initWithHandler:self
selector:@selector(mainPage:)
methods:@[ @(LDHTTPMethodGET) ]];
[server addRequestHandler:mainPageHandler forPath:@"/index.html"];
[server startWithRunLoop:CFRunLoopGetMain()];
CFRunLoopRun();
}
- (LDHTTPResponse *)mainPage:(LDHTTPRequest *)request {
LDHTTPResponse *response = [[LDHTTPResponse alloc] init];
[response addValue:@"text/html;charset=utf-8" forHTTPHeader:@"Content-Type"];
response.body = @"<html><head><title>My Example</title></head><body>Hello, world!</body></html>";
return response;
}
@end
Ladoga 还通过 LDHTMLTemplate
类提供模板系统。您可以使用一些变量指定模板,然后使用实际值进行渲染。
假设我们有一个以下模板,包含 PageTitle
和 Username
变量
<html>
<head><title>{{ PageTitle }}</title></head>
<body>Hello, {{ Username }}!</body>
</html>
因此,要渲染此模板,我们必须使用 renderTemplateAtPath:withParameters:
类方法
NSDictionary *params = @{ @"PageTitle": @"Test Page",
@"Username": @"John" };
NSString *templatePath = [[[NSBundle bundleForClass:[self class]] resourcePath] stringByAppendingPathComponent:@"index.html"];
NSString *resultHTML = [LDHTMLTemplate renderTemplateAtPath:templatePath
withParameters:params];
结果,我们得到一个包含渲染的 HTML 页面代码的字符串
<html>
<head><title>Test Page</title></head>
<body>Hello, John!</body>
</html>
如果您想帮助开发 Ladoga,欢迎您的合并请求。请遵循 git-flow
注释,并在创建合并请求之前确保所有测试都通过。
Ladoga 的源代码完全由单元测试覆盖。这意味着您可以立即检查您的更改是否存在回归。请为每一行新代码编写测试。