《Swift入门》ubuntu下编译运行Swift开发的Web后端示例

简介: 这里只是演示如何在ubuntu下编译运行Swift开发的Web后端项目。项目代码来自Bluemix上提供的示例代码,如果你有账号,可以去自己的空间下载,没有的话,可以通过下面的地址下载:http://download.

这里只是演示如何在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 = "<html><body>Hello from Swift on Linux!" +
  "<br />" +
  "<br />" +
  "<table border=\"1\">" +
  "<tr><th>Env Variable</th><th>Value</th></tr>"

for (variable, value) in environmentVars {
    responseBody += "<tr><td>\(variable)</td><td>\(value)</td></tr>\n"
}

responseBody += "</table></body></html>"

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..<FD_SETSIZE {
    if fdIsSet(i,set: &read_fd_set) {
      if i == server_sockfd {
        // Connection request on original socket
        var size = sizeof(sockaddr_in)
        // Accept request and assign socket
        withUnsafeMutablePointers(&clientname, &size) { up1, up2 in
          var client_sockfd = accept(server_sockfd,
            UnsafeMutablePointer(up1),
            UnsafeMutablePointer(up2))
            print("Received connection request from client: " + String(inet_ntoa (clientname.sin_addr)) + ", port " + String(UInt16(clientname.sin_port).bigEndian))
            fdSet(client_sockfd, set: &active_fd_set)
        }
      }
      else {
        // Send HTTP response back to client
        write(i, httpResponse, httpResponse.characters.count)
        // Close client socket
        close(i)
        fdClr(i, set: &active_fd_set)
      }
    }
  }
}
可通过以下命令编译并启动服务:

root@ubuntu:/home/aven/swifttrans# swift build
root@ubuntu:/home/aven/swifttrans# .build/debug/Server
Server is listening on port: 9080

编译时,可能会出现以下错误:

error: unable to invoke subcommand: /usr/bin/swift-build (No such file or directory)

服务启动成功后,我们就可以通过浏览器访问了,结果如下图:




目录
相关文章
|
5月前
|
存储 Ubuntu 自动驾驶
运行Udacity的MPC控制项目指南(project_10)在Ubuntu 18.04环境下
以上步骤应该能够帮助您成功设置并运行Udacity MPC控制项目,在此过程中您将学习如何应用模型预测控制理论去指导车辆沿着轨迹自主驾驶,在模拟环境下测试其效果。这个过程不但涵盖了理论知识也有实践操作,对于学习自动驾驶车辆控制系统非常有帮助。
198 15
|
8月前
|
Ubuntu 定位技术 TensorFlow
源码编译安装ROCm以运行tensorflow-rocm(适用于Ubuntu 23.04)
总结一番,完成这趟奇妙的技术之旅后,乐趣多多,还能享受 tensorflow-rocm 带来的便利和速度。这趟旅程需要耐心,勇气,以及对技术的热爱。朋友,做好准备,让你的Ubuntu系统展翅高飞吧!
463 9
|
9月前
|
Ubuntu 搜索推荐 Linux
详解Ubuntu的strings与grep命令:Linux开发的实用工具。
这就是Ubuntu中的strings和grep命令,透明且强大。我希望你喜欢这个神奇的世界,并能在你的Linux开发旅程上,通过它们找到你的方向。记住,你的电脑是你的舞台,在上面你可以做任何你想做的事,只要你敢于尝试。
454 32
|
9月前
|
Ubuntu 安全
Ubuntu中遇到"无法创建临时文件"/tmp/..."在普通或root用户运行apt-get update时的错误解决办法
这些步骤当然不能涵盖所有可能出现问题的场合,但是能覆盖大多数常见的情景。希望这些信息能帮到你解决"无法创建临时文件"/tmp/..."这样的问题。不过,请记住,因为修改系统文件和目录的权限,清理临时文件,或者运行磁盘检查,都可能对系统造成影响,所以执行这些操作前要先确认这是安全的,必要时最好先做好数据备份。
634 10
|
11月前
|
监控 Ubuntu Linux
ubuntu中设置开机自动运行的(sudo)指令
通过合理选择和配置上述方法,您可以确保在系统启动时自动执行所需的命令,并具备sudo权限,从而提高系统自动化管理的能力。
1812 8
|
Java 开发者 微服务
Spring Boot 入门:简化 Java Web 开发的强大工具
Spring Boot 是一个开源的 Java 基础框架,用于创建独立、生产级别的基于Spring框架的应用程序。它旨在简化Spring应用的初始搭建以及开发过程。
866 7
Spring Boot 入门:简化 Java Web 开发的强大工具
|
机器学习/深度学习 移动开发 JavaScript
Web实时通信的学习之旅:SSE(Server-Sent Events)的技术详解及简单示例演示
Web实时通信的学习之旅:SSE(Server-Sent Events)的技术详解及简单示例演示
6625 0
|
前端开发 JavaScript 开发者
探索现代Web前端技术:React框架入门
【10月更文挑战第9天】 探索现代Web前端技术:React框架入门
|
开发框架 缓存 Ubuntu
dotnet开发框架+ubuntu防火墙命令+win11设置自动登录+阿里云短信发送限制
dotnet开发框架+ubuntu防火墙命令+win11设置自动登录+阿里云短信发送限制
216 3
|
Ubuntu Linux Python
如何利用wsl-Ubuntu里conda用来给Windows的PyCharm开发
如何在WSL(Windows Subsystem for Linux)的Ubuntu环境中使用conda虚拟环境来为Windows上的PyCharm开发设置Python解释器。
2135 1