Documentation ¶
Overview ¶
Package webserver contains the webserver which deals with processing requests from the user, presenting him with the interface of the application.
Index ¶
- Constants
- Variables
- func InternalErrorOnErrorHandler(writer http.ResponseWriter, req *http.Request, fnc HandlerFuncWithError)
- func NewCreateQRTokenHandler(needsAuth bool, auth config.Auth) http.Handler
- func NewGzipHandler(handler http.Handler, exceptions []string) http.Handler
- func NewLoginHandler(auth config.Auth) http.Handler
- func NewLoginTokenHandler(auth config.Auth) http.Handler
- func NewLogoutHandler() http.Handler
- func NewRigisterTokenHandler() http.Handler
- func NewTemplateHandler(tpl *template.Template, title string) http.Handler
- func NewTerryHandler(handler http.Handler) http.Handler
- func WithInternalError(fnc HandlerFuncWithError) http.HandlerFunc
- type AlbumArtworkHandler
- type AlbumHandler
- type AllTemplates
- type ArtstImageHandler
- type AuthHandler
- type BrowseHandler
- type FSTemplates
- type FileHandler
- type GzipHandler
- type HandlerFuncWithError
- type SearchHandler
- type Server
- type Templates
- type TerryHandler
Constants ¶
const ( APIv1EndpointFile = "/v1/file/{fileID}" APIv1EndpointAlbumArtwork = "/v1/album/{albumID}/artwork" APIv1EndpointDownloadAlbum = "/v1/album/{albumID}" APIv1EndpointArtistImage = "/v1/artist/{artistID}/image" APIv1EndpointBrowse = "/v1/browse" APIv1EndpointSearchWithPath = "/v1/search/{searchQuery}" APIv1EndpointSearch = "/v1/search/" APIv1EndpointLoginToken = "/v1/login/token/" APIv1EndpointRegisterToken = "/v1/register/token/" )
The following are URL Path endpoints for certain API calls.
Variables ¶
var APIv1Methods map[string][]string = map[string][]string{ APIv1EndpointFile: {http.MethodGet}, APIv1EndpointAlbumArtwork: {http.MethodGet, http.MethodPut, http.MethodDelete}, APIv1EndpointDownloadAlbum: {http.MethodGet}, APIv1EndpointArtistImage: {http.MethodGet, http.MethodPut, http.MethodDelete}, APIv1EndpointBrowse: {http.MethodGet}, APIv1EndpointSearchWithPath: {http.MethodGet}, APIv1EndpointSearch: {http.MethodGet}, APIv1EndpointLoginToken: {http.MethodPost}, APIv1EndpointRegisterToken: {http.MethodPost}, }
APIv1Methods defines on which HTTP methods APIv1 endpoints will respond to. It is an uri_path => list of HTTP methods map.
Functions ¶
func InternalErrorOnErrorHandler ¶
func InternalErrorOnErrorHandler(writer http.ResponseWriter, req *http.Request, fnc HandlerFuncWithError)
InternalErrorOnErrorHandler is used to wrap around handlers-like functions which just return error. This function actually writes the HTTP error and renders the error in the html.
func NewCreateQRTokenHandler ¶ added in v1.2.1
NewCreateQRTokenHandler returns a http.Handler which will generate an access token in a QR bar code and serve it as a png image as a response. In the bar code the server address from the query value "address" is included.
func NewGzipHandler ¶
NewGzipHandler returns GzipHandler which will gzip anything written in the supplied handler. Must be the main handler given to the net.Server
func NewLoginHandler ¶ added in v1.2.0
NewLoginHandler returns a new login handler which will use the information in auth for deciding when user has logged in correctly and also for generating tokens.
func NewLoginTokenHandler ¶ added in v1.3.0
NewLoginTokenHandler returns a new login handler which will use the information in auth for deciding when device or program was logged in correctly by entering username and password.
func NewLogoutHandler ¶ added in v1.2.0
NewLogoutHandler returns a handler which will logout the user form his HTTP session by unsetting his session cookie.
func NewRigisterTokenHandler ¶ added in v1.3.0
NewRigisterTokenHandler returns a handler resposible for checking and eventually registering registering in the database token generated to a device. !TODO: actually store the device token in the database once it has a unique ID
func NewTemplateHandler ¶ added in v1.2.1
NewTemplateHandler returns a handler which will execute the page template inside the layout template.
func NewTerryHandler ¶ added in v1.0.1
NewTerryHandler returns a new TerryHandler, ready for use.
func WithInternalError ¶ added in v1.2.0
func WithInternalError(fnc HandlerFuncWithError) http.HandlerFunc
WithInternalError converts HandlerFuncWithError to http.HandlerFunc by making sure all errors returned are flushed to the writer and Internal Server Error HTTP status is sent.
Types ¶
type AlbumArtworkHandler ¶ added in v1.2.0
type AlbumArtworkHandler struct {
// contains filtered or unexported fields
}
AlbumArtworkHandler is a http.Handler which will find and serve the artwork of a particular album.
func NewAlbumArtworkHandler ¶ added in v1.2.0
func NewAlbumArtworkHandler( am library.ArtworkManager, httpRootFS fs.FS, notFoundImagePath string, ) *AlbumArtworkHandler
NewAlbumArtworkHandler returns a new Album artwork handler. It needs an implementation of the ArtworkManager.
func (AlbumArtworkHandler) ServeHTTP ¶ added in v1.2.0
func (aah AlbumArtworkHandler) ServeHTTP(writer http.ResponseWriter, req *http.Request)
ServeHTTP is required by the http.Handler's interface
type AlbumHandler ¶
type AlbumHandler struct {
// contains filtered or unexported fields
}
AlbumHandler is a http.Handler which will find and serve a zip of the album by the album ID.
func NewAlbumHandler ¶
func NewAlbumHandler(lib library.Library) *AlbumHandler
NewAlbumHandler returns a new Album handler. It needs a library to search in
func (AlbumHandler) ServeHTTP ¶
func (fh AlbumHandler) ServeHTTP(writer http.ResponseWriter, req *http.Request)
ServeHTTP is required by the http.Handler's interface
type AllTemplates ¶ added in v1.2.1
type AllTemplates struct {
// contains filtered or unexported fields
}
AllTemplates is a structure which contains all parsed templates for different pages. They are ready for usage in http handlers which return HTML.
type ArtstImageHandler ¶ added in v1.5.0
type ArtstImageHandler struct {
// contains filtered or unexported fields
}
ArtstImageHandler is a http.Handler which provides CRUD operations for artist images.
func NewArtistImagesHandler ¶ added in v1.5.0
func NewArtistImagesHandler( am library.ArtistImageManager, ) *ArtstImageHandler
NewArtistImagesHandler returns a new Artist image handler. It needs an implementation of the ArtistImageManager.
func (ArtstImageHandler) ServeHTTP ¶ added in v1.5.0
func (aih ArtstImageHandler) ServeHTTP(writer http.ResponseWriter, req *http.Request)
ServeHTTP is required by the http.Handler's interface
type AuthHandler ¶ added in v1.2.0
type AuthHandler struct {
// contains filtered or unexported fields
}
AuthHandler is a handler wrapper used for authentication. Its only job is to do the authentication and then pass the work to the Handler it wraps around. Possible methods for authentication:
- Basic Auth with the username and password
- Authorization Bearer JWT token
- JWT token in a session cookie
- JWT token as a query string
Basic auth is preserved for backward compatibility. Needless to say, it so not a preferred method for authentication.
func NewAuthHandler ¶ added in v1.5.1
func NewAuthHandler( wrapped http.Handler, username string, password string, templatesResolver Templates, secret string, exceptions []string, ) *AuthHandler
NewAuthHandler returns a new AuthHandler.
func (*AuthHandler) ServeHTTP ¶ added in v1.2.0
func (hl *AuthHandler) ServeHTTP(writer http.ResponseWriter, req *http.Request)
ServeHTTP implements the http.Handler interface and does the actual basic authenticate check for every request
type BrowseHandler ¶ added in v1.1.0
type BrowseHandler struct {
// contains filtered or unexported fields
}
BrowseHandler is a http.Handler which will allow you to browse through artists or albums with the help of pagination.
func NewBrowseHandler ¶ added in v1.1.0
func NewBrowseHandler(browser library.Browser) *BrowseHandler
NewBrowseHandler returns a new Browse handler. It needs a library.Browser to browse through.
func (BrowseHandler) ServeHTTP ¶ added in v1.1.0
func (bh BrowseHandler) ServeHTTP(writer http.ResponseWriter, req *http.Request)
ServeHTTP is required by the http.Handler's interface
type FSTemplates ¶ added in v1.5.0
type FSTemplates struct {
// contains filtered or unexported fields
}
FSTemplates is Templates implementation which uses fs.FS to extract template data.
func NewFSTemplates ¶ added in v1.5.0
func NewFSTemplates(fs fs.FS) *FSTemplates
NewFSTemplates returns a new FSTemplates which will use the argument fs.FS for finding and reading files.
func (*FSTemplates) All ¶ added in v1.5.0
func (t *FSTemplates) All() (*AllTemplates, error)
All implements the Templates interface.
type FileHandler ¶
type FileHandler struct {
// contains filtered or unexported fields
}
FileHandler will find and serve a media file by its ID
func NewFileHandler ¶
func NewFileHandler(lib library.Library) *FileHandler
NewFileHandler returns a new File handler will will be resposible for serving a file from the library identified from its ID.
func (FileHandler) ServeHTTP ¶
func (fh FileHandler) ServeHTTP(writer http.ResponseWriter, req *http.Request)
ServeHTTP is required by the http.Handler's interface
type GzipHandler ¶
type GzipHandler struct {
// contains filtered or unexported fields
}
GzipHandler gzips our output using a custom Writer. It will check if gzip is among the accepted encodings and gzip if so. Otherwise it will do nothing.
func (GzipHandler) ServeHTTP ¶
func (gzh GzipHandler) ServeHTTP(writer http.ResponseWriter, req *http.Request)
ServeHTTP satisfies the http.Handler interface
type HandlerFuncWithError ¶ added in v1.2.0
type HandlerFuncWithError func(http.ResponseWriter, *http.Request) error
HandlerFuncWithError is similar to http.HandlerFunc but returns an error when the handling of the request failed.
type SearchHandler ¶
type SearchHandler struct {
// contains filtered or unexported fields
}
SearchHandler is a http.Handler responsible for search requests. It will use the Library to return a list of matched files to the interface.
func NewSearchHandler ¶
func NewSearchHandler(lib library.Library) *SearchHandler
NewSearchHandler returns a new SearchHandler for processing search queries. They will be run against the supplied library
func (SearchHandler) ServeHTTP ¶
func (sh SearchHandler) ServeHTTP(writer http.ResponseWriter, req *http.Request)
ServeHTTP is required by the http.Handler's interface
type Server ¶
type Server struct { // Makes the server lockable. This lock should be used for accessing the // listener sync.Mutex // contains filtered or unexported fields }
Server represents our web server. It will be controlled from here
func NewServer ¶
func NewServer( ctx context.Context, cfg config.Config, lib *library.LocalLibrary, httpRootFS fs.FS, htmlTemplatesFS fs.FS, ) *Server
NewServer Returns a new Server using the supplied configuration cfg. The returned server is ready and calling its Serve method will start it.
type Templates ¶ added in v1.2.0
type Templates interface { // Get find and parses a template based on its file name. Get(path string) (*template.Template, error) // All returns a struct which contains all templates in // non-exported attributes. All() (*AllTemplates, error) }
Templates is a type which knows how to find and parse HTML templates by their name.
type TerryHandler ¶ added in v1.0.1
type TerryHandler struct {
// contains filtered or unexported fields
}
TerryHandler adds the X-Clacks-Overhead header. It wraps around the actual handler.
func (TerryHandler) ServeHTTP ¶ added in v1.0.1
func (th TerryHandler) ServeHTTP(writer http.ResponseWriter, req *http.Request)
ServeHTTP satisfies the http.Handler interface.
Source Files ¶
- apiv1_endpoints.go
- functions.go
- handler_album.go
- handler_album_artwork.go
- handler_artist_images.go
- handler_auth.go
- handler_browse.go
- handler_create_qr_token.go
- handler_file.go
- handler_gzip.go
- handler_login.go
- handler_login_token.go
- handler_logout.go
- handler_register_token.go
- handler_search.go
- handler_template.go
- handler_terry.go
- templates.go
- webserver.go