library

package module
v0.0.0-...-8cbf35a Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2021 License: MIT Imports: 22 Imported by: 0

README

Library GRPC server and GRPC gateway CRUD api

This is a GRPC server and a GRPC gateway which implements a CRUD rest API.

Endpoints:

Method Endpoint Description
POST /books Create a book
GET /books Get list of all books
GET /books/{isbn} Get user by isbn
PUT /books/{isbn} Update user by isbn
DELETE /books/{isbn} Delete user by isbn

Run locally

  • Clone the repository
git clone https://github.com/NicolaiMordrup/library.git
  • Open a terminal in the root directory of your code and run the following command to start the application.
Run with local go installtion
go run cmd/main.go

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	LastNamePattern = regexp.MustCompile(`^[a-zA-Z]+(?:\s+[a-zA-Z]+)*$`)
)

The regex patterns for the validate function

Functions

func EnsureSchema

func EnsureSchema(db *sql.DB) error

EnsureSchema runs migrations from the embedded filesystem against the provided database connection.

func NewDB

func NewDB(dbPath string) (*sql.DB, error)

NewDB opens a connection to the sqlite database.

func NewServer

func NewServer(
	dataBase *sql.DB,
	logger *zap.SugaredLogger,
	minDurationTimeBetweenUpdates time.Duration,
) *libraryServiceServer

NewServer creates a new GRPC server instance.

func RunGRPCGateway

func RunGRPCGateway(
	ctx context.Context,
	log *zap.SugaredLogger,
	addr string,
	grpcAddr string,
	registerHandlersFunc func(context.Context, *runtime.ServeMux, *grpc.ClientConn) error,
) error

RunGRPCGateway initlaizes and starts a grpc gateway

Types

type Author

type Author struct {
	FirstName string `json:"firstName"`
	LastName  string `json:"lastName"`
}

Struct for the books Author properties.

type Book

type Book struct {
	ISBN       string    `json:"isbn"` // The identification of the books
	Title      string    `json:"title"`
	CreateTime time.Time `json:"createTime"` // The time of creation of book instance
	UpdateTime time.Time `json:"updateTime"` // The time of update for book instance
	Publisher  string    `json:"publisher"`
	Author     Author    `json:"author"` // Embedded author struct
}

Struct for the book properties.

func NewBookFromProto

func NewBookFromProto(b *librarypb.Book) Book

NewBookFromProto converts a *librarypb.Book which is on the proto fromat to the Book instance such that the database can deal with it.

func (*Book) AsProto

func (b *Book) AsProto() *librarypb.Book

AsProto converts a Book instance to the *librarypb.Book which is of proto instance such that the response can deal with it.

type DBStorage

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

func (*DBStorage) DeleteBookFromDB

func (storage *DBStorage) DeleteBookFromDB(isbn string) error

Deletes a specific book from the database

func (*DBStorage) FindSpecificBook

func (storage *DBStorage) FindSpecificBook(isbnToFind string) Book

Reads from the database and find a specific book that exists.

func (*DBStorage) InsertIntoDatabase

func (storage *DBStorage) InsertIntoDatabase(b Book) error

DatabaseQuery Prepers a database query and executes the query on the database. It takes as input a query string and gives as output the rows

func (*DBStorage) ReadDatabaseList

func (storage *DBStorage) ReadDatabaseList() []Book

ReadDatabase reads the information that we get from the database.

func (*DBStorage) ReadRows

func (storage *DBStorage) ReadRows(rows *sql.Rows, b []Book) []Book

ReadRows gets the information from the query and stores it in the Book slice.

Directories

Path Synopsis
gen
proto/go/librarypb
Package librarypb is a reverse proxy.
Package librarypb is a reverse proxy.

Jump to

Keyboard shortcuts

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