hiro 2.0
Hiro (snowcrash) is the Model Rocket project generation tool. Hiro borrows heavily
from go-swagger, but builds other custom components for projects
using the Model Rocket Cloud Core.
Install
Hiro uses go modules ensure the env var GO111MODULE=on
is set in your .bash_profile
or other appropriate location.
> go install gitlab.com/ModelRocket/hiro/v2/cmd/hiro
Project Layout
All hiro projects have the same base layout:
~~~
├── .hiro // The hiro project dir
├── api/ // Generated API sources
│ └── api_name/ // The api name
│ └── rest/ // The swagger based rest api
| └── types/ // The api data types and models
| └── api_name.yaml // The swagger definition
├── functions/
│ └── .../ // Cloud functions
├── LICENSE // The project license
~~~
Getting started
You run hiro from with the directory you want to create a new project. This directory
must be within your $(GOPATH)/src. To initialize a project you simply point it at
your swagger document. This can be a file or a url, either will be copied to
api/swagger.yaml
Note: hiro cannot resolve private swagger documents at this time, so all definitions must
be public or contained within the same specification document. For projects that use domains
or external references, setup a GitHub sync in swagger to the git branch or the project
to the location above.
Initialize the project
> hiro init
Working with an API
Add an API
> hiro api add https://api.swaggerhub.com/apis/ModelRocket/acme/1.0.0/swagger.yaml
Sync the API
If you make changes to the API, you need to sync the swagger.
> hiro api sync
Create a cloud entry point for a specific API (lambda or otherwise)
> hiro function add acme
This will create a main.go
in ./functions/acme
.
Environments
You can provide environment variables to a hiro function via the hiro.json
file.
{
"environment": {
"prod": {
"CLOUD_PROVIDER": "aws",
"DB_DRIVER": "postgres"
}
}
}
Initializing the Environment
In the main package, you should call hiro.Init()
at some point before using any other methods, the best place is in init
.
func init() {
hiro.Init()
}
This will ensure the environment in the hiro.json
is applied.
Hiro Server
hiro provides a Server
method for detecting cloud environments like AWS Lambda with a fallback to starting a local HTTP server.
if err := hiro.Serve(handler, func() {
db.Close()
log.Debug("database closed")
}); err != nil {
log.Fatalln(err)
}
The method takes an http.Handler
and a callback for when the service gracefully exits upon termination signal.