Documentation ¶
Index ¶
- Variables
- func NewLogger(level, encoding string) (*zap.Logger, error)
- type Addon
- func (a *Addon) AddEndpoint(method, path string, handler fiber.Handler)
- func (a *Addon) AddMiddleware(path string, middleware fiber.Handler)
- func (a *Addon) DecodeUserData(param string, c *fiber.Ctx) (interface{}, error)
- func (a *Addon) RegisterUserData(userDataObject interface{})
- func (a *Addon) Run(stoppingChan chan bool)
- func (a *Addon) SetManifestCallback(callback ManifestCallback)
- type BehaviorHints
- type CatalogHandler
- type CatalogItem
- type ExtraItem
- type Manifest
- type ManifestCallback
- type MetaFetcher
- type MetaItem
- type MetaLinkItem
- type MetaPreviewItem
- type Options
- type ResourceItem
- type StreamHandler
- type StreamItem
- type VideoItem
Constants ¶
This section is empty.
Variables ¶
var ( // BadRequest signals that the client sent a bad request. // It leads to a "400 Bad Request" response. BadRequest = errors.New("Bad request") // NotFound signals that the catalog/meta/stream was not found. // It leads to a "404 Not Found" response. NotFound = errors.New("Not found") )
var DefaultOptions = Options{ BindAddr: "localhost", Port: 8080, LoggingLevel: "info", LogEncoding: "console", CinemetaTimeout: 2 * time.Second, }
DefaultOptions is an Options object with default values. For fields that aren't set here the zero value is the default value.
Functions ¶
func NewLogger ¶ added in v0.3.0
NewLogger creates a new logger with sane defaults and the passed level. Supported levels are: debug, info, warn, error. Only logs with that level and above are then logged (e.g. with "info" no debug logs will be logged). The encoding parameter is optional and will only be used when non-zero. Valid values: "console" (default) and "json".
It makes sense to get this logger as early as possible and use it in your ManifestCallback, CatalogHandler and StreamHandler, so that all logs behave and are formatted the same way. You should then also set this logger in the options for `NewAddon()`, so that not two loggers are created. Alternatively you can create your own custom *zap.Logger and set it in the options when creating a new addon, leading to the addon using that custom logger.
Types ¶
type Addon ¶
type Addon struct {
// contains filtered or unexported fields
}
Addon represents a remote addon. You can create one with NewAddon() and then run it with Run().
func NewAddon ¶
func NewAddon(manifest Manifest, catalogHandlers map[string]CatalogHandler, streamHandlers map[string]StreamHandler, opts Options) (*Addon, error)
NewAddon creates a new Addon object that can be started with Run(). A proper manifest must be supplied, but manifestCallback and all but one handler can be nil in case you only want to handle specific requests and opts can be the zero value of Options.
func (*Addon) AddEndpoint ¶ added in v0.3.0
AddEndpoint adds a custom endpoint (a route and its handler). If you want to be able to access custom user data, you can use a path like this: "/:userData/foo" and then either deal with the data yourself by using `c.Params("userData", "")` in the handler, or use the convenience method `DecodeUserData("userData", c)`.
func (*Addon) AddMiddleware ¶ added in v0.3.0
AddMiddleware appends a custom middleware to the chain of existing middlewares. Set path to an empty string or "/" to let the middleware apply to all routes. Don't forget to call c.Next() on the Fiber context!
func (*Addon) DecodeUserData ¶ added in v0.3.0
DecodeUserData decodes the request's user data and returns the result. It's useful when you add custom endpoints to the addon that don't have a userData parameter like the ManifestCallback, CatalogHandler and StreamHandler have. The param value must match the URL parameter you used when creating the custom endpoint, for example when using `AddEndpoint("GET", "/:userData/ping", customEndpoint)` you must pass "userData".
func (*Addon) RegisterUserData ¶ added in v0.3.0
func (a *Addon) RegisterUserData(userDataObject interface{})
RegisterUserData registers the type of userData, so the addon can automatically unmarshal user data into an object of this type and pass the object into the manifest callback or catalog and stream handlers.
func (*Addon) Run ¶
Run starts the remote addon. It sets up an HTTP server that handles requests to "/manifest.json" etc. and gracefully handles shutdowns. The call is *blocking*, so use the stoppingChan param if you want to be notified when the addon is about to shut down because of a system signal like Ctrl+C or `docker stop`. It should be a buffered channel with a capacity of 1.
func (*Addon) SetManifestCallback ¶ added in v0.3.0
func (a *Addon) SetManifestCallback(callback ManifestCallback)
SetManifestCallback sets the manifest callback
type BehaviorHints ¶
type BehaviorHints struct { // Note: Must include `omitempty`, otherwise it will be included if this struct is used in another one, even if the field of the containing struct is marked as `omitempty` Adult bool `json:"adult,omitempty"` P2P bool `json:"p2p,omitempty"` Configurable bool `json:"configurable,omitempty"` // If you set this to true, it will be true for the "/manifest.json" endpoint, but false for the "/:userData/manifest.json" endpoint, because otherwise Stremio won't show the "Install" button in its UI. ConfigurationRequired bool `json:"configurationRequired,omitempty"` }
type CatalogHandler ¶
type CatalogHandler func(ctx context.Context, id string, userData interface{}) ([]MetaPreviewItem, error)
CatalogHandler is the callback for catalog requests for a specific type (like "movie"). The id parameter is the catalog ID that you specified yourself in the CatalogItem objects in the Manifest. The userData parameter depends on whether you called `RegisterUserData()` before: If not, a simple string will be passed. It's empty if the user didn't provide user data. If yes, a pointer to an object you registered will be passed. It's nil if the user didn't provide user data.
type CatalogItem ¶
type CatalogItem struct { Type string `json:"type"` ID string `json:"id"` Name string `json:"name"` // Optional Extra []ExtraItem `json:"extra,omitempty"` }
CatalogItem represents a catalog.
type Manifest ¶
type Manifest struct { ID string `json:"id"` Name string `json:"name"` Description string `json:"description"` Version string `json:"version"` // One of the following is required // Note: Can only have one in code because of how Go (de-)serialization works //Resources []string `json:"resources,omitempty"` ResourceItems []ResourceItem `json:"resources,omitempty"` Types []string `json:"types"` // Stremio supports "movie", "series", "channel" and "tv" Catalogs []CatalogItem `json:"catalogs"` // Optional IDprefixes []string `json:"idPrefixes,omitempty"` Background string `json:"background,omitempty"` // URL Logo string `json:"logo,omitempty"` // URL ContactEmail string `json:"contactEmail,omitempty"` BehaviorHints BehaviorHints `json:"behaviorHints,omitempty"` }
Manifest describes the capabilities of the addon. See https://github.com/Stremio/stremio-addon-sdk/blob/f6f1f2a8b627b9d4f2c62b003b251d98adadbebe/docs/api/responses/manifest.md
type ManifestCallback ¶ added in v0.3.0
ManifestCallback is the callback for manifest requests, so mostly addon installations. You can use the callback for two things:
- To *prevent* users from installing your addon in Stremio. The userData parameter depends on whether you called `RegisterUserData()` before: If not, a simple string will be passed. It's empty if the user didn't provide user data. If yes, a pointer to an object you registered will be passed. It's nil if the user didn't provide user data. Return an HTTP status code >= 400 to stop further processing and let the addon return that exact status code. Any status code < 400 will lead to the manifest being returned with a 200 OK status code in the response.
- To *alter* the manifest before it's returned. This can be useful for example if you want to return some catalogs depending on the userData. Note that the manifest is only returned if the first return value is < 400 (see point 1.).
type MetaFetcher ¶ added in v0.8.0
type MetaFetcher interface { GetMovie(ctx context.Context, imdbID string) (cinemeta.Meta, error) GetTVShow(ctx context.Context, imdbID string, season int, episode int) (cinemeta.Meta, error) }
MetaFetcher returns metadata for movies and TV shows. It's used when you configure that the media name should be logged or that metadata should be put into the context.
type MetaItem ¶
type MetaItem struct { ID string `json:"id"` Type string `json:"type"` Name string `json:"name"` // Optional Genres []string `json:"genres,omitempty"` // Will be replaced by Links at some point Director []string `json:"director,omitempty"` // Will be replaced by Links at some point Cast []string `json:"cast,omitempty"` // Will be replaced by Links at some point Links []MetaLinkItem `json:"links,omitempty"` // For genres, director, cast and potentially more. Not fully supported by Stremio yet! Poster string `json:"poster,omitempty"` // URL PosterShape string `json:"posterShape,omitempty"` Background string `json:"background,omitempty"` // URL Logo string `json:"logo,omitempty"` // URL Description string `json:"description,omitempty"` ReleaseInfo string `json:"releaseInfo,omitempty"` // E.g. "2000" for movies and "2000-2014" or "2000-" for TV shows IMDbRating string `json:"imdbRating,omitempty"` Released string `json:"released,omitempty"` // Must be ISO 8601, e.g. "2010-12-06T05:00:00.000Z" Videos []VideoItem `json:"videos,omitempty"` Runtime string `json:"runtime,omitempty"` Language string `json:"language,omitempty"` Country string `json:"country,omitempty"` Awards string `json:"awards,omitempty"` Website string `json:"website,omitempty"` // URL }
MetaItem represents a meta item and is meant to be used when info for a specific item was requested. See https://github.com/Stremio/stremio-addon-sdk/blob/f6f1f2a8b627b9d4f2c62b003b251d98adadbebe/docs/api/responses/meta.md
type MetaLinkItem ¶
type MetaLinkItem struct { Name string `json:"name"` Category string `json:"category"` URL string `json:"url"` // // URL. Can be "Meta Links" (see https://github.com/Stremio/stremio-addon-sdk/blob/f6f1f2a8b627b9d4f2c62b003b251d98adadbebe/docs/api/responses/meta.links.md) }
MetaLinkItem links to a page within Stremio. It will at some point replace the usage of `genres`, `director` and `cast`. Note: It's not fully supported by Stremio yet (not fully on PC and not at all on Android)!
type MetaPreviewItem ¶
type MetaPreviewItem struct { ID string `json:"id"` Type string `json:"type"` Name string `json:"name"` Poster string `json:"poster"` // URL // Optional PosterShape string `json:"posterShape,omitempty"` // Optional, used for the "Discover" page sidebar Genres []string `json:"genres,omitempty"` // Will be replaced by Links at some point Director []string `json:"director,omitempty"` // Will be replaced by Links at some point Cast []string `json:"cast,omitempty"` // Will be replaced by Links at some point Links []MetaLinkItem `json:"links,omitempty"` // For genres, director, cast and potentially more. Not fully supported by Stremio yet! IMDbRating string `json:"imdbRating,omitempty"` ReleaseInfo string `json:"releaseInfo,omitempty"` // E.g. "2000" for movies and "2000-2014" or "2000-" for TV shows Description string `json:"description,omitempty"` }
MetaPreviewItem represents a meta preview item and is meant to be used within catalog responses. See https://github.com/Stremio/stremio-addon-sdk/blob/f6f1f2a8b627b9d4f2c62b003b251d98adadbebe/docs/api/responses/meta.md#meta-preview-object
type Options ¶
type Options struct { // The interface to bind to. // "0.0.0.0" to bind to all interfaces. "localhost" to *exclude* requests from other machines. // Default "localhost". BindAddr string // The port to listen on. // Default 8080. Port int // You can set a custom logger, or leave this empty to create a new one // with sane defaults and the LoggingLevel in these options. // If you already called `NewLogger()`, you should set that logger here. // Default nil. Logger *zap.Logger // The logging level. // Only logs with the same or a higher log level will be shown. // For example when you set it to "info", info, warn and error logs will be shown, but no debug logs. // Accepts "debug", "info", "warn" and "error". // Only required when not already setting the Logger in the options. // Default "info". LoggingLevel string // Configures zap's log encoding. // "console" will format a log line console-friendly. // "json" is better suited when using a centralized log solution like ELK, Graylog or Loki. // Default "console". LogEncoding string // Flag for indicating whether requests should be logged. // Default false (meaning requests will be logged by default). DisableRequestLogging bool // Flag for indicating whether IP addresses should be logged. // Default false. LogIPs bool // Flag for indicating whether the user agent header should be logged. // Default false. LogUserAgent bool // URL to redirect to when someone requests the root of the handler instead of the manifest, catalog, stream etc. // When no value is set, it will lead to a "404 Not Found" response. // Default "". RedirectURL string // Flag for indicating whether you want to expose URL handlers for the Go profiler. // The URLs are be the standard ones: "/debug/pprof/...". // Default false. Profiling bool // Flag for indicating whether you want to collect and expose Prometheus metrics. // The URL is the standard one: "/metrics". // There's no credentials required for accessing it. If you expose deflix-stremio to the public, // you might want to protect the metrics route in your reverse proxy. // Default false. Metrics bool // Duration of client/proxy-side cache for responses from the catalog endpoint. // Helps reducing number of requsts and transferred data volume to/from the server. // The result is not cached by the SDK on the server side, so if two *separate* users make a reqeust, // and no proxy cached the response, your CatalogHandler will be called twice. // Default 0. CacheAgeCatalogs time.Duration // Same as CacheAgeCatalogs, but for streams. CacheAgeStreams time.Duration // Flag for indicating to proxies whether they are allowed to cache responses from the catalog endpoint. // Default false. CachePublicCatalogs bool // Same as CachePublicCatalogs, but for streams. CachePublicStreams bool // Flag for indicating whether the "ETag" header should be set and the "If-None-Match" header checked. // Helps reducing the transferred data volume from the server even further. // Only makes sense when setting a non-zero CacheAgeCatalogs. // Leads to a slight computational overhead due to every CatalogHandler result being hashed. // Default false. HandleEtagCatalogs bool // Same as HandleEtagCatalogs, but for streams. HandleEtagStreams bool // Flag for indicating whether user data is Base64-encoded. // As the user data is in the URL it needs to be the URL-safe Base64 encoding described in RFC 4648. // When true, go-stremio first decodes the value before passing or unmarshalling it. // Default false. UserDataIsBase64 bool // Flag for indicating whether to look up the movie / TV show name by its IMDb ID and put it into the context. // Only works for stream requests. // Default false. PutMetaInContext bool // Flag for indicating whether to include the movie / TV show name (and year) in the request log. // Only works for stream requests. // Default false. LogMediaName bool // Meta client for fetching movie and TV show info. // Only relevant when using PutMetaInContext or LogMediaName. // You can set it if you have already created one to share its in-memory cache for example, // or leave it empty to let go-stremio create a client that fetches metadata from Stremio's Cinemeta remote addon. MetaClient MetaFetcher // Timeout for requests to Cinemeta. // Only relevant when using PutMetaInContext or LogMediaName. // Only required when not setting a MetaClient in the options already. // Note that each response is cached for 30 days, so waiting a bit once per movie / TV show per 30 days is acceptable. // Default 2 seconds. CinemetaTimeout time.Duration // "File system" with HTML files that will be served for the "/configure" endpoint. // Typically an `http.Dir`, which you can simply create with `http.Dir("/path/to/html/files")`. // No configure endpoint will be created if this is nil, so you can add a custom one. // Default nil. ConfigureHTMLfs http.FileSystem // Regex for accepted stream IDs. // Even when setting the "tt" prefix in the manifest to only allow IMDb IDs, some clients still send stream requests for completely different IDs, // potentially leading to your handlers being triggered and executing some logic before than failing due to the bad ID. // With this regex you can make sure your handlers are only called for valid IDs. An empty value will lead to your handlers being called for any ID. // IMDb example: "tt\\d{7,8}" or `tt\d{7,8}` // Default "". StreamIDregex string }
Options are the options that can be used to configure the addon.
type ResourceItem ¶
type StreamHandler ¶
type StreamHandler func(ctx context.Context, id string, userData interface{}) ([]StreamItem, error)
StreamHandler is the callback for stream requests for a specific type (like "movie"). The context parameter contains a meta object under the key "meta" if PutMetaInContext was set to true in the addon options. The id parameter can be for example an IMDb ID if your addon handles the "movie" type. The userData parameter depends on whether you called `RegisterUserData()` before: If not, a simple string will be passed. It's empty if the user didn't provide user data. If yes, a pointer to an object you registered will be passed. It's nil if the user didn't provide user data.
type StreamItem ¶
type StreamItem struct { // One of the following is required URL string `json:"url,omitempty"` // URL YoutubeID string `json:"ytId,omitempty"` InfoHash string `json:"infoHash,omitempty"` ExternalURL string `json:"externalUrl,omitempty"` // URL // Optional Title string `json:"title,omitempty"` // Usually used for stream quality FileIndex uint8 `json:"fileIdx,omitempty"` // Only when using InfoHash }
StreamItem represents a stream for a MetaItem. See https://github.com/Stremio/stremio-addon-sdk/blob/f6f1f2a8b627b9d4f2c62b003b251d98adadbebe/docs/api/responses/stream.md
type VideoItem ¶
type VideoItem struct { ID string `json:"id"` Title string `json:"title"` Released string `json:"released"` // Must be ISO 8601, e.g. "2010-12-06T05:00:00.000Z" // Optional Thumbnail string `json:"thumbnail,omitempty"` // URL Streams []StreamItem `json:"streams,omitempty"` Available bool `json:"available,omitempty"` Episode string `json:"episode,omitempty"` Season string `json:"season,omitempty"` Trailer string `json:"trailer,omitempty"` // Youtube ID Overview string `json:"overview,omitempty"` }