写一个Go HTTP服务器
-
打开一个新的终端窗口,但让守护进程运行的窗口离开。此窗口将用于运行UniK CLI命令。
-
使用文本编辑器创建文件httpd.go。将以下代码复制并粘贴到该文件中:
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "my first unikernel!")
}
- 尝试运行此代码
go run http.go
。访问http:// localhost:8080 /看到服务器正在运行。 - 我们需要创建一个虚拟
Godeps
文件。告诉Go编译器如何构建Go项目及其依赖关系是必要的。幸运的是,在这个例子中,我们的项目没有依赖关系,我们可以在Godeps
没有安装的情况下填写一个简单的文件godep
。注意:对于具有导入依赖关系的Go项目和嵌套包,您将需要安装Godeps并运行GO15VENDOREXPERIMENT=1 godep save ./...
在项目中。有关详细信息,请参阅使用UniK编译Go应用程序。
- 要创建虚拟Godeps文件,请创建
Godeps
与目录相同的文件夹httpd.go
。在里面,创建一个名为Godeps.json
并粘贴以下内容的文件:
{
"ImportPath": "my_httpd",
"GoVersion": "go1.6",
"GodepVersion": "v63",
"Packages": [
"./.."
],
"Deps": [
{
"ImportPath": "github.com/cf-unik/unik/docs/examples",
"Rev": "f8cc0dd435de36377eac060c93481cc9f3ae9688"
}
]
}
- For the purposes of this example, that matters here is
my_httpd
. It instructs the go compiler that the project should be installed from$GOPATH/src/my_httpd
.
- Great! Now we're ready to compile this code to a unikernel.