hiro

package module
v2.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 29, 2019 License: Apache-2.0 Imports: 12 Imported by: 0

README

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/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.

Documentation

Overview

Package hiro provides the hiro entry points

Index

Constants

View Source
const (
	// HiroFileName is the hiro config
	HiroFileName = "hiro.json"
)

Variables

This section is empty.

Functions

func Init

func Init(env ...string) error

Init to loads the hiro environment from the local config file This should be called from the main.init()

func Serve

func Serve(handler http.Handler, onExit ...func()) error

Serve starts the hiro api server with graceful exit

Types

type APISpec

type APISpec struct {
	Spec      string `json:"spec,omitempty"`
	Source    string `json:"source,omitempty"`
	Principal string `json:"principal,omitempty"`
	// Version is the semantic version for the function
	Version string `json:"version,omitempty"`
	// Models is the models path
	Models        string `json:"models"`
	GenerateStubs bool   `json:"operation_stubs"`
	BasePath      string `json:"base"`
	WithContext   bool   `json:"with_context"`
	AutoSync      bool   `json:"auto_sync"`
}

APISpec defines an API Specification

type Config

type Config struct {
	// Name is the project name
	Name string `json:"name"`

	// APIs is the project apis
	APIs map[string]*APISpec `json:"apis"`

	// Options is the Swagger Generation Options
	Options GenOptions `json:"options"`

	// Keys is the encryption keys
	Keys map[string]string `json:"keys"`

	// License is the license definition for the project
	License License `json:"license"`

	// Environment defines the global environment
	Environment map[string]map[string]string `json:"environment,omitempty"`
	// contains filtered or unexported fields
}

Config is the hiro project file definition

func LoadConfig

func LoadConfig(configPath ...string) (*Config, error)

LoadConfig opens the config or returns a default one

func NewConfig

func NewConfig(configPath ...string) *Config

NewConfig returns a new config with the specified path

func (*Config) Path

func (c *Config) Path() string

Path returns the path for the configuration file

func (*Config) Save

func (c *Config) Save() error

Save saves the config

func (*Config) SpecPath

func (c *Config) SpecPath(name string) string

SpecPath returns the path for the spec

type Function

type Function struct {
	Source string `json:"source,omitempty"`
	Proxy  string `json:"proxy,omitempty"`
	Env    string `json:"env,omitempty"`
}

Function defines a function settings

type GenOptions

type GenOptions struct {
	// Generate using non-pointer arrays i.e. []type vs []*type
	ScalarArrays bool `json:"scalar_arrays"`
}

GenOptions defines globa swagger generation options

type License

type License struct {
	Owner     string `json:"owner,omitempty"`
	Type      string `json:"type,omitempty"`
	Header    string `json:"header,omitempty"`
	Copyright string `json:"-"`
}

License defines the project license type

Directories

Path Synopsis
cmd
hiro command
pkg
generator
Package generator provides the code generation library for go-swagger.
Package generator provides the code generation library for go-swagger.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL