server

package
v0.0.0-...-897d125 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2018 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KeySessionID = "SESSION-ID"

	// key for session value which is user loggedin state.
	KeyLoginState = "LOGIN-STATE"

	// seconds in 365 days, where 86400 is a seconds in 1 day
	SecondsInYear = 86400 * 365
)
View Source
const (
	// keys for the URL parameters. e.g. /root/:param_name
	ParamKeyUserID = "user_id"
	ParamKeyRoomID = "room_id"
)
View Source
const KeyLoggedInUserID = "SESSION-USER-ID"

KeyLoggedInUserID is the key for the logged-in user id in the echo.Context. It is set when user is logged-in through LoginHandler.

Variables

View Source
var DefaultConfig = Config{
	HTTP:                  "localhost:8080",
	ChatAPIPrefix:         "",
	StaticHandlerPrefix:   "",
	StaticFileDir:         "",
	EnableServeStaticFile: true,
	ShowRoutes:            true,
}

DefaultConfig is default configuration for the server.

View Source
var DefaultOptions = session.Options{
	HttpOnly: true,
}
View Source
var (
	// ErrAPIRequireLoginFirst indicates that unauthenticated error message with htto status code.
	ErrAPIRequireLoginFirst = NewHTTPError(http.StatusForbidden, "require login first")
)

Functions

func CreateServerFromInfra

func CreateServerFromInfra(repos domain.Repositories, qs *chat.Queryers, ps chat.Pubsub, conf *Config) (*Server, DoneFunc)

CreateServerFromInfra creates server with infrastructure dependencies. It returns created server and finalize function. a nil config is OK and use DefaultConfig insteadly.

func ListenAndServe

func ListenAndServe(chatCmd chat.CommandService, chatQuery chat.QueryService, chatHub chat.Hub, login chat.LoginService, conf *Config) error

It starts server process using default server with user config. A nil config is OK and use DefaultConfig insteadly. It blocks until the process occurs any error and return the error.

func LoggedInUserID

func LoggedInUserID(c echo.Context) (uint64, bool)

get logged in user id which is valid after LoginHandler.Filter. the second returned value is false if logged in user id is not found.

func NewHTTPError

func NewHTTPError(statusCode int, msg ...interface{}) *echo.HTTPError

Wrapper function for the echo.HTTPError. Because echo.DefaultHTTPHandler handles only string message in the echo.HTTPError, This converts error types to string.

Types

type Config

type Config struct {
	// HTTP service address for the server.
	// The format is `[host]:[port]`, e.g. localhost:8080.
	HTTP string

	// Prefix of URI for the chat API.
	// e.g. given ChatAPIPrefix = `/api` and chat API `/chat/rooms`,
	// the prefixed chat API is `/api/chat/rooms`.
	ChatAPIPrefix string

	// Prefix of URI for the static file server.
	//
	// Example: given local html file `/www/index.html`,
	// StaticHandlerPrefix = "/www" and StaticHandlerPrefix = "/static",
	// the requesting the server with URI `/static/index.html` responds
	// the html content of `/www/index.html`.
	StaticHandlerPrefix string

	// root directory to serve static files.
	StaticFileDir string

	// indicates whether serving static files is enable.
	// if false, StaticHandlerPrefix and StaticFileDir do not
	// affect the server.
	EnableServeStaticFile bool

	// show all of URI routes at server starts.
	ShowRoutes bool
}

Configuration for server behavior.

func (*Config) Validate

func (c *Config) Validate() error

Validate checks whether the all of field values are correct format.

type DoneFunc

type DoneFunc func()

DoneFunc is function to be called after all of operations are done.

type LoginHandler

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

LoginHandler handles login requests. it holds logged-in users, so that each request can reference any logged-in user.

func NewLoginHandler

func NewLoginHandler(ls chat.LoginService, secretKeyPairs ...[]byte) *LoginHandler

func (*LoginHandler) Filter

func (lh *LoginHandler) Filter() echo.MiddlewareFunc

Filter is a middleware which filters unauthenticated request.

it sets logged-in user's id for echo.Context using KeyLoggedInUserID when the request is authenticated.

func (*LoginHandler) GetLoginState

func (lh *LoginHandler) GetLoginState(c echo.Context) error

func (*LoginHandler) IsLoggedInRequest

func (lh *LoginHandler) IsLoggedInRequest(c echo.Context) bool

func (*LoginHandler) Login

func (lh *LoginHandler) Login(c echo.Context) error

func (*LoginHandler) Logout

func (lh *LoginHandler) Logout(c echo.Context) error

func (*LoginHandler) Middleware

func (lh *LoginHandler) Middleware() echo.MiddlewareFunc

Middleware returns echo.MiddlewareFunc. it should be registered for echo.Server to use this LoginHandler.

func (*LoginHandler) Session

func (lh *LoginHandler) Session(c echo.Context) (*LoginState, bool)

it returns loginState as session state. the second returned value is true when LoginState exists.

type LoginState

type LoginState struct {
	LoggedIn   bool   `json:"logged_in"`
	RememberMe bool   `json:"remember_me"`
	UserID     uint64 `json:"user_id"`
	ErrorMsg   string `json:"error,omitempty"`
}

type RESTHandler

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

func NewRESTHandler

func NewRESTHandler(chatCmd chat.CommandService, chatQuery chat.QueryService) *RESTHandler

func (*RESTHandler) AddRoomMember

func (rest *RESTHandler) AddRoomMember(e echo.Context) error

func (*RESTHandler) CreateRoom

func (rest *RESTHandler) CreateRoom(e echo.Context) error

func (*RESTHandler) DeleteRoom

func (rest *RESTHandler) DeleteRoom(e echo.Context) error

func (*RESTHandler) GetRoomInfo

func (rest *RESTHandler) GetRoomInfo(e echo.Context) error

func (*RESTHandler) GetRoomMessages

func (rest *RESTHandler) GetRoomMessages(e echo.Context) error

func (*RESTHandler) GetUnreadRoomMessages

func (rest *RESTHandler) GetUnreadRoomMessages(e echo.Context) error

func (*RESTHandler) GetUserInfo

func (rest *RESTHandler) GetUserInfo(e echo.Context) error

func (*RESTHandler) PostRoomMessage

func (rest *RESTHandler) PostRoomMessage(e echo.Context) error

func (*RESTHandler) ReadRoomMessages

func (rest *RESTHandler) ReadRoomMessages(e echo.Context) error

func (*RESTHandler) RemoveRoomMember

func (rest *RESTHandler) RemoveRoomMember(e echo.Context) error

type Server

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

it represents server which can accepts chat room and its clients.

func NewServer

func NewServer(chatCmd chat.CommandService, chatQuery chat.QueryService, chatHub chat.Hub, login chat.LoginService, conf *Config) *Server

it returns new constructed server with config. nil config is ok and use DefaultConfig insteadly.

func (*Server) Handler

func (s *Server) Handler() http.Handler

Handler returns http.Handler interface in the server.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

it starts server process. it blocks until process occurs any error and return the error.

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

type UserForm

type UserForm struct {
	Name       string `json:"name" form:"name" query:"name"`
	Password   string `json:"password" form:"password" query:"password"`
	RememberMe bool   `json:"remember_me" form:"remember_me" query:"remember_me"`
}

Jump to

Keyboard shortcuts

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