Skip to content

Instantly share code, notes, and snippets.

@segabor
Last active September 25, 2015 06:32
Show Gist options
  • Save segabor/362d53f049d42d50eb81 to your computer and use it in GitHub Desktop.
Save segabor/362d53f049d42d50eb81 to your computer and use it in GitHub Desktop.

Revisions

  1. segabor revised this gist Sep 25, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fcgi_app.swift
    Original file line number Diff line number Diff line change
    @@ -79,7 +79,7 @@ while r >= 0 {
    "<h1>FastCGI Hello! (Swift, fcgi_stdio library)</h1></body></html>" +
    "\r\n"

    FCGI_puts( UnsafeMutablePointer<CChar>( str.utf8.map { l in CChar(bitPattern: l) } ) )
    FCGI_puts( UnsafeMutablePointer<CChar>( str.utf8.map { CChar(bitPattern: $0) } ) )
    r = FCGI_Accept()
    }

  2. segabor revised this gist Sep 25, 2015. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions fcgi_app.swift
    Original file line number Diff line number Diff line change
    @@ -50,7 +50,6 @@ http://localhost/swift/hello
    ***/

    import Darwin
    import Foundation

    //
    // The ugly part -- attach fastcgi library
    @@ -80,8 +79,7 @@ while r >= 0 {
    "<h1>FastCGI Hello! (Swift, fcgi_stdio library)</h1></body></html>" +
    "\r\n"

    FCGI_puts( UnsafeMutablePointer<CChar>( (str as NSString).UTF8String) )

    FCGI_puts( UnsafeMutablePointer<CChar>( str.utf8.map { l in CChar(bitPattern: l) } ) )
    r = FCGI_Accept()
    }

  3. segabor revised this gist Sep 22, 2015. 1 changed file with 32 additions and 22 deletions.
    54 changes: 32 additions & 22 deletions fcgi_app.swift
    Original file line number Diff line number Diff line change
    @@ -1,67 +1,77 @@
    /**
    /***

    Lot of comments go below
    Swift can say hello to your browser!

    Requirements:
    Requirements
    ------------

    * OSX Yosemite
    * OSX Yosemite or newer
    * Xcode 7, Swift 2
    * Homebrew
    * fastcgi, fcgi-swpawn, nginx packages
    ** packages: fastcgi, fcgi-swpawn, nginx packages


    Compile code without Xcode
    Make it work
    ------------

    # Download and compile the code

    swiftc -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk fcgi_app.swift -o fcgi_app


    Nginx config
    ============
    # Configure Nginx front-end

    -----
    --- ... --- ... --- ... --- ... ---
    location /swift {
    root /Users/segabor/Workspace/swift_workspace;
    root /usr/local/var/www/;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.html;
    fastcgi_param SCRIPT_FILENAME /cgi-bin/fcgi_app$fastcgi_script_name;
    fastcgi_param SCRIPT_FILENAME /cgi-bin/fcgi$fastcgi_script_name;
    include fastcgi_params;
    }
    -----
    --- ... --- ... --- ... --- ... ---


    FCGI Spawner script
    ===================
    # Spawn your web app as a FCGI daemon

    -----
    --- ... --- ... --- ... --- ... ---
    /usr/local/bin/spawn-fcgi \
    -a 127.0.0.1 -p 9000 \
    -u segabor -g staff \
    -f ./fcgi_app \
    -P ./fcgi_app.pid
    --- ... --- ... --- ... --- ... ---

    # Open your browser and enjoy the result

    -----
    http://localhost/swift/hello

    */

    ***/

    import Darwin
    import Foundation

    //
    // The ugly part -- attach fastcgi library
    //
    let handle = dlopen(
    "/usr/local/Cellar/fcgi/2.4.0/lib/libfcgi.dylib",
    RTLD_NOW)
    let sym = dlsym(handle, "FCGI_Accept")

    // lookup fcgi functions and map them to Swift types
    //
    typealias FCGI_Accept_T = @convention(c) (Void)->CInt
    let FCGI_Accept : ()->CInt = unsafeBitCast(sym, FCGI_Accept_T.self)

    let sym2 = dlsym(handle, "_fcgi_sF")
    let FCGI_stdin = UnsafeMutablePointer<Int8>(sym2)
    let FCGI_stdout = FCGI_stdin.successor()

    let sym3 = dlsym(handle, "FCGI_puts")

    typealias FCGI_Puts_T = @convention(c) (UnsafeMutablePointer<CChar>)->CInt
    let FCGI_puts : FCGI_Puts_T = unsafeBitCast(sym3, FCGI_Puts_T.self)

    //
    // Let the fun begin
    //
    var r = FCGI_Accept()
    while r >= 0 {
    let str = "Content-type: text/html\r\n" +
  4. segabor created this gist Sep 22, 2015.
    78 changes: 78 additions & 0 deletions fcgi_app.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    /**

    Lot of comments go below

    Requirements:

    * OSX Yosemite
    * Xcode 7, Swift 2
    * Homebrew
    * fastcgi, fcgi-swpawn, nginx packages


    Compile code without Xcode

    swiftc -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk fcgi_app.swift -o fcgi_app


    Nginx config
    ============

    -----
    location /swift {
    root /Users/segabor/Workspace/swift_workspace;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.html;
    fastcgi_param SCRIPT_FILENAME /cgi-bin/fcgi_app$fastcgi_script_name;
    include fastcgi_params;
    }
    -----

    FCGI Spawner script
    ===================

    -----
    /usr/local/bin/spawn-fcgi \
    -a 127.0.0.1 -p 9000 \
    -u segabor -g staff \
    -f ./fcgi_app \
    -P ./fcgi_app.pid

    -----

    */

    import Darwin
    import Foundation

    let handle = dlopen(
    "/usr/local/Cellar/fcgi/2.4.0/lib/libfcgi.dylib",
    RTLD_NOW)
    let sym = dlsym(handle, "FCGI_Accept")

    typealias FCGI_Accept_T = @convention(c) (Void)->CInt
    let FCGI_Accept : ()->CInt = unsafeBitCast(sym, FCGI_Accept_T.self)

    let sym2 = dlsym(handle, "_fcgi_sF")
    let FCGI_stdin = UnsafeMutablePointer<Int8>(sym2)
    let FCGI_stdout = FCGI_stdin.successor()

    let sym3 = dlsym(handle, "FCGI_puts")

    typealias FCGI_Puts_T = @convention(c) (UnsafeMutablePointer<CChar>)->CInt
    let FCGI_puts : FCGI_Puts_T = unsafeBitCast(sym3, FCGI_Puts_T.self)

    var r = FCGI_Accept()
    while r >= 0 {
    let str = "Content-type: text/html\r\n" +
    "\r\n" +
    "<!DOCTYPE html><html><head><title>Hello World</title></head><body>" +
    "<h1>FastCGI Hello! (Swift, fcgi_stdio library)</h1></body></html>" +
    "\r\n"

    FCGI_puts( UnsafeMutablePointer<CChar>( (str as NSString).UTF8String) )

    r = FCGI_Accept()
    }

    dlclose(handle)