在unik中,写一个Go HTTP服务器

写一个Go HTTP服务器
  1. 打开一个新的终端窗口,但让守护进程运行的窗口离开。此窗口将用于运行UniK CLI命令。

  2. 使用文本编辑器创建文件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!")
}
  1. 尝试运行此代码go run http.go访问http:// localhost:8080 /看到服务器正在运行。
  2. 我们需要创建一个虚拟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.
  1. Great! Now we're ready to compile this code to a unikernel.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值