notepet

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2021 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoNotesFound when no notes with requested NoteID are found in the storage
	ErrNoNotesFound = errors.New("error: no notes with such NoteID")
	// ErrCanNotAddEmptyNote is returned when trying to Put() note with empty body and title
	// into Storage
	ErrCanNotAddEmptyNote = errors.New("error: can not add empty note")
	// ErrStorageIsNil is returned when nil pointer is passed to NewAPIHandler or
	// NewNotepetServer
	ErrStorageIsNil = errors.New("can not use storage: storage is nil")
)
View Source
var BadNoteID = NoteID("nil")

BadNoteID is an invalid id. It is returned when method signature requires to return NoteID but there is no actual valid data to return.

Functions

func ExportJSON

func ExportJSON(st Storage) ([]byte, error)

ExportJSON requests all Notes from st Storage, serializes to JSON and returns byte array. Just use string(output) if string type is required.

func HandleFavicon

func HandleFavicon(w http.ResponseWriter, r *http.Request)

HandleFavicon is intended to be used to handle request to /favicon.ico

func HandleWeb

func HandleWeb(w http.ResponseWriter, r *http.Request)

func Migrate

func Migrate(dst, src Storage) error

Migrate copies all notes from src (source) Storage to dst (destination) Storage. If succesful the returned error in nil.

func NewNotepetServer

func NewNotepetServer(ip, port string, st Storage, handleweb bool, tokens ...string) (*http.Server, error)

NewNotepetServer returns instance of http.Server ready to run on ListenAndServe call

Types

type APIClient

type APIClient struct {
	Token      string
	HTTPClient *http.Client
	URL        url.URL
}

APIClient represents http client fetching notes from notepet server. It implements Storage interface.

func (*APIClient) Close

func (ac *APIClient) Close() error

Close implements Storage

func (*APIClient) Del

func (ac *APIClient) Del(id NoteID) error

Del implements Storage

func (*APIClient) ExportJSON

func (ac *APIClient) ExportJSON() ([]byte, error)

ExportJSON implements Storage

func (*APIClient) Get

func (ac *APIClient) Get(ids ...NoteID) ([]Note, error)

Get implements Storage

func (*APIClient) Put

func (ac *APIClient) Put(n Note) (NoteID, error)

Put implements Storage

func (*APIClient) Search

func (ac *APIClient) Search(query string) ([]Note, error)

Search implements Storage

func (*APIClient) Upd

func (ac *APIClient) Upd(id NoteID, n Note) (NoteID, error)

Upd implements Storage

type APIHandler

type APIHandler struct {
	Storage Storage
	Tokens  map[string]struct{}
}

APIHandler implements http.Handler ready to serve requests to API

func NewAPIHandler

func NewAPIHandler(st Storage, tokens ...string) (*APIHandler, error)

NewAPIHandler returns instance of http.Handler ready to run

func (*APIHandler) RegisterStorage

func (ah *APIHandler) RegisterStorage(st Storage) error

RegisterStorage makes APIHandler use the supplied Storage

func (*APIHandler) RegisterToken

func (ah *APIHandler) RegisterToken(token string)

RegisterToken adds token to globalValidTokens map so that server may use them for authentication

func (*APIHandler) ServeHTTP

func (ah *APIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServerHTTP implements http.Handler interface

type JSONFileStorage

type JSONFileStorage struct {
	Notes []Note
	// contains filtered or unexported fields
}

JSONFileStorage reads from json file and keeps objects in memory while the program is running Each change of objects list in memory is immediately flushed back to disk. This struct implements the Storage interface.

func CreateJSONFileStorage

func CreateJSONFileStorage(filename string) (*JSONFileStorage, error)

CreateJSONFileStorage initializes empty json file then creates and returns new Storage interface

func OpenJSONFileStorage

func OpenJSONFileStorage(filename string) (*JSONFileStorage, error)

OpenJSONFileStorage opens an existing sotrage and returns pointer to it

func OpenOrInitJSONFileStorage

func OpenOrInitJSONFileStorage(filename string) (*JSONFileStorage, error)

OpenOrInitJSONFileStorage returns Storage interface is file exists or initializes new storage with requested path If function returns an error this is an indication that file with requested path and name could not be created

func (*JSONFileStorage) Close

func (st *JSONFileStorage) Close() (err error)

Close flushes all notes to disk

func (*JSONFileStorage) Del

func (st *JSONFileStorage) Del(id NoteID) error

Del removes Note from Storage

func (*JSONFileStorage) ExportJSON

func (st *JSONFileStorage) ExportJSON() ([]byte, error)

ExportJSON returns a byte array of all notes in JSON format

func (*JSONFileStorage) Get

func (st *JSONFileStorage) Get(ids ...NoteID) ([]Note, error)

Get checks if Note with index i is present in the storage and returns relevant Note

func (*JSONFileStorage) Put

func (st *JSONFileStorage) Put(note Note) (NoteID, error)

Put adds Note to Storage

func (*JSONFileStorage) Search

func (st *JSONFileStorage) Search(want string) ([]Note, error)

Search removes leading and trailing spaces from request and matches the resulting substring against each note in the storage, checking body and title. Search is case insensitive.

func (*JSONFileStorage) Upd

func (st *JSONFileStorage) Upd(id NoteID, note Note) (NoteID, error)

Upd replaces Note with id with supplied Note note. Returns error if underlying io operation is unsucessful

type Note

type Note struct {
	ID         NoteID    `json:"id,omitempty"`
	Title      string    `json:"title,omitempty"`
	Body       string    `json:"body,omitempty"`
	Tags       string    `json:"tags,omitempty"`
	Sticky     bool      `json:"sticky,omitempty"`
	TimeStamp  time.Time `json:"timestamp,omitempty"`
	LastEdited time.Time `json:"lastedited,omitempty"`
}

Note is a basic structure keeping note data. Fields are self-explanatory.

func (Note) String

func (n Note) String() (out string)

type NoteID

type NoteID string

NoteID is a unique Id of each note as recorded by Storage

func (NoteID) String

func (id NoteID) String() string

type PostgresStorage

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

func (*PostgresStorage) Close

func (psql *PostgresStorage) Close() error

func (*PostgresStorage) Del

func (psql *PostgresStorage) Del(id NoteID) error

func (*PostgresStorage) ExportJSON

func (psql *PostgresStorage) ExportJSON() ([]byte, error)

func (*PostgresStorage) Get

func (psql *PostgresStorage) Get(ids ...NoteID) ([]Note, error)

func (*PostgresStorage) Put

func (psql *PostgresStorage) Put(n Note) (NoteID, error)

func (*PostgresStorage) Search

func (psql *PostgresStorage) Search(query string) ([]Note, error)

func (*PostgresStorage) Upd

func (psql *PostgresStorage) Upd(id NoteID, n Note) (NoteID, error)

type SQLiteStorage

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

func (*SQLiteStorage) Close

func (sqls *SQLiteStorage) Close() error

func (*SQLiteStorage) Del

func (sqls *SQLiteStorage) Del(id NoteID) error

func (*SQLiteStorage) ExportJSON

func (sqls *SQLiteStorage) ExportJSON() ([]byte, error)

func (*SQLiteStorage) Get

func (sqls *SQLiteStorage) Get(ids ...NoteID) ([]Note, error)

func (*SQLiteStorage) Put

func (sqls *SQLiteStorage) Put(n Note) (NoteID, error)

func (*SQLiteStorage) Search

func (sqls *SQLiteStorage) Search(query string) ([]Note, error)

func (*SQLiteStorage) Upd

func (sqls *SQLiteStorage) Upd(id NoteID, n Note) (NoteID, error)

type Storage

type Storage interface {
	// Get signature is intended to accept zero or one NoteID
	// if more than one NoteIDs specified the method may or may not
	// return second and subsequent ids depending on implementation
	Get(...NoteID) ([]Note, error)
	// Put accepts Note and should return NoteID if Note has been
	// successfully added to Storage
	Put(Note) (NoteID, error)
	// Upd accepts Note and should return NoteID if Note has been
	// successfully modified in Storage
	Upd(NoteID, Note) (NoteID, error)
	// Del deletes Note with specified NoteID. If delete was successful
	// it should return nil, error otherwise.
	Del(NoteID) error
	// Search looks up Notes containing specified string. It should
	// return error if no Notes have been found or if other error occured.
	Search(string) ([]Note, error)
	// Close should be used when Storage is no longer needed (to close
	// network connection, flush file to disk etc.)
	// If Close has not been called data is not guaranteed to be consistent.
	Close() error
}

Storage interface represents any type of storage for Note objects.

func CreateSQLiteStorage

func CreateSQLiteStorage(filename string) (Storage, error)

func NewAPIClient

func NewAPIClient(ip, port, path, apptoken string) (Storage, error)

NewAPIClient returns instance of APIClient configured to send requests to specified ip address

func OpenOrInitSQLiteStorage

func OpenOrInitSQLiteStorage(filename string) (Storage, error)

func OpenPostgresStorage

func OpenPostgresStorage(host, port, username, password, dbname string) (Storage, error)

func OpenSQLiteStorage

func OpenSQLiteStorage(filename string) (Storage, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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