Simple https web server as command line application

63 views
Skip to first unread message

Karim

unread,
Jul 10, 2015, 7:31:40 AM7/10/15
to cocoaht...@googlegroups.com




I want to write a simple web server that handle https request. The ssl identity will be store in the keychain. I can read and write from there.

But how can I make it running forever application that wait for a http request and respond?

I am very new to Mac programming. Experience with iPhone and Java.

In CocoaHTTPServer, the samples are GUI applications. All with window and NSApplicationMain. 

I want to run my application as a daemon, forever in the background of the mac. 

When I use the source in a command line project it run and exits. 

If I add a runloop, it stays. Is it the right way to use it? 

The project uses a modified version of the sample SecureHttpServer and PostHttpServer.

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        [DDLog addLogger:[DDTTYLogger sharedInstance]];
        HTTPServer *httpServer = [[HTTPServer alloc] init];

        [httpServer setPort:TCP_PORT_NUMBER];
        [httpServer setConnectionClass:[MyHTTPConnection class]];

        // Serve files from the standard Sites folder
        NSString *docRoot = [@"~/Sites" stringByExpandingTildeInPath];
        DDLogCInfo(@"Setting document root: %@", docRoot);

        [httpServer setDocumentRoot:docRoot];

        NSError *error = nil;
        if(![httpServer start:&error])
        {
            DDLogCError(@"Error starting HTTP Server: %@", error);
        }

         [[NSRunLoop currentRunLoop] run];

    }
    return 0;
}


Reply all
Reply to author
Forward
0 new messages