gnoweb

package
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2025 License: UNKNOWN not legal advice Imports: 0 Imported by: 0

README

gnoweb

gnoweb is a universal web frontend for the gno.land blockchain.

This README provides instructions on how to set up and run gnoweb for development purposes.

Prerequisites

Before you begin, ensure you have the following software installed on your machine:

  • Node.js: Required for running JavaScript and CSS build tools.
  • Go: Required for building gnoweb

Development

To start the development environment, which runs multiple development tools in parallel, use the following command:

make dev

This will:

  • Start a Go server in development mode and watch for any Go files change (targeting localhost).
  • Enable Tailwind CSS in watch mode to automatically compile CSS changes.
  • Use esbuild in watch mode to automatically transpile and bundle TypeScript changes.

You can customize the behavior of the Go server using the DEV_REMOTE and CHAIN_ID environment variables. For example, to use portal-loop as the target, run:

CHAIN_ID=portal-loop DEV_REMOTE=https://rpc.gno.land make dev

Generate

To generate the public assets for the project, including static assets (fonts, CSS and JavaScript... files), run the following command. This should be used while editing CSS, JS, or any asset files:

make generate

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrClientPathNotFound = errors.New("package not found")
	ErrRenderNotDeclared  = errors.New("render function not declared")
	ErrClientBadRequest   = errors.New("bad request")
	ErrClientResponse     = errors.New("node response error")
)
View Source
var DefaultAliases = map[string]AliasTarget{
	"/":           {"/r/gnoland/home", GnowebPath},
	"/about":      {"/r/gnoland/pages:p/about", GnowebPath},
	"/gnolang":    {"/r/gnoland/pages:p/gnolang", GnowebPath},
	"/ecosystem":  {"/r/gnoland/pages:p/ecosystem", GnowebPath},
	"/start":      {"/r/gnoland/pages:p/start", GnowebPath},
	"/license":    {"/r/gnoland/pages:p/license", GnowebPath},
	"/contribute": {"/r/gnoland/pages:p/contribute", GnowebPath},
	"/events":     {"/r/gnoland/events", GnowebPath},
}
View Source
var Redirects = map[string]string{
	"/r/demo/boards:gnolang/6": "/r/demo/boards:gnolang/3",
	"/blog":                    "/r/gnoland/blog",
	"/gor":                     "/contribute",
	"/game-of-realms":          "/contribute",
	"/grants":                  "/partners",
	"/language":                "/gnolang",
	"/getting-started":         "/start",
}

redirects are gnoweb paths that are redirected using [AliasAndRedirectMiddleware].

Functions

func AssetHandler

func AssetHandler() http.Handler

AssetHandler returns the handler to serve static assets. If cache is true, these will be served using the static files embedded in the binary; otherwise they will served from the filesystem.

func DevAssetHandler

func DevAssetHandler(path, dir string) http.Handler

func GetClientErrorStatusPage

func GetClientErrorStatusPage(_ *weburl.GnoURL, err error) (int, *components.View)

func IsHomePath

func IsHomePath(path string) bool

IsHomePath checks if the given path is the home path.

func NewRouter

func NewRouter(logger *slog.Logger, cfg *AppConfig) (http.Handler, error)

NewRouter initializes the gnoweb router with the specified logger and configuration.

func RedirectMiddleware

func RedirectMiddleware(next http.Handler, analytics bool) http.Handler

RedirectMiddleware redirects all incoming requests whose path matches any of the Redirects to the corresponding URL.

Types

type AliasKind

type AliasKind int
const (
	GnowebPath AliasKind = iota
	StaticMarkdown
)

type AliasTarget

type AliasTarget struct {
	Value string
	Kind  AliasKind
}

type AppConfig

type AppConfig struct {
	// UnsafeHTML, if enabled, allows to use HTML in the markdown.
	UnsafeHTML bool
	// Analytics enables SimpleAnalytics.
	Analytics bool
	// NodeRemote is the remote address of the gno.land node.
	NodeRemote string
	// RemoteHelp is the remote of the gno.land node, as used in the help page.
	RemoteHelp string
	// ChainID is the chain id, used for constructing the help page.
	ChainID string
	// AssetsPath is the base path to the gnoweb assets.
	AssetsPath string
	// AssetDir, if set, will be used for assets instead of the embedded public directory.
	AssetsDir string
	// FaucetURL, if specified, will be the URL to which `/faucet` redirects.
	FaucetURL string
	// Domain is the domain used by the node.
	Domain string
	// Aliases is a map of aliases pointing to another path or a static file.
	Aliases map[string]AliasTarget
}

AppConfig contains configuration for the gnoweb.

func NewDefaultAppConfig

func NewDefaultAppConfig() *AppConfig

NewDefaultAppConfig returns a new default AppConfig. The default sets 127.0.0.1:26657 as the remote node, "dev" as the chain ID and sets up Assets to be served on /public/.

type ContentRenderer

type ContentRenderer interface {
	// Render renders the content of a source file and write it on the given writer.
	// It returns a Table of Contents (Toc) and an error if any occurs.
	Render(w io.Writer, u *weburl.GnoURL, src []byte) (md.Toc, error)
}

Renderer is an interface for rendering content from source.

type FileMeta

type FileMeta struct {
	Lines  int
	SizeKb float64
}

type HTMLWebClient

type HTMLWebClient struct {
	Formatter *chromahtml.Formatter
	// contains filtered or unexported fields
}

func NewHTMLClient

func NewHTMLClient(log *slog.Logger, cfg *HTMLWebClientConfig) *HTMLWebClient

NewHTMLClient creates a new instance of WebClient. It requires a configured logger and WebClientConfig.

func (*HTMLWebClient) Doc

func (s *HTMLWebClient) Doc(pkgPath string) (*doc.JSONDocumentation, error)

Doc retrieves the JSON doc suitable for printing from a specified package path.

func (*HTMLWebClient) FormatSource

func (s *HTMLWebClient) FormatSource(w io.Writer, fileName string, src []byte) error

func (*HTMLWebClient) QueryPaths

func (s *HTMLWebClient) QueryPaths(prefix string, limit int) ([]string, error)

Sources lists all source files available in a specified package path by querying the RPC client.

func (*HTMLWebClient) RenderRealm

func (s *HTMLWebClient) RenderRealm(w io.Writer, u *weburl.GnoURL, cr ContentRenderer) (*RealmMeta, error)

RenderRealm renders the content of a realm from a given path and arguments into the provided writer. It uses Goldmark for Markdown processing to generate HTML content.

func (*HTMLWebClient) SourceFile

func (s *HTMLWebClient) SourceFile(w io.Writer, path, fileName string, isRaw bool) (*FileMeta, error)

SourceFile fetches and writes the source file from a given package path and file name to the provided writer. It uses Chroma for syntax highlighting or Raw style source.

func (*HTMLWebClient) Sources

func (s *HTMLWebClient) Sources(path string) ([]string, error)

Sources lists all source files available in a specified package path by querying the RPC client.

func (*HTMLWebClient) WriteFormatterCSS

func (s *HTMLWebClient) WriteFormatterCSS(w io.Writer) error

type HTMLWebClientConfig

type HTMLWebClientConfig struct {
	Domain            string
	RPCClient         *client.RPCClient
	ChromaStyle       *chroma.Style
	ChromaHTMLOptions []chromahtml.Option
}

func NewDefaultHTMLWebClientConfig

func NewDefaultHTMLWebClientConfig(client *client.RPCClient) *HTMLWebClientConfig

NewDefaultHTMLWebClientConfig initializes a WebClientConfig with default settings.

type MarkdownRenderer

type MarkdownRenderer struct {
	// contains filtered or unexported fields
}

func NewMarkdownRenderer

func NewMarkdownRenderer(logger *slog.Logger, cfg *MarkdownRendererConfig) *MarkdownRenderer

func (*MarkdownRenderer) Render

func (mr *MarkdownRenderer) Render(w io.Writer, u *weburl.GnoURL, src []byte) (md.Toc, error)

type MarkdownRendererConfig

type MarkdownRendererConfig struct {
	GoldmarkOptions []goldmark.Option
}

func NewDefaultMarkdownRendererConfig

func NewDefaultMarkdownRendererConfig(chromaOptions []chromahtml.Option) *MarkdownRendererConfig

type MockPackage

type MockPackage struct {
	Path      string
	Domain    string
	Files     map[string]string // filename -> body
	Functions []*doc.JSONFunc
}

MockPackage represents a mock package with files and function signatures.

type MockWebClient

type MockWebClient struct {
	Packages map[string]*MockPackage // path -> package
}

MockWebClient is a mock implementation of the Client interface.

func NewMockWebClient

func NewMockWebClient(pkgs ...*MockPackage) *MockWebClient

func (*MockWebClient) Doc

func (m *MockWebClient) Doc(path string) (*doc.JSONDocumentation, error)

Doc simulates retrieving function docs from a package.

func (*MockWebClient) QueryPaths

func (m *MockWebClient) QueryPaths(prefix string, limit int) ([]string, error)

Sources simulates listing all source files in a package.

func (*MockWebClient) RenderRealm

func (m *MockWebClient) RenderRealm(w io.Writer, u *weburl.GnoURL, _ ContentRenderer) (*RealmMeta, error)

RenderRealm simulates rendering a package by writing its content to the writer.

func (*MockWebClient) SourceFile

func (m *MockWebClient) SourceFile(w io.Writer, pkgPath, fileName string, isRaw bool) (*FileMeta, error)

SourceFile simulates retrieving a source file's metadata.

func (*MockWebClient) Sources

func (m *MockWebClient) Sources(path string) ([]string, error)

Sources simulates listing all source files in a package.

type RealmMeta

type RealmMeta struct {
	Toc md.Toc
}

type StaticMetadata

type StaticMetadata struct {
	Domain     string
	AssetsPath string
	ChromaPath string
	RemoteHelp string
	ChainId    string
	Analytics  bool
}

StaticMetadata holds static configuration for a web handler.

type WebClient

type WebClient interface {
	// RenderRealm renders the content of a realm from a given path and
	// arguments into the giver `writer`. The method should ensures the rendered
	// content is safely handled and formatted.
	RenderRealm(w io.Writer, u *weburl.GnoURL, cr ContentRenderer) (*RealmMeta, error)

	// SourceFile fetches and writes the source file from a given
	// package path, file name and if raw. The method should ensures the source
	// file's content is safely handled and formatted.
	SourceFile(w io.Writer, pkgPath, fileName string, isRaw bool) (*FileMeta, error)

	// QueryPath list any path given the specified prefix
	QueryPaths(prefix string, limit int) ([]string, error)

	// Doc retrieves the JSON doc suitable for printing from a
	// specified package path.
	Doc(path string) (*doc.JSONDocumentation, error)

	// Sources lists all source files available in a specified
	// package path.
	Sources(path string) ([]string, error)
}

WebClient is an interface for interacting with package and node resources.

type WebHandler

type WebHandler struct {
	Logger           *slog.Logger
	Static           StaticMetadata
	Client           WebClient
	MarkdownRenderer *MarkdownRenderer
	Aliases          map[string]AliasTarget
}

WebHandler processes HTTP requests.

func NewWebHandler

func NewWebHandler(logger *slog.Logger, cfg *WebHandlerConfig) (*WebHandler, error)

NewWebHandler creates a new WebHandler.

func (*WebHandler) Get

func (h *WebHandler) Get(w http.ResponseWriter, r *http.Request)

Get processes a GET HTTP request.

func (*WebHandler) GetDirectoryView

func (h *WebHandler) GetDirectoryView(gnourl *weburl.GnoURL, indexData *components.IndexData) (int, *components.View)

func (*WebHandler) GetHelpView

func (h *WebHandler) GetHelpView(gnourl *weburl.GnoURL) (int, *components.View)

func (*WebHandler) GetMarkdownView

func (h *WebHandler) GetMarkdownView(gnourl *weburl.GnoURL, mdContent string) (int, *components.View)

GetMarkdownView handles markdown files.

func (*WebHandler) GetPackageView

func (h *WebHandler) GetPackageView(gnourl *weburl.GnoURL, indexData *components.IndexData) (int, *components.View)

GetPackageView handles package pages.

func (*WebHandler) GetPathsListView

func (h *WebHandler) GetPathsListView(gnourl *weburl.GnoURL, indexData *components.IndexData) (int, *components.View)

func (*WebHandler) GetRealmView

func (h *WebHandler) GetRealmView(gnourl *weburl.GnoURL, indexData *components.IndexData) (int, *components.View)

func (*WebHandler) GetSourceDownload

func (h *WebHandler) GetSourceDownload(gnourl *weburl.GnoURL, w http.ResponseWriter, r *http.Request)

func (*WebHandler) GetSourceView

func (h *WebHandler) GetSourceView(gnourl *weburl.GnoURL) (int, *components.View)

func (*WebHandler) ServeHTTP

func (h *WebHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handles HTTP requests.

type WebHandlerConfig

type WebHandlerConfig struct {
	Meta             StaticMetadata
	WebClient        WebClient
	MarkdownRenderer *MarkdownRenderer
	Aliases          map[string]AliasTarget
}

WebHandlerConfig configures a WebHandler.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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