api

package
v0.0.3-alpha Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2021 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package api is an entry point to go-pb API. It defines all the types and interfaces that needed to be implemented.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HTTPError

type HTTPError struct {
	Code    int    `json:"code"`    // HTTP status code
	Message string `json:"message"` // Error message
}

HTTPError represents an error that endpoints send to the consumers.

type Paste

type Paste struct {
	ID              int64     `json:"id" gorm:"primaryKey"`
	Title           string    `json:"title"`
	Body            string    `json:"body"`
	Expires         time.Time `json:"expires" gorm:"index"`
	DeleteAfterRead bool      `json:"delete_after_read"`
	Password        string    `json:"password"`
	Created         time.Time `json:"created"`
	Syntax          string    `json:"syntax"`
	UserID          int64     `json:"user_id" gorm:"index default:null"`
	User            User      `json:"-"`
}

Paste is a the type that represents a single paste as it is stored in the database.

func (*Paste) Expiration

func (p *Paste) Expiration() string

Expiration returns a "humanized" duration between now and the expiry date stored in `Expires`. For example: "in 25 minutes" or "in 2 months".

func (*Paste) URL

func (p *Paste) URL() string

URL generates a base62 encoded string from the ID.

type PasteForm

type PasteForm struct {
	Title           string `json:"title" form:"title"`
	Body            string `json:"body" form:"body" binding:"required"`
	Expires         string `json:"expires" form:"expires" binding:"required"`
	DeleteAfterRead bool   `json:"delete_after_read" form:"delete_after_read" binding:"-"`
	Password        string `json:"password" form:"password"`
	Syntax          string `json:"syntax" form:"syntax" binding:"required"`
	UserID          int64  `json:"user_id"`
}

PasteForm represents the data that we expect to recieve when the user submitts the form.

type PasteService

type PasteService interface {
	// Get returns a paste by ID.
	// Please note that error should be not nil only if there is an actual
	// error. Paste not found is not an error. It should be expected that if
	// there is no paste with the ID that you provided the return value for
	// the paste will be nil.
	Get(id int64) (*Paste, error)
	// Create creates new paste, saves it to the storage and returns it.
	Create(p PasteForm) (*Paste, error)
	// Delete deletes a paste by ID.
	// If paste with the provided ID doesn't exist this method does nothing,
	// it will not return an error in such case.
	Delete(id int64) error
	// List returns all the pastes with specified user ID.
	List(uid int64) []Paste
}

PasteService is the interface that defines methods for working with Pastes.

Implementations should define the underlying storage such as database, plain files or even memory.

type User

type User struct {
	ID           int64     `json:"id" gorm:"primaryKey"`
	Username     string    `json:"username" gorm:"index"`
	Email        string    `json:"email" gorm:"index"`
	PasswordHash string    `json:"-"`
	CreatedAt    time.Time `json:"-"`
}

User is a type that represents a single user as it is stored in the database

type UserInfo

type UserInfo struct {
	ID       int64  `json:"user_id"`
	Username string `json:"username"`
	Token    string `json:"token"`
}

UserInfo represents the data that we send back in response to various operation such as Authenticate or Validate.

type UserLogin

type UserLogin struct {
	Username string `json:"username" form:"username" binding:"required"`
	Password string `json:"password" form:"password" binding:"required"`
}

UserLogin represents the data that we expect to recieve from the login page.

type UserRegister

type UserRegister struct {
	Username   string `json:"username" form:"username" binding:"required"`
	Email      string `json:"email" form:"email" binding:"required"`
	Password   string `json:"password" form:"password" binding:"required"`
	RePassword string `json:"repassword" form:"repassword" binding:"required"`
}

UserRegister represents the data that we expect to recieve from the registration page.

type UserService

type UserService interface {
	// Creates a new user.
	// Returns an error if user with the same username or the same email
	// already exist or if passwords do not match.
	Create(u UserRegister) error
	// Authenticates a user by validating that it exists and hash of the
	// provided password matches. On success returns a JWT token.
	Authenticate(u UserLogin) (UserInfo, error)
	// Validates given token for a given user.
	Validate(u User, t string) (UserInfo, error)
}

UserService is the interface that defines methods to work with Users

Directories

Path Synopsis
auth
memory
Package memory provides an implementation of api.UserService that uses memory as a storage.
Package memory provides an implementation of api.UserService that uses memory as a storage.
sqldb
Package sqlite provides an implementation of api.UserService that uses sqlite database as a storage.
Package sqlite provides an implementation of api.UserService that uses sqlite database as a storage.
Package base62 provides simple implementation of Base62 encoding/ecoding.
Package base62 provides simple implementation of Base62 encoding/ecoding.
Package http provides an ApiServer type - a server that uses api.PasteService and api.UserService to provide many useful endpoints.
Package http provides an ApiServer type - a server that uses api.PasteService and api.UserService to provide many useful endpoints.
paste
memory
Package memory provides methods to work with pastes using memory as a storage.
Package memory provides methods to work with pastes using memory as a storage.
sqldb
Package sqlite provides implementation of api.PasteService that uses sqlite database as a storage.
Package sqlite provides implementation of api.PasteService that uses sqlite database as a storage.

Jump to

Keyboard shortcuts

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