入门
前提条件
开始之前,请确保已安装 Go 1.21 或更高版本。可以使用以下命令检查 Go 版本:
go version
网络应用快速入门
本节将演示如何创建并运行一个简单的 Hiboot 网络应用。让我们开始吧!
第一步:创建项目
mkdir hello-hiboot && cd hello-hiboot
go mod init hello-hiboot
go get github.com/hidevopsio/hiboot
第二步:编写代码
创建 main.go 文件:
package main
import (
"github.com/hidevopsio/hiboot/pkg/app"
"github.com/hidevopsio/hiboot/pkg/app/web"
"github.com/hidevopsio/hiboot/pkg/at"
"github.com/hidevopsio/hiboot/pkg/starter/actuator"
)
// Controller 是一个 RESTful 控制器,路径为 /
type Controller struct {
at.RestController
}
// Get 处理 GET / 请求
func (c *Controller) Get() string {
return "Hello, Hiboot!"
}
func main() {
web.NewApplication(new(Controller)).
SetProperty(app.ProfilesInclude, actuator.Profile).
Run()
}
第三步:运行应用
go run main.go
你会看到类似以下的输出:
______ ____________ _____
___ / / /__(_)__ /_______________ /_
__ /_/ /__ /__ __ \ __ \ __ \ __/
_ __ / _ / _ /_/ / /_/ / /_/ / /_ Hiboot Application Framework
/_/ /_/ /_/ /_.___/\____/\____/\__/ https://hiboot.hidevops.io
[INFO] Starting Hiboot web application on localhost with PID xxx
[INFO] Mapped "/" onto main.Controller.Get()
[INFO] Mapped "/health" onto actuator.healthController.Get()
[INFO] Hiboot started on port(s) http://localhost:8080
第四步:测试接口
curl http://localhost:8080/
输出:
Hello, Hiboot!
健康检查接口也已自动可用:
curl http://localhost:8080/health
输出:
{"status":"UP"}
集成 Swagger 快速入门
Hiboot 支持使用 Swagger 自动生成 API 文档。以下是增强版示例:
package main
import (
"github.com/hidevopsio/hiboot/pkg/app"
"github.com/hidevopsio/hiboot/pkg/app/web"
"github.com/hidevopsio/hiboot/pkg/at"
"github.com/hidevopsio/hiboot/pkg/starter/actuator"
"github.com/hidevopsio/hiboot/pkg/starter/swagger"
)
// Controller 是一个 RESTful 控制器
type Controller struct {
at.RestController
at.RequestMapping `value:"/"`
}
// Get 处理 GET / 请求,包含 Swagger 文档注解
func (c *Controller) Get(at struct {
at.GetMapping `value:"/"`
at.Operation `id:"helloWorld" description:"Hello World API"`
at.Produces `values:"text/plain"`
Responses struct {
StatusOK struct {
at.Response `code:"200" description:"成功"`
at.Schema `type:"string" description:"返回问候消息"`
}
}
}) string {
return "Hello, Hiboot!"
}
func main() {
// 注册 Swagger API 信息
app.Register(swagger.ApiInfoBuilder().
Title("Hello Hiboot API").
Description("一个简单的 Hiboot 网络应用"))
web.NewApplication(new(Controller)).
SetProperty(app.ProfilesInclude, swagger.Profile, actuator.Profile).
Run()
}
运行后,访问 http://localhost:8080/swagger/ 即可查看交互式 API 文档。
命令行应用快速入门
Hiboot 同样支持命令行应用,并可使用依赖注入功能。
创建命令行应用
创建 main.go 文件:
package main
import (
"fmt"
"github.com/hidevopsio/hiboot/pkg/app"
"github.com/hidevopsio/hiboot/pkg/app/cli"
)
// rootCommand 定义命令行命令
type rootCommand struct {
cli.RootCommand
to string
}
func newRootCommand() *rootCommand {
c := new(rootCommand)
c.Use = "hello"
c.Short = "hello 命令"
c.Long = "运行 hello 命令以快速入门"
c.Example = `
hello -h : 帮助
hello -t John : 向 John 问好
`
c.PersistentFlags().StringVarP(&c.to, "to", "t", "world", "例如 --to=world 或 -t world")
return c
}
// Run 执行命令
func (c *rootCommand) Run(args []string) error {
fmt.Printf("Hello, %v\n", c.to)
return nil
}
func main() {
cli.NewApplication(newRootCommand).
SetProperty(app.BannerDisabled, true).
Run()
}
运行命令行应用
go run main.go
输出:
Hello, world
编译并运行
go build -o hello
./hello --help
输出:
运行 hello 命令以快速入门
Usage:
hello [flags]
Flags:
-h, --help able for hello
-t, --to string 例如 --to=world 或 -t world (default "world")
向 Hiboot 问好:
./hello --to Hiboot
输出:
Hello, Hiboot
下一步
现在你已经创建了第一个 Hiboot 应用,可以继续探索以下主题: