Documentation ¶
Index ¶
- type ActionCtx
- type ActionFunc
- type Page
- type PageCtx
- type PageFunc
- type Server
- func (s *Server) AddAction(name string, fn ActionFunc)
- func (s *Server) AddFiles(baseURL string, fs http.FileSystem)
- func (s *Server) AddPage(path string, fn PageFunc)
- func (s *Server) AddStream(name string, fn StreamFunc)
- func (s *Server) Router() *httprouter.Router
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- type StreamFunc
- type Update
- func (u *Update) AddInsertAfter(selector, content string)
- func (u *Update) AddInsertBefore(selector, content string)
- func (u *Update) AddJSCall(name string, args any)
- func (u *Update) AddReplaceContent(selector, content string)
- func (u *Update) AddReplaceElement(selector, content string)
- func (u *Update) AddStream(name string, args any)
- type UpdateablePage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ActionCtx ¶
type ActionCtx struct { Writer http.ResponseWriter Request *http.Request State json.RawMessage Args json.RawMessage }
ActionCtx is the context that is passed to an ActionFunc. It extends the Request and Writer from a typical HTTP request handler with State and Args fields from the Action call that were sent from the browser.
type ActionFunc ¶
ActionFunc is the action handler function that should return an Update in response to the call from the client. ActionFuncs are registered with the Server using the AddAction() function.
type Page ¶
Page gets returned from a PageFunc. The page needs to be able to write the HTML representation of the page to an io.Writer. It can be extended into an UpdateablePage by implementing Update().
type PageCtx ¶
type PageCtx struct { Writer http.ResponseWriter Request *http.Request Params httprouter.Params // params from placeholders in the URL }
PageCtx is the context that is passed to a PageFunc. It extends the Request and Writer from a typical HTTP request handler with Params from the HTTP router.
For more info on httrouter.Params see: https://github.com/julienschmidt/httprouter
type PageFunc ¶
PageFunc is the page handler function that should return a Page value in response to a HTTP request or guiapi page request. PageFuncs are registered with the using the AddPage() function.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server contains all the registered Pages, Actions, Files and Streams. It implements the http.Handler interface, so it can be directly passed to a function like http.ListenAndServe(). When a request comes in, the server will handle GET requests for pages, POST requests for actions, and WebSocket requests for streams.
func New ¶
func New() *Server
New returns a new guiapi Server. After registering all the Pages, Actions, Files and Streams, the server can be directly used as a http.Handler.
func (*Server) AddAction ¶
func (s *Server) AddAction(name string, fn ActionFunc)
AddAction registers an ActionFunc with the passed name and handler function on the server.
func (*Server) AddFiles ¶
func (s *Server) AddFiles(baseURL string, fs http.FileSystem)
AddFiles registers a http.FileSystem with the passed baseURL on the server. The files can also be in a subdirectory of the baseURL. This function is the main way of serving static files from the guiapi server.
func (*Server) AddPage ¶
AddPage registers a PageFunc with the passed path and page handler function on the server. The URL path can contain placeholders in the form of :name, which then get passed as Params in a PageCtx.
See https://github.com/julienschmidt/httprouter for more info.
func (*Server) AddStream ¶
func (s *Server) AddStream(name string, fn StreamFunc)
AddStream registers a StreamFunc with the passed name and handler function on the server.
func (*Server) Router ¶
func (s *Server) Router() *httprouter.Router
Router returns the underlying HTTP router. This way any additional endpoints can be added to the HTTP server, bypassing guiapi.
See https://github.com/julienschmidt/httprouter for more info.
type StreamFunc ¶
StreamFunc is the type of a stream handler function. The initial arguments from the client side are passed as JSON in args. Any time an update is ready to be sent, it needs to be sent to the res channel. The stream can be closed by returning from the function. If the client side closes the connection, the context will be canceled. Because of this it is important to check ctx.Done() regularly.
type Update ¶
type Update struct { Name string `json:",omitempty"` // Name of the action that was called URL string `json:",omitempty"` // URL that was loaded Error *api.Error `json:",omitempty"` // Error that occurred while handling the action HTML []api.HTMLUpdate `json:",omitempty"` // DOM updates to apply JS []api.JSCall `json:",omitempty"` // JS calls to execute State any `json:",omitempty"` // State to pass back to the browser Stream []api.Stream `json:",omitempty"` // Stream to subscribe to via websocket }
Update is the returned body of a GUI API call.
func InsertAfter ¶
InsertAfter returns a new Update that inserts HTML content on the same level after the passed selector. The selector gets passed to document.querySelector, so it can be any valid CSS selector.
func InsertBefore ¶
InsertBefore returns a new Update that inserts HTML content on the same level before the passed selector. The selector gets passed to document.querySelector, so it can be any valid CSS selector.
func JSCall ¶
JSCall returns a new Update that will call the registered JavaScript function that is identified by the name, and will pass the arguments.
func ReplaceContent ¶
ReplaceContent returns a new Update that replaces the content of the element that gets selected by the passed selector with the HTML content. The selector gets passed to document.querySelector, so it can be any valid CSS selector.
func ReplaceElement ¶
ReplaceElement returns a new Update that replaces the whole element that gets selected by the passed selector with the HTML content. The selector gets passed to document.querySelector, so it can be any valid CSS selector.
func Stream ¶
Stream returns a new Update that will connect to a stream with the passed name and arguments.
func (*Update) AddInsertAfter ¶
AddInsertAfter adds a HTML update that inserts HTML content on the same level after the passed selector. The selector gets passed to document.querySelector, so it can be any valid CSS selector.
func (*Update) AddInsertBefore ¶
AddInsertBefore adds a HTML update that inserts HTML content on the same level before the passed selector. The selector gets passed to document.querySelector, so it can be any valid CSS selector.
func (*Update) AddJSCall ¶
AddJSCall adds a new JSCall Update that will call the registered JavaScript function that is identified by the name, and will pass the arguments.
func (*Update) AddReplaceContent ¶
AddReplaceContent adds a new HTML Update that replaces the content of the element that gets selected by the passed selector with the HTML content. The selector gets passed to document.querySelector, so it can be any valid CSS selector.
func (*Update) AddReplaceElement ¶
AddReplaceElement adds a HTML Update that replaces the whole element that gets selected by the passed selector with the HTML content. The selector gets passed to document.querySelector, so it can be any valid CSS selector.
type UpdateablePage ¶
UpdateablePage is a Page that can also produce a relative Update. This means that the browser can just update the relevant parts of the page and doesn't have to reload the whole page.