You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
gohtmlbinder is a lightweight Go package that makes it easy to bind HTML templates to HTTP routes using Gorilla Mux.
It provides a simple and reusable way to serve web pages without having to manually handle html/template, routes, and servers in every project.
Check the registred routes on the /status auto generated route:
b.Status()
Start the server:
b.serve(":port")
Example
Static data
package main
import binder "github.com/Masterpat48/gohtmlbinder"funcmain() {
//initialize the binder W the index file (need to have the index file created)b:=binder.New("index.html")
//create the main routeb.NewRoute("/", "index.html")
//print in console all the registred routesb.PrintRoutes()
//Check the routes on the /status routeb.Status()
//start the server on port 1000b.Serve(":1000")
}
Dynamic data
package main
import (
"net/http""sync""github.com/Masterpat48/gohtmlbinder"
)
funcmain() {
b:=binder.New("index.html")
varmu sync.Mutexcount:=0// Main route to show the numberb.NewRouteData("/", "index.html", func(r*http.Request) any {
mu.Lock()
defermu.Unlock()
returnmap[string]any{"Count": count}
})
// Route to increment the numberb.NewRouteData("/increment", "index.html", func(r*http.Request) any {
ifr.Method==http.MethodPost {
mu.Lock()
count++mu.Unlock()
}
mu.Lock()
defermu.Unlock()
returnmap[string]any{"Count": count}
})
b.PrintRoutes()
b.Status()
b.Serve(":1000")
}