webby

package module
v0.0.0-...-29ebb52 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2023 License: 0BSD Imports: 13 Imported by: 0

README

webby

it builds websites

Usage

Example project structure:

- main.go
- index.html
- components/
 |- components.go
 |- some_component.html
- public/
 |- style.css
 |- favicon.ico

Configure a page (main.go):

var PageIndex = &webby.Page{
	Location:   "/", // Location in website
	Title:      "jnichols", // Page title, included in PageMeta
	Icon:       "/favicon.ico", // Icon, included in PageMeta (default /favicon.ico)
	Styles:     []string{"/style.css"}, // Stylesheets to import, included in PageMeta
	File:       "/index.html", // 
	Components: []*webby.Component{components.Head, components.Navbar, components.Footer},
}

Configure a component (components.go):

var Head = &webby.Component{
	Name: "Head",
	File: "/components/head.html",
}

Render a component in HTML (index.html):

<!DOCTYPE html>
{{ render .Components.Head .PageMeta }}
<body>
    {{ render .Components.Navbar nil }}
    <div class="main">
    ...

You can alter the input data of a component or page by defining OnLoad...

var Component = &webby.Component {
    ...
    OnLoad = func(input any) any {
        return "Hello"!
    }
}

...and access data in the template through .Data:

<head>
    <title>{{ .Data.Title }}</title>
    <link rel="icon" type="image/x-icon" href="{{ .Data.Icon }}">
{{ range .Data.Styles }}
    <link rel="stylesheet" href="{{ . }}">
{{ end }}
    <meta name="viewport" content="width=device-width, initial-scale=1" />
</head>

License

webby is free software. The Zero-Clause BSD license is OSI approved.

Support

If you want to support my work, please consider buying me a coffee on Ko-fi.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LogRequests

func LogRequests(logger *slog.Logger) middleware.Middleware

func NewHandler

func NewHandler(max slog.Level, tag string, out io.Writer) slog.Handler

func NewLogger

func NewLogger(max slog.Level, tag string, out io.Writer) *slog.Logger

Types

type Component

type Component struct {
	Name       string
	File       string
	Components []*Component
	// OnLoad transforms whatever input data is passed to the Component into a different data to use for rendering
	OnLoad func(input any) any
	// contains filtered or unexported fields
}

A Component is renderable on any data. Typically this is a sub-item contained in a page and rendered through "render".

func (*Component) Compile

func (p *Component) Compile() (err error)

func (*Component) Render

func (p *Component) Render(data any) string

type Config

type Config struct {
	// Setting Debug to true sets the server to debug, increasing log verbosity and recompiling pages on refresh.
	Debug bool
	// RootDir is the base directory to serve files from.
	RootDir string
	// PublicDir is the base directory of public files, and is always served at the root /. Public files shouldn't match the paths of any Pages.
	// Leaving PublicDir empty will not server any public files.
	PublicDir string
	// Pages are all of the pages you want to render in the website.
	Pages []*Page
	// LogLevel is the max log level for the server. Defaults to slog.LevelInfo.
	// If Debug is set to true, this is *always* overridden to slog.LevelDebug.
	LogLevel slog.Level
}

Config is the base configuration for a Server. It's not explicitly required that you build a server this way, but I think it's handy.

type Page

type Page struct {
	Location   string
	Title      string
	Icon       string
	Styles     []string
	File       string
	Components []*Component
	// OnLoad transforms the input request into arbitrary data for Render.
	OnLoad func(req *http.Request) any
	// contains filtered or unexported fields
}

Pages are components that are Renderable on an *http.Request. These don't technically have to be HTML, but they usually are.

func (*Page) Compile

func (p *Page) Compile() (err error)

Compile prepares a page for rendering.

func (*Page) Render

func (p *Page) Render(req *http.Request) string

Render renders the page to string data. If in debug mode, Render will recompile before rendering.

type PageMeta

type PageMeta struct {
	Title  string
	Icon   string
	Styles []string
}

PageMeta is passed to a Page on render.

type RenderData

type RenderData struct {
	Components map[string]RenderFunc
	PageMeta   PageMeta
	Data       any
}

RenderData is the base structure passed to *all* pages and components on render.

type RenderFunc

type RenderFunc func(any) string

RenderFunc is any function that renders arbitrary data to a string.

type Server

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

Server is the base-level object for serving up the website. It manages pages, the public directory, and logging.

func New

func New(conf Config) (serv *Server)

New creates a new Server from a Config.

func (*Server) Compile

func (serv *Server) Compile() (err error)

Compile prepares the server to handle HTTP requests. Run fails if called before Compile, so make sure to always do this.

func (*Server) Log

func (serv *Server) Log(level slog.Level, msg string, attrs ...any)

Log aliases slog.Logger.Log for the server's current logger. Currently, servers always use rfc3164 format.

func (*Server) Page

func (serv *Server) Page(page *Page)

Page compiles and prepares to serve a Page.

func (*Server) Public

func (serv *Server) Public(directory string)

Public prepares to serve a directory at the root URI. Public files are always served after any page locations, so given a public file page.html and a page at location page.html, the page is *always* loaded.

func (*Server) Run

func (serv *Server) Run()

Run listens on 0.0.0.0:3000 for traffic to the server.

func (*Server) ServeHTTP

func (serv *Server) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP implements http.Handler.

type SyslogHandler

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

func (*SyslogHandler) Enabled

func (handler *SyslogHandler) Enabled(ctx context.Context, level slog.Level) bool

func (*SyslogHandler) Handle

func (handler *SyslogHandler) Handle(ctx context.Context, r slog.Record) error

func (*SyslogHandler) WithAttrs

func (handler *SyslogHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*SyslogHandler) WithGroup

func (handler *SyslogHandler) WithGroup(name string) slog.Handler

Jump to

Keyboard shortcuts

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