跳到主要内容

Cvgo Web 是一个超轻量级的 Web 开发框架,基于标准库的 net/http。其所有代码都在 provider/httpserver 包中,如果你想自己定制 Web 服务,可以选用。

快速启动

启动一个 Http 服务

main.go
package main

import (
"github.com/textthree/cvgoweb"
"github.com/textthree/cvgoweb/cvgohttp"
)

func main() {
cvgohttp.Run(Router, ":9000")
}

// 路由定义
func Router(engine *httpserver.Engine) {
engine.Get("/", Index)
}

// 路由响应函数
func Index(ctx *httpserver.Context) {
ctx.Resp.Text("Hello World!")
}

编写如上代码 go run main.go 运行,然后浏览器访问 http://localhost:9000 查看 Hello World!

通过 goodhttp.Run(Router, ":8080") 启动服务时,如果在配置文件 app.yaml 中配置了 server.http-port 项 ,则可以省略第二个参数,如果同时在配置文件和代码中都指定了服务端口,会优先使用代码中指定的端口。

配置示例:

app.yaml
server:
http-port: 8080