backapi

package module
v0.0.0-...-f39411c Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2020 License: MIT Imports: 4 Imported by: 6

README

Back API Skeleton

Introduction

The purpose of this repository is to be usable as a skeleton of Web API. It contains the main components initialized and running without any further complexity nor any domain-specific code.

It can be used to start any project from the blog to a E-Commerce website or a SaaS.

Content

Currently, the project contains :

  • Global
  • Configuration handling with files and environment variables. uses Viper
  • A running CLI to start the project. uses Cobra
  • HTTP
  • A server initialized and running. uses Echo
  • CRUD Endpoints for users
  • Sessions system with login/logout
  • Admin system
  • Authentication, Authorization and Admin middlewares
  • Payloads validation
  • Response formatter
  • Database
  • A Postgresql implementation running
  • Migration system.
  • A basic User model with all the required methods to interact with it
  • A basic Session model with all the required methods to interact with it
  • Misc
  • a JWT interface to create easily JWT token if needed

How to add a new entity

Adding a new entity is really straightforward and should only require very specific independent steps :

  • Add the entity in the migration / model (under the PG folder.)
  • Add the corresponding part in the root of the project (corresponding to the controller)
  • Finally add the corresponding endpoints in the HTTP part

More detailled version to come...

Other

The code is tested from end-to-end and should be usable as is to start a new project. That's also why it will not implement that much more new features to keep it simple to run and understand.

Documentation

Index

Constants

View Source
const (
	// Version represents the current version of the project
	Version = "0.1.0-DEV"
)

Variables

This section is empty.

Functions

func IsInternalServerError

func IsInternalServerError(err error) bool

IsInternalServerError identify an error as an InternalServerError

func IsNotFoundError

func IsNotFoundError(err error) bool

IsNotFoundError identify an error as an NotFoundError

func IsOutdatedResourceError

func IsOutdatedResourceError(err error) bool

IsResourceAlreadyCreatedError identify an error as a ResourceAlreadyCreatedError

func IsResourceAlreadyCreatedError

func IsResourceAlreadyCreatedError(err error) bool

IsResourceAlreadyCreatedError identify an error as a ResourceAlreadyCreatedError

func IsUnauthorizedError

func IsUnauthorizedError(err error) bool

IsUnauthorizedError identifies an error as UnauthorizedError

func NewInternalServerError

func NewInternalServerError(key string) error

NewInternalServerError is used when an error unexpected appears

func NewNotFoundError

func NewNotFoundError(key string) error

NewNotFoundError is used when we cannot find a specified resource

func NewOutdatedResourceError

func NewOutdatedResourceError(key string) error

NewOutdatedResourceError is used when a resource already exist and could not be created another time

func NewResourceAlreadyCreatedError

func NewResourceAlreadyCreatedError(key string) error

NewResourceAlreadyExist is used when a resource already exist and could not be created another time

func NewUnauthorizedError

func NewUnauthorizedError(key string, subjectAndMessage ...string) error

NewUnauthorizedError is used when the action is not authorized

Types

type CfgDatabase

type CfgDatabase struct {
	Host     string `json:"host"`
	Port     int    `json:"port"`
	User     string `json:"user"`
	Password string `json:"password"`
	DBName   string `json:"dbname"`
}

type CfgHTTP

type CfgHTTP struct {
	Port string `json:"port"`
}

type CfgMailer

type CfgMailer struct {
	FromMail         string `json:"from_mail"`
	FromUser         string `json:"from_user"`
	MailjetAPIKey    string `json:"mailjet_api_key"`
	MailjetAPISecret string `json:"mailjet_api_secret"`
}

type CfgStripe

type CfgStripe struct {
	SecretKey string `json:"secret_key"`
}

type Config

type Config struct {
	Database  CfgDatabase `json:"database"`
	HTTP      CfgHTTP     `json:"http"`
	Mailer    CfgMailer   `json:"mailer"`
	Stripe    CfgStripe   `json:"stripe"`
	DomainURL string      `json:"domain_url"`
}

type ErrorWithKey

type ErrorWithKey struct {
	Key string
}

func (*ErrorWithKey) Error

func (e *ErrorWithKey) Error() string

type InternalServerError

type InternalServerError struct {
	ErrorWithKey
}

InternalServerError is used when an error unexpected appears

type NotFoundError

type NotFoundError struct {
	ErrorWithKey
}

NotFoundError is used when we cannot find a specified resource

type OutdatedResourceError

type OutdatedResourceError struct {
	ErrorWithKey
}

ResourceAlreadyCreatedError is used when a resource already exist and could not be created another time

type ResourceAlreadyCreatedError

type ResourceAlreadyCreatedError struct {
	ErrorWithKey
}

ResourceAlreadyCreatedError is used when a resource already exist and could not be created another time

type Session

type Session struct {
	ID        string    `json:"id"`
	UserID    string    `json:"user_id"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
	ExpiresAt time.Time `json:"expires_at"`
}

type SessionFinder

type SessionFinder interface {
	ByID(ID string) (*Session, error)
}

type SessionService

type SessionService struct {
	StoreManager *StoreManager
}

func (*SessionService) Create

func (s *SessionService) Create(ctx context.Context, Email string, Password string) (*Session, error)

func (*SessionService) Delete

func (s *SessionService) Delete(ctx context.Context, ID string) error

func (*SessionService) GetByID

func (s *SessionService) GetByID(ctx context.Context, ID string) (*Session, error)

type SessionStore

type SessionStore interface {
	Create(*Session) (*Session, error)
	Delete(ID string) error
	DeleteAllForUserID(userID string) error
}

type StoreManager

type StoreManager struct {
	UserStore  UserStore
	UserFinder UserFinder

	SessionStore  SessionStore
	SessionFinder SessionFinder
}

StoreManager handle all the different stores accessible

type UnauthorizedError

type UnauthorizedError struct {
	ErrorWithKey
}

UnauthorizedError is used when action is not authorized

type User

type User struct {
	// IDs
	ID string `json:"id"`
	// CustomerID is related to Stripe
	CustomerID string `json:"customer_id"`

	// Login information
	Email    string `json:"email"`
	Password string `json:"-"`

	// Personal information
	Firstname   string `json:"firstname"`
	Lastname    string `json:"lastname"`
	Username    string `json:"username"`
	PhoneNumber string `json:"phone_number"`

	// Status
	Admin bool `json:"admin"`

	// Steps date
	CreatedAt   time.Time `json:"created_at"`
	ConfirmedAt time.Time `json:"confirmed_at"`
	UpdatedAt   time.Time `json:"updated_at"`
	DeletedAt   time.Time `json:"deleted_at"`
}

User defines all the fields that represent a user

type UserFinder

type UserFinder interface {
	ByEmail(email string) (*User, error)
	ByID(id string) (*User, error)
	List() ([]User, error)
}

UserFinder defines all the methods usable to read data in the store for users

type UserService

type UserService struct {
	StoreManager *StoreManager
}

func (*UserService) ChangeEmail

func (s *UserService) ChangeEmail(ctx context.Context, user *User, currentPassword string, newEmail string) (*User, error)

func (*UserService) ChangePassword

func (s *UserService) ChangePassword(ctx context.Context, user *User, currentPassword string, newPassword string) (*User, error)

func (*UserService) CreateUser

func (s *UserService) CreateUser(ctx context.Context, user *User) (*User, error)

func (*UserService) GetByID

func (s *UserService) GetByID(ctx context.Context, ID string) (*User, error)

type UserStore

type UserStore interface {
	Create(*User) (*User, error)
	Confirm(ID string) error
	Delete(ID string) error
	SetPassword(ID string, passwd string) error
	SetEmail(ID string, email string) error
}

UserStore defines all the methods usable to write data in the store for users

type Validator

type Validator interface {
	Validate(payload interface{}) error
}

Validator defines the bahaviour that should be implemented by any oject that can be used to validate payload.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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