这里只是演示如何在ubuntu下编译运行Swift开发的Web后端项目。
项目代码来自Bluemix上提供的示例代码,如果你有账号,可以去自己的空间下载,没有的话,可以通过下面的地址下载:
http://download.csdn.net/detail/testcs_dn/9513395
编译环境安装配置请参考:Ubuntu 14 server安装Swift运行环境
环境配置好之后,将下载的示例代码解压出来:
主要代码是“main.swift”,内容如下:
/** * Copyright IBM Corporation 2016 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. **/ /** * Creates a simple HTTP server that listens for incoming connections on port 9080. * For each request receieved, the server simply sends a simple hello world message * back to the client. **/ #if os(Linux) import Glibc #else import Darwin #endif import Utils import Foundation // Create server socket let address = parseAddress() let server_sockfd = createSocket(address) // Listen on socket with queue of 5 listen(server_sockfd, 5) var active_fd_set = fd_set() print("Server is listening on port: \(address.port)\n") // Initialize the set of active sockets fdSet(server_sockfd, set: &active_fd_set) let FD_SETSIZE = Int32(1024) // Generate HTTP response // Get environment variables let environmentVars = NSProcessInfo.processInfo().environment var responseBody = "Hello from Swift on Linux!" + " " + " " + "" + "" for (variable, value) in environmentVars { responseBody += "\n" } responseBody += " Env Variable Value \(variable) \(value) " let httpResponse = "HTTP/1.0 200 OK\n" + "Content-Type: text/html\n" + "Content-Length: \(responseBody.length) \n\n" + responseBody var clientname = sockaddr_in() while true { // Block until input arrives on one or more active sockets var read_fd_set = active_fd_set; select(FD_SETSIZE, &read_fd_set, nil, nil, nil) // Service all the sockets with input pending for i in 0..关注打赏