base

package module
v0.0.0-...-5cbdbc3 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2018 License: MIT Imports: 14 Imported by: 0

README

Base

Standardized foundation for Go microservices.

What is Included

This foundation includes a handful of common dependencies:

  • Flags to configure database, HTTP port, etc.
  • Gorm database handle
  • RSA keypair for signing and verifying JWTs
  • Minio Client to access S3 compatible storage
  • Echo web server

Usage

I suggest embeddeding Base into a service struct that has your HTTP handlers.

type myService struct {
	*base.Base
}

You then have easy access to the database handle wherever you have access to this service.

See the simple example.

Limitations

This reusability works for me but it's not that flexible currently, so I don't expect it to work for you!

You can use the Base struct but customize your own setup. This would allow for example using MySQL instead of the default Postgres.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConnectPostgres

func ConnectPostgres(s DatabaseSettings) (*gorm.DB, error)

ConnectPostgres connects to a Postgres database using the given settings and returns a *gorm.DB handle.

func LoadRSAPrivateKey

func LoadRSAPrivateKey(path string) (*rsa.PrivateKey, error)

LoadRSAPrivateKey reads an RSA private key from the specified file

func LoadRSAPublicKey

func LoadRSAPublicKey(path string) (*rsa.PublicKey, error)

LoadRSAPublicKey reads an RSA public key from the specified file

func SetupEcho

func SetupEcho() (*echo.Echo, error)

SetupEcho creates a new Echo instance for HTTP

Types

type AuthSettings

type AuthSettings struct {
	PublicKey  string
	PrivateKey string
}

AuthSettings configures application authentication.

type Base

type Base struct {
	Settings       Settings
	AuthPublicKey  *rsa.PublicKey
	AuthPrivateKey *rsa.PrivateKey
	DB             *gorm.DB
	ObjectStore    *minio.Client
	Echo           *echo.Echo
}

Base contains a core set of dependencies needed for web services. This struct can be made available to HTTP handlers.

func Must

func Must() *Base

Must is a helper that wrap New and exits the process on failure

func New

func New() (*Base, error)

New environment using settings provided via command line options and environment variables. You can also implement your own version of this if you need to since Env fields are all public.

func (*Base) Destroy

func (base *Base) Destroy()

Destroy resource handles

func (*Base) JWTMiddleware

func (base *Base) JWTMiddleware() echo.MiddlewareFunc

JWTMiddleware returns JWT authentication middleware

func (*Base) Run

func (base *Base) Run() error

Run the HTTP server

type DatabaseSettings

type DatabaseSettings struct {
	Host        string
	Port        int
	User        string
	Password    string
	Name        string
	SSLMode     string
	SSLRootCert string
	SSLCert     string
	SSLKey      string
}

DatabaseSettings specifies location and authentication information needed to connect to the application database.

type HTTPSettings

type HTTPSettings struct {
	ListenAddress string
}

HTTPSettings configures the application HTTP interface.

type JWTClaims

type JWTClaims struct {
	Name    string `json:"name,omitempty"`
	Admin   bool   `json:"admin,omitempty"`
	Context string `json:"context,omitempty"`
	jwt.StandardClaims
}

JWTClaims are custom claims extending default ones

type ObjectStoreSettings

type ObjectStoreSettings struct {
	URL        string
	Region     string
	Bucket     string
	DisableSSL bool
}

ObjectStoreSettings configures the application object storage endpoint.

type SSLMode

type SSLMode string

SSLMode defines SSL settings used to connect to Postgres

const (
	// SSLModeDisabled disables SSL
	SSLModeDisabled SSLMode = "disable"

	// SSLModeRequired makes SSL required
	SSLModeRequired SSLMode = "require"

	// SSLModeVerifyCA enables SSL with server and client certificates
	SSLModeVerifyCA SSLMode = "verify-ca"
)

type Settings

type Settings struct {
	Database    DatabaseSettings
	ObjectStore ObjectStoreSettings
	Auth        AuthSettings
	HTTP        HTTPSettings
}

Settings for the application

func GetSettings

func GetSettings() Settings

GetSettings returns application configuration derived from command line options and environment variables.

func (Settings) Log

func (s Settings) Log()

Log settings via the standard logger while omitting the sensitive fields.

Directories

Path Synopsis
examples
simple command

Jump to

Keyboard shortcuts

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