Documentation ¶
Index ¶
- Variables
- func ExportJSON(st Storage) ([]byte, error)
- func HandleFavicon(w http.ResponseWriter, r *http.Request)
- func HandleWeb(w http.ResponseWriter, r *http.Request)
- func Migrate(dst, src Storage) error
- func NewNotepetServer(ip, port string, st Storage, handleweb bool, tokens ...string) (*http.Server, error)
- type APIClient
- func (ac *APIClient) Close() error
- func (ac *APIClient) Del(id NoteID) error
- func (ac *APIClient) ExportJSON() ([]byte, error)
- func (ac *APIClient) Get(ids ...NoteID) ([]Note, error)
- func (ac *APIClient) Put(n Note) (NoteID, error)
- func (ac *APIClient) Search(query string) ([]Note, error)
- func (ac *APIClient) Upd(id NoteID, n Note) (NoteID, error)
- type APIHandler
- type JSONFileStorage
- func (st *JSONFileStorage) Close() (err error)
- func (st *JSONFileStorage) Del(id NoteID) error
- func (st *JSONFileStorage) ExportJSON() ([]byte, error)
- func (st *JSONFileStorage) Get(ids ...NoteID) ([]Note, error)
- func (st *JSONFileStorage) Put(note Note) (NoteID, error)
- func (st *JSONFileStorage) Search(want string) ([]Note, error)
- func (st *JSONFileStorage) Upd(id NoteID, note Note) (NoteID, error)
- type Note
- type NoteID
- type PostgresStorage
- func (psql *PostgresStorage) Close() error
- func (psql *PostgresStorage) Del(id NoteID) error
- func (psql *PostgresStorage) ExportJSON() ([]byte, error)
- func (psql *PostgresStorage) Get(ids ...NoteID) ([]Note, error)
- func (psql *PostgresStorage) Put(n Note) (NoteID, error)
- func (psql *PostgresStorage) Search(query string) ([]Note, error)
- func (psql *PostgresStorage) Upd(id NoteID, n Note) (NoteID, error)
- type SQLiteStorage
- func (sqls *SQLiteStorage) Close() error
- func (sqls *SQLiteStorage) Del(id NoteID) error
- func (sqls *SQLiteStorage) ExportJSON() ([]byte, error)
- func (sqls *SQLiteStorage) Get(ids ...NoteID) ([]Note, error)
- func (sqls *SQLiteStorage) Put(n Note) (NoteID, error)
- func (sqls *SQLiteStorage) Search(query string) ([]Note, error)
- func (sqls *SQLiteStorage) Upd(id NoteID, n Note) (NoteID, error)
- type Storage
- func CreateSQLiteStorage(filename string) (Storage, error)
- func NewAPIClient(ip, port, path, apptoken string) (Storage, error)
- func OpenOrInitSQLiteStorage(filename string) (Storage, error)
- func OpenPostgresStorage(host, port, username, password, dbname string) (Storage, error)
- func OpenSQLiteStorage(filename string) (Storage, error)
Constants ¶
This section is empty.
Variables ¶
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") )
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 ¶
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
Types ¶
type APIClient ¶
APIClient represents http client fetching notes from notepet server. It implements Storage interface.
func (*APIClient) ExportJSON ¶
ExportJSON implements Storage
type APIHandler ¶
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
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.
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)
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)
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 NewAPIClient ¶
NewAPIClient returns instance of APIClient configured to send requests to specified ip address