app

package
v0.0.0-...-52ed2a8 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2021 License: MIT Imports: 29 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LoggerConfig = gin.LoggerConfig{
	Formatter: func(f gin.LogFormatterParams) string {
		return fmt.Sprintf(
			"[\x1b[35m%s\x1b[0m] \"\x1b[34m%s\x1b[0m\" %6v %s %s %s%d%s %s\n",
			f.TimeStamp.Format(time.Stamp),
			f.ClientIP,
			f.Latency,
			f.Request.Proto,
			f.Method,
			statusColor(f.StatusCode), f.StatusCode, "\x1b[0m",
			f.Path,
		)
	},
}

LoggerConfig is a config for gin loggers that has cleaner output

Functions

func ErrStatus

func ErrStatus(status int, msg string) error

ErrStatus creates a new error type with a spesific status code

func ListDiscussions

func ListDiscussions(db *sqlx.DB) gin.HandlerFunc

ListDiscussions returns a handlerfunc that lists discussions. Depends on "limit" and "offset" being set from middleware.

func ListExams

func ListExams(db *sqlx.DB) gin.HandlerFunc

ListExams returns a handlerfunc that lists exams. Depends on "limit" and "offset" being set from middleware.

func ListInstructors

func ListInstructors(db *sqlx.DB) gin.HandlerFunc

ListInstructors returns a handler func that lists the isntructors in the database. Requires that limit and offset have been set in middleware before this is called.

func ListLabs

func ListLabs(db *sqlx.DB) gin.HandlerFunc

ListLabs returns a handlerfunc that lists labs. Depends on "limit" and "offset" being set from middleware.

func ListLectures

func ListLectures(db *sqlx.DB) func(*gin.Context)

ListLectures returns a handlerfunc that lists lectures. Depends on "limit" and "offset" being set from middleware.

func NewErr

func NewErr(msg string) error

NewErr creates a new error type

Types

type App

type App struct {
	DB        *sqlx.DB
	Config    *Config
	Engine    *gin.Engine
	RateStore limiter.Store
	Protected gin.HandlerFunc
	// contains filtered or unexported fields
}

App is the main app

func New

func New(conf *Config) (*App, error)

New creates a new app

func (*App) Close

func (a *App) Close() error

Close the application resourses

func (*App) CreateUser

func (a *App) CreateUser(u *users.User, password string) (*users.User, error)

CreateUser stores a user in the database and sets its private variables

func (*App) GetInstructor

func (a *App) GetInstructor(id interface{}) (*models.Instructor, error)

GetInstructor will get an instructor by id

func (*App) GetUser

func (a *App) GetUser(u users.User) (*users.User, error)

GetUser will find a full initialized user give a partially initialized user.

func (*App) NewJWTAuth

func (a *App) NewJWTAuth() (*ginjwt.GinJWTMiddleware, error)

NewJWTAuth creates the default jwt auth middleware

func (*App) PostUser

func (a *App) PostUser(c *gin.Context)

PostUser handles user creation TODO: should be protected, only admin

func (*App) RegisterRoutes

func (a *App) RegisterRoutes(g *gin.RouterGroup)

RegisterRoutes will setup all the app routes

func (*App) ServeHTTP

func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*App) SilentCreateUser

func (a *App) SilentCreateUser(c *gin.Context)

SilentCreateUser will create a new user without writing to the response body

type Config

type Config struct {
	Host     string         `config:"host,shorthand=H,usage=server host" default:"0.0.0.0"`
	Port     int64          `config:"port,shorthand=P,usage=server port" default:"8080"`
	Mode     string         `config:"mode,usage=set the gin mode ('debug'|'release')" default:"debug"`
	Secret   string         `config:"secret,notflag" env:"JWT_SECRET"`
	Database DatabaseConfig `config:"db" yaml:"db"`

	InMemoryRateStore bool `config:"in_memory_rate_store" yaml:"in_memory_rate_store" default:"true"`
}

Config is the application config struct

func (*Config) Address

func (c *Config) Address() string

Address formats the server address:port from the app config

func (*Config) GetDSN

func (c *Config) GetDSN() string

GetDSN builds the database dns from the database config parameters

func (*Config) Init

func (c *Config) Init() error

Init sets up command line flags and parses command line args and gets config defaults

type DatabaseConfig

type DatabaseConfig struct {
	Driver   string `config:"driver,usage=database driver name"`
	Host     string `config:"host,shorthand=h" default:"localhost"`
	Port     int    `config:"port,shorthand=p" default:"5432" env:"POSTGRES_PORT"`
	User     string `config:"user,shorthand=U"`
	Password string `config:"password" env:"POSTGRES_PASSWORD"`
	// Database name or database filename
	Name string `config:"name,shorthand=d,usage=name of the database"`
	SSL  string `config:"ssl" default:"disable"`
}

DatabaseConfig is the part of the config struct that handles database info

func (*DatabaseConfig) GetDSN

func (dbc *DatabaseConfig) GetDSN() string

GetDSN builds the database dns from the database config parameters

type Error

type Error struct {
	Msg    string `json:"error"`
	Status int    `json:"status,omitempty"`
}

Error is an app spesific error

func (*Error) Error

func (e *Error) Error() string

type Msg

type Msg struct {
	Msg    string `json:"message"`
	Status int    `json:"status,omitempty"`
}

Msg is a standardized response message for misc json endpoints.

Jump to

Keyboard shortcuts

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