Ecosyste.ms: Packages

An open API service providing package, version and dependency metadata of many open source software ecosystems and registries.

proxy.golang.org : modernc.org/dyd

Command dyd compiles HTML with embedded Go scripts to a self-contained, go install-able, single binary server producing dynamic web pages. The above file is all you need to produce a web server for a hello world example. - A working Go toolchain must be installed to use dyd. You can install Go from here: https://go.dev/dl/. - The goimports tool is needed. To install goimports issue 'path' points to the web server root, it defaults to ".". -v adds more verbose output. -addr is the web server address. It's the same argument as the first http.ListeAndServe wants. It defaults to ":6226". -modinit creates a go.mod file with module path <arg> at 'path'. Ignored if a mod file at 'path' already exists. -clean removes all generated files in the tree starting at 'path' and exits. -serve compiles and runs the server. -htmlfmt attempts to format html files. Use with caution, formatting may in some cases lose information. A backup is made before overwriting the HTML file. For example The preprocessor can handle this just fine, the formatter cannot. Write instead Using the hello world example from above in index.html. Generate and run the server (`-v` is optional). Stop the server. Every directory is - or becomes - a Go package. dyd looks for .html files and generates Go functions serving the content. Any Go code can appear within HTML processing instructions `<?` and `?>`. This code is inserted into the serving function. Go code can refer to two arguments of the serving function. The 'ctx' argument provides context. The type is documented at http://modernc.org/dyd/dyd#Context. The other argument is 'write' and its a function declared as The write function is only a small helper writing to the ctx.ResponseWriter. is the same as The web root directory must be package main, directories under the root cannot be package main. If there's no main.go/func main() in the web root, dyd will create one. If a manually written main.go/func main() exists, it will be kept. To invoke the web server in your manual func main(), add this as the first line of the function body or etc. The 'dydStart' will be defined automatically in dyd.go. Consider this file name reserved by dyd. Let's see what was generated for the Hello world example. The dyd.go file contains the `dydStart` function. It registers all the serving functions to handle the respective URLs. Preprocessed index.html is in index.html.go. And finally, main.go. You can write a function with this signature in any of the directories Replace <X> with what this function returns (copied from modernc.org/dyd/etc.go) For example, "index.html" becomes "index_1html" and the complete function name will be "Serve_index_1html". dyd can find such functions and adds them, using the decoded file name to the site map so the respective URLs will get handled by the user function. The function needs, of course, to produce a complete HTML document. From dyd's POV a HTML file is an UTF-8 encoded text file with zero or more processing instructions embedded. Processing instructions between the `<?` and `?>` tags are used as Go code verbatim. The rest of the HTML file is used as HTML and again verbatim. That means a HTML file intended for dyd consumption does not have to be a complete, well-formed HTML document. Sometimes it is not because, for example, opening/closing tags can come from both HTML and Go, possibly intermixed. the preprocessor does not parse anything found in a HTML file, it looks only for the PI tags. Also, the embedded Go code can contain characters forbidden in a HTML file. The well-formedness of HTML can be checked only after the web server fully produces a complete document and this is not computable at dyd/Go compile time in the general case. The processing instruction opening and closing tags cannot nest and there's no escape sequence for the PI tags. HTML file names should preferably match `[a-zA-Z0-9-.]*`. HTML files matching `^.*_\.html$`, like top_.html, are not externally bound, the web server will not handle their URLs. But all HTML URLs can be used by `ctx.Include()`. Use `ctx.Include(path)` to insert static/dynamic HTML, for example. See examples/include for a full, working demo. Use `ctx.W3CSS()` to get the content of https://www.w3schools.com/w3css/4/w3.css without using an internet connection. It can be used like this More information about W3.CSS can be found at https://www.w3schools.com/w3css/default.asp. Use `ctx.DBConn()` to get a connection to the persistent database of the web server instance, created on demand in the working directory. It's the CGo-free SQLite database (http://modernc.org/sqlite). See examples/db for a full, working demo. Use `ctx.KV()` to access a persistent key/value store, created on demand within the web server instance database. Full documentation available at http://modernc.org/dyd/dyd. See examples/kv for a full, working demo.

Registry - Source - Documentation - JSON
purl: pkg:golang/modernc.org/dyd
Keywords: go, server-side-scripting, web
License: BSD-3-Clause
Latest release: 7 months ago
First release: about 1 year ago
Namespace: modernc.org
Stars: 1 on GitLab.com
Forks: 0 on GitLab.com
See more repository details: repos.ecosyste.ms
Last synced: 19 days ago

    Loading...
    Readme
    Loading...