A simple example site using Vincent. You can see the live demo at http://www.vincentgo.org.
If you don't have a golang development environment already, install golang.
This example is a simple HTTP server, configured using the golang flag
package.
To create the server, call New
with an optional logger instance.
In main.go:
// Create vincent server
logger.Debug("Creating Server")
svr, err := vincent.New(logger)
if err!=nil { logger.Error("Cannot Create Server: %s", err); return }
Your site files should be in a directory (templates in this example). Template files use the handlebars format and should have the .hbs
extension - all other files are served as-is. Templates are loaded using the LoadTemplates
function on Server
.
In main.go:
// Load templates from the templates/ directory
logger.Debug("Loading Templates")
err = svr.LoadTemplates("","templates")
if err!=nil { logger.Error("Cannot load Templates: %s", err); return }
Controllers are registered on routes and executed when a request traverses the route, before any templates are passed. All keys set in Output
will be available to all templates under that route.
Example: Here, we bind a controller to the root path /
- meaning it will be executed on all requests. We set two context variables in Output
- version
and port
.
In main.go:
// This is an example controller
svr.AddController("/", func(context *vincent.Context) (bool, error) {
context.Output["version"]="1.0.1"
context.Output["port"] = *cfg.HTTPPort
return true, nil
})
In index.html.hbs:
Starting the server causes Vincent to listen on the configured port and start serving requests. The Vincent server runs in its own goroutine, a call to Start
will return immediately.
In main.go:
// Start the server on the configured port
logger.Debug("Starting HTTP Server")
svr.Start(fmt.Sprintf(":%d", *cfg.HTTPPort))
logger.Info("HTTP Listening on port %d", *cfg.HTTPPort)
vincent-demo is licensed under the Open Source MIT license. Please see the License File for more details.
The vincent project supports and enforces The Contributor Covenant. Please read the code of conduct before contributing.