Getting started
Prerequisites
Before you begin, ensure you have Go 1.21 or later installed. You can check your Go version with:
go version
Quick start web application
This section will show you how to create and run a simple Hiboot web application. Let’s get started!
Step 1: Create a new project
mkdir hello-hiboot && cd hello-hiboot
go mod init hello-hiboot
go get github.com/hidevopsio/hiboot
Step 2: Write the code
Create 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 is a RESTful controller with path /
type Controller struct {
at.RestController
}
// Get handles GET /
func (c *Controller) Get() string {
return "Hello, Hiboot!"
}
func main() {
web.NewApplication(new(Controller)).
SetProperty(app.ProfilesInclude, actuator.Profile).
Run()
}
Step 3: Run the application
go run main.go
You’ll see output like:
______ ____________ _____
___ / / /__(_)__ /_______________ /_
__ /_/ /__ /__ __ \ __ \ __ \ __/
_ __ / _ / _ /_/ / /_/ / /_/ / /_ 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
Step 4: Test the API
curl http://localhost:8080/
Output:
Hello, Hiboot!
The health check endpoint is also available:
curl http://localhost:8080/health
Output:
{"status":"UP"}
Quick start with Swagger
Hiboot supports automatic API documentation with Swagger. Here’s an enhanced example:
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 is a RESTful controller
type Controller struct {
at.RestController
at.RequestMapping `value:"/"`
}
// Get handles GET / with Swagger documentation
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:"Success"`
at.Schema `type:"string" description:"Returns hello message"`
}
}
}) string {
return "Hello, Hiboot!"
}
func main() {
// Register Swagger API info
app.Register(swagger.ApiInfoBuilder().
Title("Hello Hiboot API").
Description("A simple Hiboot web application"))
web.NewApplication(new(Controller)).
SetProperty(app.ProfilesInclude, swagger.Profile, actuator.Profile).
Run()
}
After running, visit http://localhost:8080/swagger/ to see the interactive API documentation.
Quick start CLI application
Hiboot also supports command-line applications with dependency injection.
Create a CLI application
Create main.go:
package main
import (
"fmt"
"github.com/hidevopsio/hiboot/pkg/app"
"github.com/hidevopsio/hiboot/pkg/app/cli"
)
// rootCommand defines the CLI command
type rootCommand struct {
cli.RootCommand
to string
}
func newRootCommand() *rootCommand {
c := new(rootCommand)
c.Use = "hello"
c.Short = "hello command"
c.Long = "Run hello command for getting started"
c.Example = `
hello -h : help
hello -t John : say hello to John
`
c.PersistentFlags().StringVarP(&c.to, "to", "t", "world", "e.g. --to=world or -t world")
return c
}
// Run executes the command
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()
}
Run the CLI application
go run main.go
Output:
Hello, world
Build and run with flags
go build -o hello
./hello --help
Output:
Run hello command for getting started
Usage:
hello [flags]
Flags:
-h, --help help for hello
-t, --to string e.g. --to=world or -t world (default "world")
Say hello to Hiboot:
./hello --to Hiboot
Output:
Hello, Hiboot
What’s next?
Now that you’ve created your first Hiboot applications, explore these topics:
- Web Application - Build full-featured REST APIs
- CLI Application - Create powerful CLI tools
- Inversion of Control - Master dependency injection
- Auto Configuration - Create custom starters