Documentation
¶
Overview ¶
Package via provides a reactive, real-time engine for creating Go web applications. It lets you build live, type-safe web interfaces without JavaScript.
Via unifies routing, state, and UI reactivity through a simple mental model: Go on the server — HTML in the browser — updated in real time via Datastar.
Index ¶
- func NewSQLiteSessionManager(db *sql.DB) (*scs.SessionManager, error)
- type ActionTriggerOption
- type Context
- func (c *Context) Action(f func()) *actionTrigger
- func (c *Context) Component(initCtx func(c *Context)) func() h.H
- func (c *Context) ExecScript(s string)
- func (c *Context) GetPathParam(param string) string
- func (c *Context) OnInterval(duration time.Duration, handler func()) *OnIntervalRoutine
- func (c *Context) Redirect(url string)
- func (c *Context) Redirectf(format string, a ...any)
- func (c *Context) ReplaceURL(url string)
- func (c *Context) ReplaceURLf(format string, a ...any)
- func (c *Context) Session() *Session
- func (c *Context) Signal(v any) *signal
- func (c *Context) Sync()
- func (c *Context) SyncElements(elem ...h.H)
- func (c *Context) SyncSignals()
- func (c *Context) View(f func() h.H)
- type LogLevel
- type OnIntervalRoutine
- type Options
- type Plugin
- type Session
- func (s *Session) Clear() error
- func (s *Session) Delete(key string)
- func (s *Session) Destroy() error
- func (s *Session) Exists(key string) bool
- func (s *Session) Get(key string) any
- func (s *Session) GetBool(key string) bool
- func (s *Session) GetBytes(key string) []byte
- func (s *Session) GetFloat64(key string) float64
- func (s *Session) GetInt(key string) int
- func (s *Session) GetString(key string) string
- func (s *Session) GetTime(key string) time.Time
- func (s *Session) ID() string
- func (s *Session) Keys() []string
- func (s *Session) Pop(key string) any
- func (s *Session) PopBool(key string) bool
- func (s *Session) PopBytes(key string) []byte
- func (s *Session) PopFloat64(key string) float64
- func (s *Session) PopInt(key string) int
- func (s *Session) PopString(key string) string
- func (s *Session) PopTime(key string) time.Time
- func (s *Session) RenewToken() error
- func (s *Session) Set(key string, val any)
- type V
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewSQLiteSessionManager ¶ added in v0.2.5
func NewSQLiteSessionManager(db *sql.DB) (*scs.SessionManager, error)
NewSQLiteSessionManager creates a session manager using SQLite for persistence. Creates the sessions table if it doesn't exist. The returned manager can be configured further (Lifetime, Cookie settings, etc.) before passing to Options.SessionManager.
Types ¶
type ActionTriggerOption ¶
type ActionTriggerOption interface {
// contains filtered or unexported methods
}
ActionTriggerOption configures behavior of action triggers
func WithSignal ¶
func WithSignal(sig *signal, value string) ActionTriggerOption
WithSignal sets a signal value before triggering the action.
func WithSignalInt ¶
func WithSignalInt(sig *signal, value int) ActionTriggerOption
WithSignalInt sets a signal to an int value before triggering the action.
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context is the living bridge between Go and the browser.
It holds runtime state, defines actions, manages reactive signals, and defines UI through View.
func (*Context) Action ¶
func (c *Context) Action(f func()) *actionTrigger
Action registers an event handler and returns a trigger to that event that that can be added to the view fn as any other via.h element.
Example:
n := 0
increment := c.Action(func(){
n++
c.Sync()
})
c.View(func() h.H {
return h.Div(
h.P(h.Textf("Value of n: %d", n)),
h.Button(h.Text("Increment n"), increment.OnClick()),
)
})
func (*Context) Component ¶
Component registers a subcontext that has self contained data, actions and signals. It returns the component's view as a DOM node fn that can be placed in the view of the parent. Components can be added to components.
Example:
counterCompFn := func(c *via.Context) {
(...)
}
v.Page("/", func(c *via.Context) {
counterComp := c.Component(counterCompFn)
c.View(func() h.H {
return h.Div(
h.H1(h.Text("Counter")),
counterComp(),
)
})
})
func (*Context) ExecScript ¶
func (*Context) GetPathParam ¶
GetPathParam retrieves the value from the page request URL for the given parameter name or an empty string if not found.
Example:
v.Page("/users/{user_id}", func(c *via.Context) {
userID := GetPathParam("user_id")
c.View(func() h.H {
return h.Div(
h.H1(h.Textf("User ID: %s", userID)),
)
})
})
func (*Context) OnInterval ¶
func (c *Context) OnInterval(duration time.Duration, handler func()) *OnIntervalRoutine
OnInterval starts a go routine that sets a time.Ticker with the given duration and executes the given handler func() on every tick. Use *Routine.UpdateInterval to update the interval.
func (*Context) Redirect ¶
Redirect navigates the browser to the given URL. This triggers a full page navigation - the current context will be disposed and a new context created at the destination URL.
func (*Context) Redirectf ¶
Redirectf navigates the browser to a URL constructed from the format string and arguments.
func (*Context) ReplaceURL ¶
ReplaceURL updates the browser's URL and history without triggering navigation. Useful for updating query params or path to reflect UI state changes.
func (*Context) ReplaceURLf ¶
ReplaceURLf updates the browser's URL using a format string.
func (*Context) Session ¶
Session returns the session for this context. Session data persists across page views for the same browser. Returns a no-op session if no SessionManager is configured.
func (*Context) Signal ¶
Signal creates a reactive signal and initializes it with the given value. Use Bind() to link the value of input elements to the signal and Text() to display the signal value and watch the UI update live as the input changes.
Example:
mysignal := c.Signal("world")
c.View(func() h.H {
return h.Div(
h.P(h.Span(h.Text("Hello, ")), h.Span(mysignal.Text())),
h.Input(mysignal.Bind()),
)
})
Signals are 'alive' only in the browser, but Via always injects their values into the Context before each action call. If any signal value is updated by the server, the update is automatically sent to the browser when using Sync() or SyncSignsls().
func (*Context) Sync ¶
func (c *Context) Sync()
Sync pushes the current view state and signal changes to the browser immediately over the live SSE event stream.
func (*Context) SyncElements ¶
SyncElements pushes an immediate html patch over the live SSE stream to the browser that merges with the DOM
For the merge to occur, each top lever element in the patch needs to have an ID that matches the ID of an element that already sits in the view.
Example:
If the view already contains the element:
h.Div(
h.ID("my-element"),
h.P(h.Text("Hello from Via!"))
)
Then, the merge will only occur if the ID of one of the top level elements in the patch matches 'my-element'.
func (*Context) SyncSignals ¶
func (c *Context) SyncSignals()
SyncSignals pushes the current signal changes to the browser immediately over the live SSE event stream.
type LogLevel ¶
type LogLevel int
const ( LogLevelError LogLevel LogLevelWarn LogLevelInfo LogLevelDebug )
type OnIntervalRoutine ¶
type OnIntervalRoutine struct {
// contains filtered or unexported fields
}
OnIntervalRoutine allows for defining concurrent goroutines safely. Goroutines started by *OnIntervalRoutine are tied to the *Context lifecycle.
func (*OnIntervalRoutine) Start ¶
func (r *OnIntervalRoutine) Start()
Start executes the predifined goroutine. If no predifined goroutine exists, or it already started, Start does nothing.
func (*OnIntervalRoutine) Stop ¶
func (r *OnIntervalRoutine) Stop()
Stop interrupts the predifined goroutine. If no predifined goroutine exists, or it already ustopped, Stop does nothing.
func (*OnIntervalRoutine) UpdateInterval ¶
func (r *OnIntervalRoutine) UpdateInterval(d time.Duration)
UpdateInterval sets a new interval duration for the internal *time.Ticker. If the provided duration is equal of less than 0, UpdateInterval does nothing.
type Options ¶
type Options struct {
// The development mode flag. If true, enables server and browser auto-reload on `.go` file changes.
DevMode bool
// The http server address. e.g. ':3000'
ServerAddress string
// Level of the logs to write to stdout.
// Options: Error, Warn, Info, Debug.
LogLvl LogLevel
// The title of the HTML document.
DocumentTitle string
// Plugins to extend the capabilities of the `Via` application.
Plugins []Plugin
// SessionManager enables cookie-based sessions. If set, Via wraps handlers
// with scs LoadAndSave middleware. Configure the session manager before
// passing it (lifetime, cookie settings, store, etc).
SessionManager *scs.SessionManager
// DatastarContent is the Datastar.js script content.
// If nil, the embedded default is used.
DatastarContent []byte
// DatastarPath is the URL path where the script is served.
// Defaults to "/_datastar.js" if empty.
DatastarPath string
}
Options defines configuration options for the via application
type Plugin ¶
type Plugin func(v *V)
Plugin is a func that can mutate the given *via.V app runtime. It is useful to integrate popular JS/CSS UI libraries or tools.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session provides access to the user's session data. Session data persists across page views for the same browser.
func (*Session) GetFloat64 ¶
GetFloat64 retrieves a float64 value from the session.
func (*Session) Pop ¶
Pop retrieves a value and deletes it from the session (flash message pattern).
func (*Session) PopFloat64 ¶
PopFloat64 retrieves a float64 value and deletes it from the session.
func (*Session) RenewToken ¶
RenewToken regenerates the session token (use after login to prevent session fixation).
type V ¶
type V struct {
// contains filtered or unexported fields
}
V is the root application. It manages page routing, user sessions, and SSE connections for live updates.
func (*V) AppendToFoot ¶
AppendToFoot appends the given h.H nodes to the end of the base HTML document body. Useful for including JS scripts.
func (*V) AppendToHead ¶
AppendToHead appends the given h.H nodes to the head of the base HTML document. Useful for including css stylesheets and JS scripts.
func (*V) HTTPServeMux ¶
HTTPServeMux returns the underlying HTTP request multiplexer to enable user extentions, middleware and plugins. It also enables integration with test frameworks like gost-dom/browser for SSE/Datastar testing.
IMPORTANT. The returned *http.ServeMux can only be modified during initialization, before calling via.Start(). Concurrent handler registration is not safe.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package h provides a Go-native DSL for HTML composition.
|
Package h provides a Go-native DSL for HTML composition. |
|
internal
|
|
|
examples/chatroom
command
|
|
|
examples/counter
command
|
|
|
examples/countercomp
command
|
|
|
examples/greeter
command
|
|
|
examples/livereload
command
|
|
|
examples/pathparams
command
|
|
|
examples/picocss
command
|
|
|
examples/plugins
command
|
|
|
examples/realtimechart
command
|
|
|
examples/session
command
|
|
|
examples/shakespeare
command
|