app

package module
v0.0.0-...-b05bab5 Latest Latest
Warning

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

Go to latest
Published: May 6, 2019 License: Apache-2.0 Imports: 31 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CORSMiddleware

func CORSMiddleware(next http.Handler) http.Handler

CORSMidlleware writer cors headers to requests

func InitPostgres

func InitPostgres(ctx context.Context, o DbOptions) (db *sql.DB, err error)

Types

type Admin

type Admin struct {
	Email              string `json:"email" db:"email"`
	Name               string `json:"name" db:"name"`
	HashedPassword     string `json:"hashed_password" db:"hashed_password"`
	IsSuperUser        bool   `json:"is_superuser" db:"is_superuser"`
	IsActive           bool   `json:"is_active" db:"is_active"`
	EmailConfirmed     bool   `json:"email_confirmed" db:"email_confirmed"`
	AuthToken          string `json:"auth_token" db:"auth_token"`
	PasswordResetToken string `json:"password_reset_token" db:"password_reset_token"`
	// contains filtered or unexported fields
}

Admin represents registered dashboard Helpdesker

func NewAdmin

func NewAdmin(email string, password string, isSuperUser bool) (a *Admin)

type AuthMail

type AuthMail struct {
	Receiver string
	Subject  string
	Body     string
	URL      string
	URLName  string
}

AuthMail represents data needed for user/admin authorization email.

type BaseBot

type BaseBot struct {

	// below is duplicating of Connector channels, but since real bots
	// inherit base bot and used as interfaces, whose channes needs to be wrapped
	// in separate struct for further usage
	Input  chan []byte  // messages from Customers
	Output chan Message // messages to Customers
	Error  chan error   // Errors for admins
	// contains filtered or unexported fields
}

BaseBot is abstraction for useful business logic methods, such as authorization sequence etc. Every bot is basicly a REPL, with one difference: sometime your command result is given by helpdesker, and sometimes it's fully automatic. That single fact explains api design and provides ease of testing. Use it as embedded struct for various helpers and avoiding duplicating code. See MockBot implementation below

func (*BaseBot) Connector

func (b *BaseBot) Connector() *Connector

func (*BaseBot) HashToken

func (b *BaseBot) HashToken(try int) (hash string)

func (*BaseBot) Stop

func (b *BaseBot) Stop()

type Bot

type Bot interface {
	Run(onExit func())
	Stop()
	Connector() *Connector
	HashToken(int) string
	GetFileDirectURL(string) (string, error)
}

type BotOptions

type BotOptions struct {
	T              BotType
	Token          string
	EmailAuth      bool
	AllowedDomains []string
	Name           string
}

BotOptions is type/token pair which is needed for bot to start working

type BotType

type BotType string

BotType represents social network/messanger which bot uses

const (
	Telegram BotType = "TgBot"
	// NOTE slack support is not implemented yet
	Slack BotType = "SlackBot"
)

type Client

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

Client is a middleman between the websocket connection and the hub.

type Connector

type Connector struct {
	Input  chan []byte    // messages from Customers
	Output chan<- Message // messages to Customers
	Error  chan error     // Errors for admins
}

type CustomJWTClaims

type CustomJWTClaims struct {
	CurrentBot string `json:"bot"`
	jwt.StandardClaims
}

type DbOptions

type DbOptions struct {
	User     string
	Password string
	Host     string
	Port     string
	DbName   string
}

type DbType

type DbType int

DbType describes supported databases

const (
	Postgres DbType = iota
	Sqllite
)

type Hub

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

func NewHub

func NewHub(b chan []byte) *Hub

type Mailer

type Mailer interface {
	SendAuthMail(AuthMail) error
}

Mailer interface provides api for sending mails to users

func NewMockMailer

func NewMockMailer(t *templator.Templator, l log.Logger) Mailer

NewMockMailer initializes mock email client. It logs new messages to provided logger. NOTE: I'm not sure tampletor need to be in args.

type Message

type Message struct {
	FromAdmin   bool `json:"from_bot"`
	IsBroadcast bool `json:"is_broadcast"`
	// I'm not sure what message id's are unique
	Text           string    `json:"text"`
	MessageID      int       `json:"message_id"`
	UserID         int       `json:"user_id"`
	ChatID         int64     `json:"chat_id"`
	Date           int       `json:"send_date"`
	ForwardFrom    int       `json:"forward_from"`
	ForwardDate    int       `json:"forward_date"`
	ReplyToMessage int       `json:"reply_to_message_id"`
	EditDate       int       `json:"edit_date"`
	CreatedAt      time.Time `json:"created_at"`
	UpdatedAt      time.Time `json:"updated_at"`
	// those are just file ids for FileProxy, currently only Photo and Document
	// are supported
	DocumentID string `json:"file_id"`
	PhotoID    string `json:"photo_id"`
	// contains filtered or unexported fields
}

Message is atomic piece of communication between User and Admin

type MockBot

type MockBot struct {
	*BaseBot
}

MockBot takes reader/writer (ex. goes os.Stdin and os.Stdout) and simulates bot behavior and logic.

func NewMockBot

func NewMockBot(r io.Reader, w io.Writer) (*MockBot, error)

NewMockBot initializes bot for development/testing purposes

func (*MockBot) Run

func (m *MockBot) Run()

Run ...

type Options

type Options struct {
	Root          string
	Domain        string
	Port          string
	EmailServer   string
	EmailAddress  string
	EmailPassword string
	DatabaseURL   string
	ServeStatic   bool
	StaticFiles   string
	DbOptions     DbOptions
	Secret        string
	TgBotTokens   string
}

Options represents server initialization options

type Server

type Server struct {
	QuitCh chan struct{}
	Ctx    context.Context
	Cancel func()

	Secret string
	DB     *sql.DB
	// contains filtered or unexported fields
}

Server contains gosupport server state

func InitServer

func InitServer(
	l kitlog.Logger,
	t *templator.Templator,
	m Mailer,
	db *sql.DB,
	o Options,
) *Server

InitServer initialize new server instance with provided options & logger

func (*Server) Add

func (s *Server) Add(b Bot)

func (*Server) InitRoutes

func (s *Server) InitRoutes(router chi.Router, static string)

InitRoutes initializes url schema. Separate function argument for routes is used to escape bugs there server tries to init routes without provided chi.Mux

func (*Server) ListenAndServe

func (s *Server) ListenAndServe()

ServeRouter starts http server

func (*Server) RenderTemplate

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

func (*Server) RunBots

func (s *Server) RunBots()

func (*Server) ServeWs

func (s *Server) ServeWs(hub *Hub, w http.ResponseWriter, r *http.Request)

ServeWs handles websocket requests from the peer.

func (*Server) Shutdown

func (s *Server) Shutdown()

func (*Server) StopBots

func (s *Server) StopBots()

type TgBot

type TgBot struct {
	*BaseBot
	// contains filtered or unexported fields
}

TgBot represents bot in telegram messanger

func NewTgBot

func NewTgBot(ctx context.Context, s *Server, token string) (t *TgBot, err error)

NewTgBot ... FIXME

func (*TgBot) GetFileDirectURL

func (t *TgBot) GetFileDirectURL(fileID string) (string, error)

func (*TgBot) Run

func (t *TgBot) Run(onExit func())

Run starts polling on telegram bot

type User

type User struct {
	UserID            int    `json:"userid"`
	ChatID            int64  `json:"chatid"`
	Email             string `json:"email"`
	Name              string `json:"name"`
	Username          string `json:"username"`
	HasUnreadMessages bool   `json:"has_unread_messages"`
	// AuthToken used for email authorization
	AuthToken      []byte `json:"authtoken"`
	IsAuthorized   bool   `json:"isauthorized"`
	IsTokenExpired bool   `json:"is_token_expired"`
	LastMessageAt  int64  `json:"lastMessageAt"`
	// FIXME
	LastMessage Message   `json:"last_message"`
	UserPhotoID string    `json:"user_photo_id"`
	IsActive    bool      `json:"is_active"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}

User represents single chat endpoint of communication

func (User) String

func (u User) String() string

Jump to

Keyboard shortcuts

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