Documentation ¶
Index ¶
- Constants
- Variables
- func ContextWithLoggedUser(parent context.Context, loggedUser string) context.Context
- func GetLoggedUser(ctx context.Context) string
- func HandleHttpAddUsersRequest(w http.ResponseWriter, r *http.Request)
- func HandleHttpDeleteUserRequest(w http.ResponseWriter, r *http.Request)
- func HandleHttpGetUserListRequest(w http.ResponseWriter, r *http.Request)
- func HandleHttpGetUserRequest(w http.ResponseWriter, r *http.Request)
- func HandleHttpLoginRequest(w http.ResponseWriter, r *http.Request)
- func HandleHttpUpdateUserRequest(w http.ResponseWriter, r *http.Request)
- func Serve(config *Config) error
- type APIHandler
- type BaseHandler
- func (b *BaseHandler) AddUsers(ctx context.Context, reader io.Reader) error
- func (b *BaseHandler) DeleteUser(ctx context.Context, userId string) error
- func (b *BaseHandler) GetUser(ctx context.Context, userId string) (UserData, error)
- func (b *BaseHandler) GetUserList(ctx context.Context, opts ListOptions) (*UserDataList, error)
- func (b *BaseHandler) Login(ctx context.Context, user string, password string) (bool, error)
- func (b *BaseHandler) UpdateUser(ctx context.Context, userId string, userData UserData) error
- type ConcurrentUserDataProcessingRunner
- type Config
- type Files
- type ListOptions
- type UserData
- type UserDataCallback
- type UserDataList
- type UserDataProcessingResult
- type UserDataProcessor
- type UserDataProcessorFunc
- type UserDataProvider
- type UserDataStore
Constants ¶
const ( // LoginEndpoint is the HTTP API endpoint to initialise an authenticated session LoginEndpoint = "/login" // AddUsersEndpoint is the HTTP API endpoint to add users AddUsersEndpoint = "/add/users" // DeleteUserEndpoint is the HTTP API endpoint to delete data of a user DeleteUserEndpoint = "/delete/user/{id}" // GetUserEndpoint is the HTTP API endpoint to get user data GetUserEndpoint = "/user/{id}" // ListUsersEndpoint is the HTTP API endpoint to add users ListUsersEndpoint = "/users/list" // UpdateUserEndpoint is the HTTP API endpoint to add users UpdateUserEndpoint = "/user/{id}" )
const (
DefaultUserListCount = 5
)
Variables ¶
var ( BadInput = errors.New("bad input") AuthenticationRequired = errors.New("authentication required") Forbidden = errors.New("forbidden") NotAuthorized = errors.New("not authorized") NotFound = errors.New("not found") Internal = errors.New("internal") )
var Env = struct { DataStore UserDataStore Files Files AdminPassword string CookiesStore sessions.Store }{ DataStore: NewUserDataMemoryStore(), Files: NewMemoryFiles(), CookiesStore: sessions.NewCookieStore(), }
Functions ¶
func ContextWithLoggedUser ¶
ContextWithLoggedUser creates a new context that holds loggedUser in addition of the parent values
func GetLoggedUser ¶
GetLoggedUser extracts logged user name from context values
func HandleHttpAddUsersRequest ¶
func HandleHttpAddUsersRequest(w http.ResponseWriter, r *http.Request)
HandleHttpAddUsersRequest initializes an APIHandler and calls its APIHandler.AddUsers with the request body content The request body content is expected to be an encoded JSON object list
func HandleHttpDeleteUserRequest ¶
func HandleHttpDeleteUserRequest(w http.ResponseWriter, r *http.Request)
HandleHttpDeleteUserRequest initializes an APIHandler and calls its APIHandler.DeleteUser with userId extracted from the request URI path
func HandleHttpGetUserListRequest ¶
func HandleHttpGetUserListRequest(w http.ResponseWriter, r *http.Request)
HandleHttpGetUserListRequest initializes an APIHandler and calls its APIHandler.GetUserList "r" is expected to be a POST request with an encoded JSON object list as body The returned value by GetUserList is set as the HTTP response body
func HandleHttpGetUserRequest ¶
func HandleHttpGetUserRequest(w http.ResponseWriter, r *http.Request)
HandleHttpGetUserRequest initializes an APIHandler and calls its APIHandler.GetUser with userId extracted from the request URI path The returned value by GetUser is set as the HTTP response body
func HandleHttpLoginRequest ¶
func HandleHttpLoginRequest(w http.ResponseWriter, r *http.Request)
HandleHttpLoginRequest initializes an APIHandler and calls its APIHandler.Login method with user credentials parsed from the request body
func HandleHttpUpdateUserRequest ¶
func HandleHttpUpdateUserRequest(w http.ResponseWriter, r *http.Request)
HandleHttpUpdateUserRequest initializes an APIHandler and calls its APIHandler.UpdateUser with userId extracted from the request URI path and the request content body. The request body content is expected to be a JSON encoded UserData object
Types ¶
type APIHandler ¶
type APIHandler interface { // Login creates an authenticated session if credentials match a registered user Login(ctx context.Context, username string, password string) (bool, error) // AddUsers parses user data list from the "reader" stream and store them in the database AddUsers(ctx context.Context, reader io.Reader) error // DeleteUser deletes the user identified by "userId' from the database DeleteUser(ctx context.Context, userId string) error // GetUser retrieves the data of the user identified by "userId" GetUser(ctx context.Context, userId string) (UserData, error) // GetUserList retrieves a set of users from the database GetUserList(ctx context.Context, opts ListOptions) (*UserDataList, error) // UpdateUser updates the data of the user identified by "userId" UpdateUser(ctx context.Context, userId string, userData UserData) error }
func NewAPIHandler ¶
func NewAPIHandler() (handler APIHandler)
NewAPIHandler constructs an API handler pipe
type BaseHandler ¶
type BaseHandler struct {
Next APIHandler
}
BaseHandler defines a API calls handling pipe
func (*BaseHandler) DeleteUser ¶
func (b *BaseHandler) DeleteUser(ctx context.Context, userId string) error
func (*BaseHandler) GetUserList ¶
func (b *BaseHandler) GetUserList(ctx context.Context, opts ListOptions) (*UserDataList, error)
func (*BaseHandler) UpdateUser ¶
type ConcurrentUserDataProcessingRunner ¶
type ConcurrentUserDataProcessingRunner struct { Provider UserDataProvider Processor UserDataProcessor TasksResultsSignals chan<- chan UserDataProcessingResult ResultSignal chan<- chan error }
ConcurrentUserDataProcessingRunner starts a routine. And for each provided UserData calls the callback and returns the result to the caller through the "results" channel
func (ConcurrentUserDataProcessingRunner) Run ¶
func (r ConcurrentUserDataProcessingRunner) Run()
type Files ¶
type Files interface { Save(userId string, data string) error Delete(userId string) error Get(userId string) (string, error) }
Files is a convenience for UserData file persistence
func NewDirFiles ¶
func NewMemoryFiles ¶
func NewMemoryFiles() Files
type ListOptions ¶
ListOptions defines a range limit
type UserDataCallback ¶
UserDataCallback is function that handle a UserData
type UserDataList ¶
type UserDataList struct { Offset int `json:"offset"` UserDataList []UserData `json:"user_data_list"` }
UserDataList holds info about a range of UserData objects
type UserDataProcessingResult ¶
UserDataProcessingResult holds info about UserData processing result
type UserDataProcessor ¶
UserDataProcessor is a convenience for user data processor
type UserDataProcessorFunc ¶
UserDataProcessorFunc is a function that implements of UserDataProcessor
func (UserDataProcessorFunc) ProcessData ¶
func (f UserDataProcessorFunc) ProcessData(data UserData) (UserData, error)
type UserDataProvider ¶
type UserDataProvider func(callback UserDataCallback) error
type UserDataStore ¶
type UserDataStore interface { // Save saves user data Save(data UserData) error // Delete deletes the userData matching the given id Delete(id string) error // Get retrieves userData matching the given id Get(id string) (UserData, error) // ListForUser fetches a range of UserData that matches the userId // and pass each pass each parsed userdata to the callback ListForUser(userId string, offset, count int, callback UserDataCallback) error // List fetches a range of UserData and pass each pass each // parsed userdata to the callback List(offset, count int, callback UserDataCallback) error }
UserDataStore is a convenience for UserData persistence management
func NewMongoUserDataStore ¶
func NewMongoUserDataStore(uri string) (UserDataStore, error)
func NewUserDataMemoryStore ¶
func NewUserDataMemoryStore() UserDataStore
NewUserDataMemoryStore constructs a memory based UserDataStore