hiboot: hidevops.io/hiboot Index | Examples | Files | Directories

package hiboot

import "hidevops.io/hiboot"

Package hiboot is a web/cli app application framework

Hiboot is a cloud native web and cli application framework written in Go.

Hiboot integrates the popular libraries but make them simpler, easier to use. It borrowed some of the Spring features like dependency injection, aspect oriented programming, and auto configuration. You can integrate any other libraries easily by auto configuration with dependency injection support. hiboot-data is the typical project that implement customized hiboot starters. see https://godoc.org/hidevops.io/hiboot-data

Overview

Web MVC - (Model-View-Controller).
Auto Configuration - pre-create instance with properties configs for dependency injection.
Dependency injection - with struct tag name `inject:""` or Constructor func.

Introduction to Hiboot

One of the most significant feature of Hiboot is Dependency Injection. Hiboot implements JSR-330 standard.

Let's say that we have two implementations of AuthenticationService, below will explain how does Hiboot work.

type AuthenticationService interface {
	Authenticate(credential Credential) error
}

type basicAuthenticationService struct {
}

func newBasicAuthenticationService() AuthenticationService {
	return &basicAuthenticationService{}
}

func (s *basicAuthenticationService) Authenticate(credential Credential) error {
	// business logic ...
	return nil
}

type oauth2AuthenticationService struct {
}

func newOauth2AuthenticationService() AuthenticationService {
	return &oauth2AuthenticationService{}
}

func (s *oauth2AuthenticationService) Authenticate(credential Credential) error {
	// business logic ...
	return nil
}

func init() {
	app.Register(newBasicAuthenticationService, newOauth2AuthenticationService)
}

Field Injection

In Hiboot the injection into fields is triggered by `inject:""` struct tag. when inject tag is present on a field, Hiboot tries to resolve the object to inject by the type of the field. If several implementations of the same service interface are available, you have to disambiguate which implementation you want to be injected. This can be done by naming the field to specific implementation.

type userController struct {
	at.RestController

	BasicAuthenticationService AuthenticationService	`inject:""`
	Oauth2AuthenticationService AuthenticationService	`inject:""`
}

func newUserController() {
	return &userController{}
}

func init() {
	app.Register(newUserController)
}

Constructor Injection

Although Field Injection is pretty convenient, but the Constructor Injection is the first-class citizen, we usually advise people to use constructor injection as it has below advantages,

* It's testable, easy to implement unit test.
* Syntax validation, with syntax validation on most of the IDEs to avoid typo.
* No need to use a dedicated mechanism to ensure required properties are set.

type userController struct {
	at.RestController

	basicAuthenticationService AuthenticationService
}

// Hiboot will inject the implementation of AuthenticationService
func newUserController(basicAuthenticationService AuthenticationService) {
	return &userController{
		basicAuthenticationService: basicAuthenticationService,
	}
}

func init() {
	app.Register(newUserController)
}

Features

App
	cli - command line application
	web - web application

Starters
	actuator - health check
	locale - locale starter
	logging - customized logging settings
	jwt - jwt starter
	grpc - grpc application starter

Tags
	inject - inject generic instance into object
	default - inject default value into struct object
	value - inject string value or references / variables into struct string field

Utils
	cmap - concurrent map
	copier - copy between struct
	crypto - aes, base64, md5, and rsa encryption / decryption
	gotest - go test util
	idgen - twitter snowflake id generator
	io - file io util
	mapstruct - convert map to struct
	replacer - replacing stuct field value with references or environment variables
	sort - sort slice elements
	str - string util enhancement util
	validator - struct field validation

Getting started

This section will show you how to create and run a simplest hiboot application. Let’s get started!

Getting started with Hiboot web application

Get the source code

go get -u hidevops.io/hiboot
cd $GOPATH/src/hidevops.io/hiboot/examples/web/helloworld/

Source Code

This is a simple hello world example

Code:

package main

import (
    "hidevops.io/hiboot/pkg/app/web"
    "hidevops.io/hiboot/pkg/at"
)

// This is a simple hello world example
func main() {
    // the web application entry
    web.NewApplication(new(Controller)).Run()
}

// Controller is the RESTful Controller, derived from at.RestController.
// The context path of this controller is '/' by default
// if you name it HelloController, then the default context path will be /hello
// which the context path of this controller is hello
type Controller struct {
    at.RestController
}

// Get method, the context mapping of this method is '/' by default
// the Method name Get means that the http request method is GET
func (c *Controller) Get() string {
    // response data
    return "Hello world"
}

Index

Examples

Package Files

doc.go

Directories

PathSynopsis
pkg/appPackage app provides abstract layer for cli/web application
pkg/app/cliPackage cli provides quick start framework for command line application.
pkg/app/fakePackage fake provides fake.ApplicationContext for unit testing
pkg/app/webPackage web provides quick start framework for web application.
pkg/app/web/context
pkg/atPackage at provides annotations for struct
pkg/factoryPackage factory provides hiboot factory interface
pkg/factory/autoconfigurePackage autoconfigure implement ConfigurableFactory
pkg/factory/dependsPackage depends provides dependency resolver for factory
pkg/factory/depends/barPackage bar is the test package for package depends
pkg/factory/depends/fakePackage fake is the test package for package depends
pkg/factory/depends/fooPackage foo is the test package for package depends
pkg/factory/instantiatePackage instantiate implement InstantiateFactory
pkg/injectPackage inject implements dependency injection.
pkg/logPackage log provides logging with level debug, info, warn, error, fatal.
pkg/modelPackage model provides common request and response models.
pkg/starterPackage starter provides quick starters for hiboot cli/web application.
pkg/starter/actuatorPackage actuator provide the health check endpoint for web application
pkg/starter/grpcPackage grpc provides the hiboot starter for injectable grpc client and server dependency
pkg/starter/grpc/mockgrpcPackage mockgrpc is a generated GoMock package.
pkg/starter/jwtPackage jwt provides the hiboot starter for injectable jwt dependency
pkg/starter/localePackage locale provides the hiboot starter for injectable locale (i18n) dependency
pkg/starter/loggingPackage logging provides the hiboot starter for injectable logging dependency
pkg/starter/websocketPackage websocket provides web socket auto configuration for web/cli application
pkg/systemPackage system provides system builder which response for properties dependency injection.
pkg/system/typesPackage types provides all const types name
pkg/utilsPackage utils provides useful utilities
pkg/utils/cmapPackage cmap provides concurrent map
pkg/utils/copierPackage copier provides utility that copy element between structs
pkg/utils/cryptoPackage crypto provides crypto encryption/decryption utilities
pkg/utils/crypto/aesPackage aes provides aes encryption/decryption utilities
pkg/utils/crypto/base64Package base64 provides base64 encryption/decryption utilities
pkg/utils/crypto/md5Package md5 provides md5 encryption utilities
pkg/utils/crypto/rsaPackage rsa provides rsa encryption/decryption utilities
pkg/utils/gotestPackage gotest provides function to check whether is running in go test mode.
pkg/utils/idgenPackage idgen provides unique id generation utilities that use twitter's snowflake algorithm
pkg/utils/ioPackage io provides file or directory io utilities.
pkg/utils/mapstructPackage mapstruct provides utilities that decode map and inject values into struct
pkg/utils/reflectorPackage reflector provides utilities for reflection
pkg/utils/reflector/tester
pkg/utils/replacerPackage replacer provides utilities that replace the reference and environment variables with its value
pkg/utils/sortPackage sort provides utility that sort slice by length
pkg/utils/strPackage str provides enhanced string utilities
pkg/utils/validatorPackage validator provides data validation utilities

Updated 2019-06-18. Refresh now. Tools for package owners.