show

package module
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: BSD-3-Clause Imports: 14 Imported by: 4

README

go-www-show

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

For background, have a look at the Small focused tools for visualizing geographic data blog post.

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/v2"
)

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)
}

For a complete working example see the sfomuseum/go-geojson-show package.

Browsers (targets)

This package defines a Browser interface for opening URLs in or more target environments.

// Browser is an interface for rendering URLs.
type Browser interface {
	// OpenURL opens a given in a URL specific to the browser's implementation context. The method expects
	// a URL to open and a channel that can be used (optionally) to signal when the specific `Browser`
	// implementation exits or otherwise triggers a "completed" state.
	OpenURL(context.Context, string, chan bool) error
}

The following implementations are available when using this package:

WebBrowser (web://)

Open URLs in the operating system's default web browser. The WebBrowser implementation can be instantiated like this:

browser, _ := show.NewBrowser(ctx, "web://")
WebViewBrowser (webview://)

Open URLs in a dedicated application window using the webview/webview_go package. The WebViewBrowser implementation can be instantiated like this:

browser, _ = show.NewBrowser(ctx, "webview://?width=500&height=700")
Parameters
Name Value Required Notes
width int no The initial width of the webview window. Default is 1024.
height int no The initial height of the webview window. Default is 1024.

Applications requiring the WebViewBrowser implementation will need to be built with the webview tag. For example:

$> go build -mod vendor -tags webview -ldflags="-s -w" -o bin/show-url cmd/show-url/main.go

Tools

$> make cli
go build -mod vendor -ldflags="-s -w" -o bin/show-url cmd/show-url/main.go

Note that the default cli Makefile target does not build tools with the webview tag enabled.

show-url

Open a URL in a sfomuseum/go-www-show/v2.Browser environment.

$> ./bin/show-url -h
Open a URL in a sfomuseum/go-www-show/v2.Browser environment.
Usage:
	 ./bin/show-url [options]
Valid options are:
  -browser-uri string
    	A valid sfomuseum/go-www-show/v2.Browser URI. Valid options are: web:// (default "web://")
  -url string
    	The URL to open

See also

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. The method expects
	// a URL to open and a channel that can be used (optionally) to signal when the specific `Browser`
	// implementation exits or otherwise triggers a "completed" state.
	OpenURL(context.Context, string, chan bool) 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
	// The amount of time to wait in between checks to ensure web server is responding to requests.
	EnsureServiceDelay time.Duration
}

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, done_ch chan bool) error

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

Directories

Path Synopsis
cmd
show-url command
show-url is a command-line tool for opening a URL in a sfomuseum/go-www-show/v2.Browser environment.
show-url is a command-line tool for opening a URL in a sfomuseum/go-www-show/v2.Browser environment.

Jump to

Keyboard shortcuts

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