server

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Database

type Database struct {
	// contains filtered or unexported fields
}

Database An abstraction of an active mongodb database connection. The same connection is re-used across all SDK operations to ensure that we don't exceed the connection pool limit

func NewDatabase

func NewDatabase(hostname string, port int, defaultDatabase string) *Database

NewDatabase - A constructor for the database object. This exists in the event the caller wants to create a new database object without using Viper

func NewDatabaseFromConfig

func NewDatabaseFromConfig() *Database

NewDatabaseFromConfig - A wrapper for NewDatabase. Constructs a new database from configuration values passed in Viper

func (*Database) Client

func (database *Database) Client() *mongo.Client

Client - Getter function for returning a reference to the MongoDB client

func (*Database) Connect

func (database *Database) Connect()

Connect to the MongoDB instance defined in the Database object

func (*Database) Database

func (database *Database) Database() *mongo.Database

Database - Getter function for returning a pointer to the MongoDB database

func (*Database) Delete

func (database *Database) Delete(collection string, query bson.M) error

Delete - Remove a single document from the MongoDB collection

func (*Database) Exists

func (database *Database) Exists(collection string, query bson.M) (bool, error)

Exists - Check to see if a document exists from within the database

func (*Database) Find

func (database *Database) Find(collection string, query bson.M, model interface{}, exclude ...string) error

Find - Fetch a document from MongoDB and decode the results into the reference passed in the model parameter

func (*Database) Insert

func (database *Database) Insert(collection string, model interface{}) error

Insert - Insert a single document into the MongoDB collection attached to this Database instance

func (*Database) Replace

func (database *Database) Replace(collection string, query bson.M, model interface{}) error

Replace - Replace a single document in the MongoDB collection attached to this Database instance

func (*Database) SetSCRAMAuthentication

func (database *Database) SetSCRAMAuthentication(username string, password string)

SetSCRAMAuthentication - Set the credentials for the database connection if they are needed

type HandlerFunc

type HandlerFunc func(service *Service) func(c *gin.Context)

HandlerFunc - A wrapper for your gin middleware

type Service

type Service struct {
	// Name - The name of the Service
	Name string

	// Port - The port to expose the API on. Defaults to 8080
	Port int
	// contains filtered or unexported fields
}

Service - An abstraction of a Micro-Service. Responsible for exposing the underlying gin REST API and providing a connection to MongoDB

func FromConfig

func FromConfig() *Service

FromConfig - A wrapper around New. Constructs a new Service struct using values provided by Viper

func New

func New(name string, port int, database *Database) *Service

New - A constructor for the Service object

func (*Service) Database

func (service *Service) Database() *Database

Database - Getter function for returning a pointer to the database

func (*Service) RegisterEndpoint

func (service *Service) RegisterEndpoint(method string, endpoint string, handler HandlerFunc)

RegisterEndpoint - Wraps the gin function gin.Engine.Handle and registers a new endpoint with the router. The 'handler' parameter should be the logic of your endpoint using the HandlerFunc type

func (*Service) Run

func (service *Service) Run() error

Run - Start the Service and expose the API to the port defined in Service.Port. Logic in here should eventually be deprecated so it can gracefully shutdown. Gin doesn't provide a way natively within its framework to gracefully stop accepting connections

func (*Service) Shutdown

func (service *Service) Shutdown() error

Shutdown - Gracefully disconnects from the database. This will be improved eventually as realistically the API should stop accepting new requests before gracefully disconnecting from MongoDB

Jump to

Keyboard shortcuts

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