Giter VIP home page Giter VIP logo

httpkit's Introduction

HTTPKit

HTTPKit is a lightweight framework for building webservers in Objective-C or Tranquil.

Basic usage

#import <HTTPKit.h>

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        HTTP *http = [HTTP new];
        // Simple "Hello you!" pong
        [http handleGET:@"/hello/*"
                   with:^(HTTPConnection *connection, NSString *name) {
            return [NSString stringWithFormat:@"Hello %@!", userName];
        }];
        
        // 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.";
        }];
        [http listenOnPort:8081 onError:^(id reason) {
            fprintf(stderr, "Error starting server: %s", [reason UTF8String]);
            exit(1);
        }];
        [[NSRunLoop mainRunLoop] runUntilDate:[NSDate distantFuture]];
    }
    return 0;
}

Or written in tranquil as:

import "HTTPKit"
import "html"
t = Tag
HTTP new handleGET: "/hello/*" with: `conn, name| "Hello «name»!"`;
         handleGET: "/login" with: { conn |
             t html: "Log in" :[
                 t :#form :[
                     t :#label :"Name:" :{ #for  => #username },
                     t :#input :nil     :{ #name => #username, #type => #text },
                     t :#label :"Password:" :{ #for  => #password },
                     t :#input :nil         :{ #name => #password, #type => #password },
                     t :#input :nil :{ #value => "Sign in", #type => #submit  }
                 ] :{ #method => #post }
             ]
         };
        handlePOST: "/login" with: { conn |
             "Logging in user: «conn requestBodyVar: #username» with password: «conn requestBodyVar: #password»" print
             ^"Welcome! I trust you so I didn't even check your password."
        };
        listenOnPort: 8080 onError: { reason | "Error starting server: «reason»" print. ^^1 }
        
NSRunLoop mainRunLoop runUntilDate: NSDate distantFuture

httpkit's People

Contributors

fjolnir avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.