gofr

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2021 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMYSQL

func NewMYSQL(config *DBConfig) (*sql.DB, error)

func NewRedisClient

func NewRedisClient(config RedisConfig) (*redis.Client, error)

func ToSnakeCase

func ToSnakeCase(str string) string

Types

type App

type App struct {

	// Config can be used by applications to fetch custom configurations from environment or file.
	Config Config // If we directly embed, unnecessary confusion between app.Get and app.GET will happen.
	// contains filtered or unexported fields
}

App is the main application in the gofr framework.

func New

func New() *App

New creates a HTTP Server Application and returns that App.

func NewCMD

func NewCMD() *App

NewCMD creates a command line application.

func (*App) DELETE

func (a *App) DELETE(pattern string, handler Handler)

DELETE adds a Handler for http DELETE method for a route pattern.

func (*App) GET

func (a *App) GET(pattern string, handler Handler)

GET adds a Handler for http GET method for a route pattern.

func (*App) POST

func (a *App) POST(pattern string, handler Handler)

POST adds a Handler for http POST method for a route pattern.

func (*App) PUT

func (a *App) PUT(pattern string, handler Handler)

PUT adds a Handler for http PUT method for a route pattern.

func (*App) Run

func (a *App) Run()

Run starts the application. If it is a HTTP server, it will start the server.

func (*App) SubCommand

func (a *App) SubCommand(pattern string, handler Handler)

SubCommand adds a sub-command to the CLI application. Can be used to create commands like "kubectl get" or "kubectl get ingress".

type Config

type Config interface {
	Get(string) string
	GetOrDefault(string, string) string
}

type Container

type Container struct {
	logging.Logger
	Redis *redis.Client
	DB    *DB
}

Container is a collection of all common application level concerns. Things like Logger, Connection Pool for Redis etc which is shared across is placed here.

type Context

type Context struct {
	ctx.Context

	// Request needs to be public because handlers need to access request details. Else, we need to provide all
	// functionalities of the Request as a method on context. This is not needed because Request here is an interface
	// So, internals are not exposed anyway.
	Request

	// Same logic as above.
	*Container
	// contains filtered or unexported fields
}

func (*Context) Bind

func (c *Context) Bind(i interface{}) error

func (*Context) Trace

func (c *Context) Trace(name string) trace.Span

Trace returns an open telemetry span. We have to always close the span after corresponding work is done. Usages:

span := c.Trace("Some Work")
// Do some work here.
defer span.End()

If an entire function has to traced as span, we can use a simpler format:

defer c.Trace("ExampleHandler").End()

We can write this at the start of function and because of how defer works, trace will start at that line but End will be called after function ends.

Developer Note: If you chain methods in a defer statement, everything except the last function will be evaluated at call time.

type DB

type DB struct {
	*sql.DB
}

func (*DB) Select

func (d *DB) Select(ctx context.Context, data interface{}, query string, args ...interface{})

Select runs a query with args and binds the result of the query to the data. data should ba a point to a slice, struct or any type. Slice will return multiple objects whereas struct will return a single object.

Example Usages:

  1. Get multiple rows with only one column ids := make([]int, 0) db.Select(ctx, &ids, "select id from users")
  1. Get a single object from database type user struct { Name string ID int Image string } u := user{} db.Select(ctx, &u, "select * from users where id=?", 1)
  1. Get array of objects from multiple rows type user struct { Name string ID int Image string `db:"image_url"` } users := []user{} db.Select(ctx, &users, "select * from users")

type DBConfig

type DBConfig struct {
	HostName string
	User     string
	Password string
	Port     string
	Database string
}

DBConfig has those members which are necessary variables while connecting to database.

type ErrCommandNotFound

type ErrCommandNotFound struct{}

func (ErrCommandNotFound) Error

func (e ErrCommandNotFound) Error() string

type Handler

type Handler func(c *Context) (interface{}, error)

type Redis

type Redis interface {
	Get(string) (string, error)
}

TODO - if we make Redis an interface and expose from container we can avoid c.Redis(c, command) using methods on c and still pass c.

type RedisConfig

type RedisConfig struct {
	HostName string
	Port     int
	Options  *redis.Options
}

type Request

type Request interface {
	Context() context.Context
	Param(string) string
	PathParam(string) string
	Bind(interface{}) error
	Header(string) string
}

type Responder

type Responder interface {
	Respond(data interface{}, err error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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