server

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2025 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInternalDatabase = credstackError.NewError(500, "INTERNAL_DATABASE_ERROR", "database: an internal error occurred")

ErrInternalDatabase - Provides a simple wrapper around an internal database error

Functions

This section is empty.

Types

type Database

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

Database - Defines the core abstraction around a MongoDB database. This structure provides construction from Viper config values, along with basic parameters. Additionally, authentication can be controlled here without needing to be aware of MongoDB's underlying API structure.

Queries that would be made on collections (like findOne) are not abstracted here as I didn't want to overcomplicate this abstraction. I find that having service level packages (like the Token service) directly interact with the raw mongo.Collection pointer vastly simplifies maintenance as I don't need to re-abstract whatever functionality that MongoDB is providing to me

If a service wishes to make Database calls, it can call the Database.Collection method and pass the collection that it wants to use in the argument of that function call.

func NewDatabase

func NewDatabase(opts ...*options.DatabaseOptions) *Database

NewDatabase - Constructs a new Database using the values passed in each of its parameters. Calling this function does not connect to the database automatically. This needs to be done post-construction with Database.Connect. If an options structure is not passed in this functions parameter, then the Database is initialized with default values. Additionally, if more than 1 are passed here, only the first is used.

If you need to construct a new database from viper configurations, you should use options.DatabaseOptions.FromConfig

func (*Database) Collection

func (database *Database) Collection(collection string) *mongo.Collection

Collection - A getter for returning the underlying mongo.Collection pointer

func (*Database) Connect

func (database *Database) Connect() error

Connect - General wrapper around mongo.Connect. Generally, the mongo session created with this function should be re-used across multiple calls to ensure that excess resources are not wasted initiating additional connections to MongoDB.

func (*Database) Disconnect

func (database *Database) Disconnect() error

Disconnect - Gracefully disconnects from the MongoDB client. Acts as a wrapper around mongo.Client.Disconnect and returns any errors that arise from it

func (*Database) Init

func (database *Database) Init() map[string]error

Init - Initializes MongoDB with default collections and indexes where they are needed. The Init function anticipates that the default database already exists and that authentication has been established on it. Automation for this is not provided.

Each collection applies a unique index on header.identifier to ensure that objects with duplicated UUID's do not get inserted. This really shouldn't happen any way as these are generated based on unique values for its respective object, applying indexes here provides an easier way to determine if an object already exists without consuming an additional database call.

A map is returned representing the errors that were encountered during the initialization process. The maps key represents the name of the collection and the value is the error that occurred. If an error occurs during initialization then the current iteration of the loop is continued and initialization is continued

func (*Database) Options

func (database *Database) Options() *options.DatabaseOptions

Options - Returns a pointer to the options struct used with the Database

type Log

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

Log - An abstraction for the Logger. Handles any logic for creating and writing log files here

func NewLog

func NewLog(opts ...*options.LogOptions) *Log

NewLog - Constructs a new Log using the values passed in its parameters. If an options structure is not passed in this functions parameter, then the Log is initialized with default values. Additionally, if more than 1 are passed here, only the first is used.

func (*Log) CloseLog

func (log *Log) CloseLog() error

CloseLog - Sync's buffered log entries to log's core and if the user is using file logging, its associating file is gracefully closed. This should really be called in the event that a panic happens in underlying code so if you are utilizing this, then call this using `defer`

func (*Log) LogAuthEvent

func (log *Log) LogAuthEvent(eventType string, email string, username string, method string, appId string)

LogAuthEvent - Handler for logging any kind of authentication events. This includes login's, logouts, and registration primarily. Token events are logged using the Log.LogTokenEvent Handler.

func (*Log) LogDatabaseEvent

func (log *Log) LogDatabaseEvent(eventType string, hostname string, port int)

LogDatabaseEvent - Logs database specific events, mostly connection and disconnections. Additionally, authentication errors get logged here as well.

func (*Log) LogErrorEvent

func (log *Log) LogErrorEvent(description string, err error)

LogErrorEvent - Handler for logging any kind of error events.

func (*Log) LogShutdownEvent

func (log *Log) LogShutdownEvent(eventType string, description string)

LogShutdownEvent - Log handler for logging misc shutdown events

func (*Log) LogStartupEvent

func (log *Log) LogStartupEvent(eventType string, description string)

LogStartupEvent - Log handler for logging misc startup events

func (*Log) LogTokenEvent

func (log *Log) LogTokenEvent(eventType string, email string, tokenType string, appId string, apiId string)

LogTokenEvent - Handler for logging any kind of token events. This includes generation, revocation, introspection, and validation.

func (*Log) Logger

func (log *Log) Logger() *zap.Logger

Logger - Returns a pointer to the underlying zap logger structure

func (*Log) Options

func (log *Log) Options() *options.LogOptions

Options - Returns a pointer to the options used for the Log

type Server

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

Server - Provides an abstraction of any commonly used resources that services would need to interact with. Also provides lifecycle control for these objects

func Default

func Default() *Server

Default - Initializes the server and its components with default configurations

func FromConfig

func FromConfig() *Server

FromConfig - Initializes the server and its components with configurations created from viper values

func New

func New(dbOpts *options.DatabaseOptions, logOpts *options.LogOptions) *Server

New - Constructs a new server using configurations passed in the arguments of this function

func (*Server) Database

func (server *Server) Database() *Database

Database - Returns a pointer to the Database that the server is currently using. The same database gets re-used across multiple services as re-connecting to the database across every function call gets expensive

func (*Server) Log

func (server *Server) Log() *Log

Log - Returns a pointer to the Log that the server is currently using. If you are using this be sure to call Log.Close once the application exists as existing writes that have been buffered will get flushed

Jump to

Keyboard shortcuts

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