http

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2021 License: MIT Imports: 31 Imported by: 0

Documentation

Index

Constants

View Source
const SessionCookieName = "session"

SessionCookieName is the name of the cookie used to store the session.

View Source
const ShutdownTimeout = 1 * time.Second

ShutdownTimeout is the time given for outstanding requests to finish before shutdown.

Variables

This section is empty.

Functions

func Error

func Error(w http.ResponseWriter, r *http.Request, err error)

Error prints & optionally logs an error message.

func ErrorStatusCode

func ErrorStatusCode(code string) int

ErrorStatusCode returns the associated HTTP status code for a WTF error code.

func FromErrorStatusCode

func FromErrorStatusCode(code int) string

FromErrorStatusCode returns the associated WTF code for an HTTP status code.

func ListenAndServeDebug

func ListenAndServeDebug() error

ListenAndServeDebug runs an HTTP server with /debug endpoints (e.g. pprof, vars).

func ListenAndServeTLSRedirect

func ListenAndServeTLSRedirect(domain string) error

ListenAndServeTLSRedirect runs an HTTP server on port 80 to redirect users to the TLS-enabled port 443 server.

func LogError

func LogError(r *http.Request, err error)

LogError logs an error with the HTTP route information.

func SetFlash

func SetFlash(w http.ResponseWriter, s string)

SetFlash sets the flash cookie for the next request to read.

Types

type Client

type Client struct {
	URL string
}

Client represents an HTTP client.

func NewClient

func NewClient(u string) *Client

NewClient returns a new instance of Client.

type DialService

type DialService struct {
	Client *Client
}

DialService implements the wtf.DialService over the HTTP protocol.

func NewDialService

func NewDialService(client *Client) *DialService

NewDialService returns a new instance of DialService.

func (*DialService) AverageDialValueReport

func (s *DialService) AverageDialValueReport(ctx context.Context, start, end time.Time, interval time.Duration) (*wtf.DialValueReport, error)

AverageDialValueReport is not implemented by the HTTP service.

func (*DialService) CreateDial

func (s *DialService) CreateDial(ctx context.Context, dial *wtf.Dial) error

CreateDial creates a new dial and assigns the current user as the owner. The owner will automatically be added as a member of the new dial.

func (*DialService) DeleteDial

func (s *DialService) DeleteDial(ctx context.Context, id int) error

DeleteDial permanently removes a dial by ID. Only the dial owner may delete a dial. Returns ENOTFOUND if dial does not exist. Returns EUNAUTHORIZED if user is not the dial owner.

func (*DialService) FindDialByID

func (s *DialService) FindDialByID(ctx context.Context, id int) (*wtf.Dial, error)

FindDialByID retrieves a single dial by ID along with associated memberships. Only the dial owner & members can see a dial. Returns ENOTFOUND if dial does not exist or user does not have permission to view it.

func (*DialService) FindDials

func (s *DialService) FindDials(ctx context.Context, filter wtf.DialFilter) ([]*wtf.Dial, int, error)

FindDials retrieves a list of dials based on a filter. Only returns dials that the user owns or is a member of. Also returns a count of total matching dials which may different from the number of returned dials if the "Limit" field is set.

func (*DialService) SetDialMembershipValue added in v0.1.1

func (s *DialService) SetDialMembershipValue(ctx context.Context, dialID, value int) error

SetDialMembershipValue sets the value of the user's membership in a dial. This works the same as calling UpdateDialMembership() although it doesn't require that the user know their membership ID. Only the dial ID.

Returns ENOTFOUND if the membership does not exist.

func (*DialService) UpdateDial

func (s *DialService) UpdateDial(ctx context.Context, id int, upd wtf.DialUpdate) (*wtf.Dial, error)

UpdateDial is not implemented by the HTTP service.

type ErrorResponse

type ErrorResponse struct {
	Error string `json:"error"`
}

ErrorResponse represents a JSON structure for error output.

type Server

type Server struct {

	// Bind address & domain for the server's listener.
	// If domain is specified, server is run on TLS using acme/autocert.
	Addr   string
	Domain string

	// Keys used for secure cookie encryption.
	HashKey  string
	BlockKey string

	// GitHub OAuth settings.
	GitHubClientID     string
	GitHubClientSecret string

	// Servics used by the various HTTP routes.
	AuthService           wtf.AuthService
	DialService           wtf.DialService
	DialMembershipService wtf.DialMembershipService
	EventService          wtf.EventService
	UserService           wtf.UserService
	// contains filtered or unexported fields
}

Server represents an HTTP server. It is meant to wrap all HTTP functionality used by the application so that dependent packages (such as cmd/wtfd) do not need to reference the "net/http" package at all.

func NewServer

func NewServer() *Server

NewServer returns a new instance of Server.

func (*Server) Close

func (s *Server) Close() error

Close gracefully shuts down the server.

func (*Server) MarshalSession

func (s *Server) MarshalSession(session Session) (string, error)

MarshalSession encodes session data to string. This is exported to allow the unit tests to generate fake sessions.

func (*Server) OAuth2Config

func (s *Server) OAuth2Config() *oauth2.Config

OAuth2Config returns the GitHub OAuth2 configuration.

func (*Server) Open

func (s *Server) Open() (err error)

Open validates the server options and begins listening on the bind address.

func (*Server) Port

func (s *Server) Port() int

Port returns the TCP port for the running server. This is useful in tests where we allocate a random port by using ":0".

func (*Server) Scheme

func (s *Server) Scheme() string

Scheme returns the URL scheme for the server.

func (*Server) URL

func (s *Server) URL() string

URL returns the local base URL of the running server.

func (*Server) UnmarshalSession

func (s *Server) UnmarshalSession(data string, session *Session) error

UnmarshalSession decodes session data into a Session object. This is exported to allow the unit tests to generate fake sessions.

func (*Server) UseTLS

func (s *Server) UseTLS() bool

UseTLS returns true if the cert & key file are specified.

type Session

type Session struct {
	UserID      int    `json:"userID"`
	RedirectURL string `json:"redirectURL"`
	State       string `json:"state"`
}

Session represents session data stored in a secure cookie.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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