gnoweb

package
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 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 Aliases = map[string]string{
	"/":           "/r/gnoland/home",
	"/about":      "/r/gnoland/pages:p/about",
	"/gnolang":    "/r/gnoland/pages:p/gnolang",
	"/ecosystem":  "/r/gnoland/pages:p/ecosystem",
	"/partners":   "/r/gnoland/pages:p/partners",
	"/testnets":   "/r/gnoland/pages:p/testnets",
	"/start":      "/r/gnoland/pages:p/start",
	"/license":    "/r/gnoland/pages:p/license",
	"/contribute": "/r/gnoland/pages:p/contribute",
	"/events":     "/r/gnoland/events",
}

Aliases are gnoweb paths that are rewritten using AliasAndRedirectMiddleware.

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",
}

Redirect are gnoweb paths that are redirected using AliasAndRedirectMiddleware.

Functions

func AliasAndRedirectMiddleware

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

AliasAndRedirectMiddleware redirects all incoming requests whose path matches any of the Redirects to the corresponding URL; and rewrites the URL path for incoming requests which match any of the Aliases.

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 NewRouter

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

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

Types

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
}

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 FileMeta

type FileMeta struct {
	Lines  int
	SizeKb float64
}

type HTMLWebClient

type HTMLWebClient struct {
	Markdown  goldmark.Markdown
	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) FormatSource

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

func (*HTMLWebClient) Functions

func (s *HTMLWebClient) Functions(pkgPath string) ([]vm.FunctionSignature, error)

Functions retrieves a list of function signatures from a specified package path.

func (*HTMLWebClient) RenderRealm

func (s *HTMLWebClient) RenderRealm(w io.Writer, pkgPath string, args string) (*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) (*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 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
	GoldmarkOptions   []goldmark.Option
}

func NewDefaultHTMLWebClientConfig

func NewDefaultHTMLWebClientConfig(client *client.RPCClient) *HTMLWebClientConfig

NewDefaultHTMLWebClientConfig initializes a WebClientConfig with default settings. It sets up goldmark Markdown parsing options and default domain and highlighter.

type MockPackage

type MockPackage struct {
	Path      string
	Domain    string
	Files     map[string]string // filename -> body
	Functions []vm.FunctionSignature
}

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

func (m *MockWebClient) Functions(path string) ([]vm.FunctionSignature, error)

Functions simulates retrieving function signatures from a package.

func (*MockWebClient) RenderRealm

func (m *MockWebClient) RenderRealm(w io.Writer, path string, args string) (*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) (*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 PageData

type PageData struct {
	Layout       string
	Component    string
	IsDevmodView bool
}

PageData groups layout, component, and dev mode information.

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, path string, args string) (*RealmMeta, error)

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

	// Functions retrieves a list of function signatures from a
	// specified package path.
	Functions(path string) ([]vm.FunctionSignature, 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
}

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) (int, *components.View)

func (*WebHandler) GetHelpView

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

func (*WebHandler) GetPackageView

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

GetPackageView handles package pages.

func (*WebHandler) GetRealmView

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

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
}

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