mgosrv

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2018 License: MIT Imports: 7 Imported by: 0

README

CircleCI codecov

http-mgo-service

The http-mgo-service is the lab259/http service implementation for the go-mgo/mgo library.

Dependencies

It depends on the lab259/http (and its dependencies, of course) itself and the go-mgo/mgo library.

Installation

First, fetch the library to the repository.

go get github.com/lab259/http-mgo-service

Usage

Applying configuration and starting service

// Create MgoService instance
var mgoService MgoService

// Applying configuration
err := mgoService.ApplyConfiguration(MgoServiceConfiguration{
    Addresses: []string{"localhost"},
	Username:  "username",
	Password:  "password",
	Database:  "my-db",
	PoolSize:  1,
    Timeout:   60,
})

if err != nil {
    panic(err)
}
        
// Starting service
err := mgoService.Start()

if err != nil {
    panic(err)
}

// Create a custom object
var object MyModel

// Executing something using a *mgo.Session
err := mgoService.RunWithSession(func(session *mgo.Session) error {
    // Retrieving an object from the MongoDB
    return session.DB("my-db").C("my-collection").FindId("my-object-id").One(&object)
})

if err != nil {
    panic(err)
}

id := object.Id // "my-object-id"

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MgoService

type MgoService struct {
	Configuration MgoServiceConfiguration
	// contains filtered or unexported fields
}

MgoService implements the mgo service itself.

func (*MgoService) ApplyConfiguration

func (service *MgoService) ApplyConfiguration(configuration interface{}) error

ApplyConfiguration implements the type verification of the given `configuration` and applies it to the service.

func (*MgoService) LoadConfiguration

func (service *MgoService) LoadConfiguration() (interface{}, error)

LoadConfiguration is an abstract method that should be overwritten on the actual usage of this service.

func (*MgoService) Restart

func (service *MgoService) Restart() error

Restart restarts the service.

func (*MgoService) RunWithSession

func (service *MgoService) RunWithSession(handler func(session *mgo.Session) error) error

RunWithSession runs a handler passing a new instance of the a session.

func (*MgoService) Start

func (service *MgoService) Start() error

Start initialize the mongo connection and saves the session.

func (*MgoService) Stop

func (service *MgoService) Stop() error

Stop stops the service.

type MgoServiceConfiguration

type MgoServiceConfiguration struct {
	Addresses []string        `yaml:"addresses"`
	Database  string          `yaml:"database"`
	Username  string          `yaml:"username"`
	Password  string          `yaml:"password"`
	PoolSize  int             `yaml:"pool_size"`
	Timeout   int             `yaml:"timeout"`
	Mode      *MgoServiceMode `yaml:"mode"`
	UseTLS    bool            `yaml:"use_tls"`
}

MgoServiceConfiguration describes the `MgoService` configuration.

type MgoServiceMode

type MgoServiceMode mgo.Mode

MgoServiceMode is an alias for the `mgo.Mode` that implements Unmarshaling from the YAML.

func (*MgoServiceMode) UnmarshalYAML

func (mode *MgoServiceMode) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML implements the marshaling a `mgo.Mode` to string.

Jump to

Keyboard shortcuts

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