show

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2024 License: BSD-3-Clause Imports: 14 Imported by: 2

README

go-www-show

Go package for starting a local webserver and then opening its URL in a target source once it (the web server) is running.

Usage

This package is meant to be used by other packages that have configured a http.Mux instance for serving web requests. The package will start a web server on localhost listening on a randomly chose port number (unless a user-defined value is provided) and then open that URL in a target environment (like a web browser).

import (
	"context"
	"net/http"

	"github.com/sfomuseum/go-www-show"
)

func main() {

	ctx := context.Background()
	
	mux := http.NewServeMux()
	
	// Configure handlers for mux here

	browser, _ = show.NewBrowser(ctx, "web://")
	
        show_opts := &www_show.RunOptions{
                Browser: browser,
                Mux:     mux,
        }

        return show.RunWithOptions(ctx, www_show_opts)
}

This package defines a Browser interface for opening URLs. Currently there is only a single implementation (web) for opening URLs in the operating system's default web browser. In the future there may be other implementations to "open" a URL by delivering it to a remote service that handles opening and displaying that URL.

For a complete working example see ...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BrowserSchemes

func BrowserSchemes() []string

BrowserSchemes returns the list of schemes that have been registered.

func RegisterBrowser

func RegisterBrowser(ctx context.Context, scheme string, init_func BrowserInitializationFunc) error

RegisterBrowser registers 'scheme' as a key pointing to 'init_func' in an internal lookup table used to create new `Browser` instances by the `NewBrowser` method.

func RunWithOptions

func RunWithOptions(ctx context.Context, opts *RunOptions) error

RunWithOptions start a local webserver and then open its URL in a target source once it (the web server) is running.

Types

type Browser

type Browser interface {
	// OpenURL opens a given in a URL specific to the browser's implementation context.
	OpenURL(context.Context, string) error
}

Browser is an interface for rendering URLs.

func NewBrowser

func NewBrowser(ctx context.Context, uri string) (Browser, error)

NewBrowser returns a new `Browser` instance configured by 'uri'. The value of 'uri' is parsed as a `url.URL` and its scheme is used as the key for a corresponding `BrowserInitializationFunc` function used to instantiate the new `Browser`. It is assumed that the scheme (and initialization function) have been registered by the `RegisterBrowser` method.

func NewWebBrowser

func NewWebBrowser(ctx context.Context, uri string) (Browser, error)

NewWebBrowser retuns a new `WebBrowser` instance.

type BrowserInitializationFunc

type BrowserInitializationFunc func(ctx context.Context, uri string) (Browser, error)

BrowserInitializationFunc is a function defined by individual browser package and used to create an instance of that browser

type RunOptions

type RunOptions struct {
	// The hostname for the web server to listen on. If empty then "localhost" will be assumed.
	Host string
	// The port number for the web server to listen on. If `0` then a random port number will be assigned.
	Port int
	// The `http.ServeMux` that the web server should use to route requests.
	Mux *http.ServeMux
	// The `Browser` instance to use to open the URL pointing to the web server.
	Browser Browser
}

RunOptions defines options for serving and opening a local web server

type WebBrowser

type WebBrowser struct {
	Browser
}

WebBrowser implements the `Browser` interface for loading URLs in a web browser.

func (*WebBrowser) OpenURL

func (br *WebBrowser) OpenURL(ctx context.Context, url string) error

OpenURL opens 'url' in the operating system's default web browser.

Jump to

Keyboard shortcuts

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