shared

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: May 18, 2020 License: GPL-3.0 Imports: 13 Imported by: 0

README

kaijday-api-shared

A collection of shared utilities for kaijday-api microservices.

  • Version [0.2.2]

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ConfigApiPort configuration setting for the port to run the API on.
	ConfigApiPort = os.Getenv("ApiPort")

	// ConfigDatabaseConnString configuration setting for the postgres database connection string.
	ConfigDatabaseConnString = os.Getenv("ApiDatabaseConnectionString")

	// ConfigSecretKey configuration setting for the secret key to encrypt data with.
	ConfigSecretKey = os.Getenv("ApiSecretKey")
)
View Source
var (
	// ClaimsID claims lookup string to get the ID for a user.
	ClaimsUserID = "Id"
)

Functions

This section is empty.

Types

type ApiError

type ApiError struct {
	Code  int    `json:"code"`
	Error string `json:"error"`
}

ApiError A standard Api response model for errors.

type ApiResponse

type ApiResponse struct {
	Code   int    `json:"code"`
	Result string `json:"result"`
}

ApiResponse A standard Api response model.

type ApiService added in v0.1.6

type ApiService struct {
	Options ApiServiceOptions
	Server  *http.Server
}

func NewApiService added in v0.1.6

func NewApiService(options ApiServiceOptions) *ApiService

func (*ApiService) ListenAndServe added in v0.1.6

func (s *ApiService) ListenAndServe()

type ApiServiceOptions added in v0.1.6

type ApiServiceOptions struct {
	Port   string
	Router *gin.Engine
}

type DbService added in v0.1.6

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

DbService database service.

func NewDbService added in v0.1.6

func NewDbService(connectionString string) DbService

NewDbService creates a new DbService instance.

func (*DbService) Connect added in v0.1.6

func (s *DbService) Connect() *sql.DB

Connect creates a new connection to the database

func (*DbService) InitialiseSchema added in v0.1.6

func (s *DbService) InitialiseSchema(fn func(conn *sql.DB), conn *sql.DB)

InitialiseSchema runs a function against the database designed to initialise the schema for the database.

type DbSettings added in v0.1.6

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

DbSettings settings for connecting to the database.

type JwtService

type JwtService struct {
	UsersRepository UsersRepository
}

JwtService provides Json Web Token authentication middleware and hashing for use in API microservices.

func NewJwtService

func NewJwtService(r UsersRepository) JwtService

NewJwtService Creates a new JwtService instance, with a UsersRepository.

func (*JwtService) ComparePasswords

func (s *JwtService) ComparePasswords(hash string, password string) error

ComparePasswords compares a hashed and non-hashed password to check if they match. Returns an error if they do not.

func (*JwtService) CreateJwtMiddleware

func (s *JwtService) CreateJwtMiddleware(secretKey string) *jwt.GinJWTMiddleware

CreateJwtMiddleware provides a GIN compatible JWT middleware for authenticating with the API microservices.

func (*JwtService) HashPassword

func (s *JwtService) HashPassword(password string) (string, error)

HashPassword hashes passwords for saving into storage securely. Returns an error if generating a hash fails for any reason.

type LoginRequest

type LoginRequest struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

LoginRequest model to represent payload used by callers of the api to request a new JWT token. Payload needs to be in the form of {"username": "...", "password": "..."}

type User

type User struct {
	// Id is the unique identifier for the user.
	ID uuid.UUID `json:"id"`

	// Username is the login name for the user.
	Username string `json:"username"`

	// Password is the secure pass for securing the user.
	Password string `json:"password"`
}

User represents an API user and its database structure.

type UsersRepository

type UsersRepository struct {
	// conn An open connection to the database
	Conn *sql.DB
}

UsersRepository provides methods to access data from within a database for storage of api users.

func NewUsersRepository

func NewUsersRepository(conn *sql.DB) UsersRepository

NewUsersRepository creates a UsersRepository instance.

func (*UsersRepository) AddUser added in v0.1.5

func (r *UsersRepository) AddUser(user User) error

AddUser adds a new user to the repository. Returns any error that occurs or nil if added successfully.

func (*UsersRepository) DeleteUser added in v0.1.5

func (r *UsersRepository) DeleteUser(ID uuid.UUID) error

DeleteUser removes a user from the repository. Returns any error that occurs or nil if removed successfully.

func (*UsersRepository) GetUserByID added in v0.1.5

func (r *UsersRepository) GetUserByID(ID uuid.UUID) (*User, error)

GetUserByID gets a user from the repository, using its identifier as the lookup. Returns the user with any errors.

func (*UsersRepository) GetUserByUsername added in v0.1.5

func (r *UsersRepository) GetUserByUsername(username string) (*User, error)

GetUserByUsername gets a user from the repository, using its username as the lookup. Returns the user with any errors.

Jump to

Keyboard shortcuts

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