Last active
September 25, 2015 06:32
-
-
Save segabor/362d53f049d42d50eb81 to your computer and use it in GitHub Desktop.
Revisions
-
segabor revised this gist
Sep 25, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 { CChar(bitPattern: $0) } ) ) r = FCGI_Accept() } -
segabor revised this gist
Sep 25, 2015 . 1 changed file with 1 addition and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -50,7 +50,6 @@ http://localhost/swift/hello ***/ import Darwin // // 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.utf8.map { l in CChar(bitPattern: l) } ) ) r = FCGI_Accept() } -
segabor revised this gist
Sep 22, 2015 . 1 changed file with 32 additions and 22 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,67 +1,77 @@ /*** Swift can say hello to your browser! Requirements ------------ * OSX Yosemite or newer * Xcode 7, Swift 2 * Homebrew ** packages: fastcgi, fcgi-swpawn, nginx packages 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 # Configure Nginx front-end --- ... --- ... --- ... --- ... --- location /swift { root /usr/local/var/www/; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.html; fastcgi_param SCRIPT_FILENAME /cgi-bin/fcgi$fastcgi_script_name; include fastcgi_params; } --- ... --- ... --- ... --- ... --- # 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 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" + -
segabor created this gist
Sep 22, 2015 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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)