Documentation
Go-Boiler
Go-boiler is desigend for creating webapps & websites with golang.
According to golang philosophy you don't need framework.
Instead of using framework, integrate libraries according to your need.Go-boiler provides routing and response rendering. Everything else is left on developer ( Yes! that's you ) including database.
Features
Go-boiler is built-on top of existing libraries. It provider very basic facilites, wrapper functions and structure for website/ webapps.
- Web Server (Golang net/http)
- Routing (Gorilla Mux)
- Static Routes
- Dynamic Routes
- Response Rendering (Cloudykit/jet & enocde/json)
- HTML Rendering
- Text Rendering
- JSON Response
- CSRF (Gorilla/csrf)
- JWT Authentication ( In process )
Installation
You can either clone the project from github or
download source code.
For clonning project
$ git clone https://www.github.com/mchampaneri/go-boiler.git
Ready to Go!
Folder Structure
- application (Root Folder)
- config
- app.json contains application configurations
- static.json contains list of static routes
- config
- public Public Folder
- resource
- js
- index.js webpack entry file
- sass
- js
- server-logic ( All gocode & Server logic)
- app.go
- boostrap.go
- config.go
- helping-functions.go custom helping function definations
- response-generator.go response generator functions
- router.go
- routes.go contains dynamic routes
- storage
- view Default root folder for views
Getting Started
You have to build and start application server for your application to start your application.
- Build server
- Open Terminal / CMD
- Goto root folder of your project
$ application\ :
- Travers to server-logic folder
$ application\server-logic\ :
- Generate server in parent folder
$ appliaction\server-logic\ : go build -o ../xerver
- Start server
- Open Terminal / CMD
- Goto root folder of your project
$ application\ :
- Start xerver executable
$ appliactions\: ./xerver
Open URL http://localhost:8081.
Static Routing
When you open URL localhost:8081. You get welcome page. It's
actully comming from static routes mapping.
{
"pages":[{
"url":"/",
"view":"index.html"
},
{
"url":"/about",
"view":"site/about.html"
}]
}
static.json
http://yoursite-name.domain/about.html
Path to the view file ( Relative path to view folder )
view
- index.html
- site
- about.html
All static routes has to be defined inside static.json as pair of URL and page.
For example we have index.html page and about.html page with "/" and "/about" route. For those route static.json and folder arrangment of files are shown in example.
Static routes has to be defined in static.json. Application need to be restarted to get static routes changes in effect. Static routes does not process any render time data. So it should be use only for complete static data.( Thouhg, you may fetch dynamic data using ajax )
Dynamic Routing
Dynmic routing is routing you specify inside dynamicRoutes()
routes.go file
// dynamicRoutes requires route
// and handling function for response.
func dynamicRoutes(router *mux.Route
router.HandleFunc("/example-route",
func(w http.ResponseWriter, r *http.Request){
// rendering view
data := make(map[string]interface)
data["user"] = "john"
View(w,r, data, "user.html")
}
routes.go
- application
-
server-logic
- routes.go
-
server-logic
You have to register every dynamic routes inside dynamicRoutes functions. To get dynamic routes in effect you need to recomile whole server again.
Dynamic routes can carry render time data with them using render function in handler. As shown in example we have passed render time data to View function.
Rendering Functions
View
View function expected to be used in routes. View function processs input paramerter with selected view template and returns processed view.
View function required 4 paramter in order defiened as below
http.ResponseWriter (w) | Where output has to be written |
*http.Request (r) | request object |
map[string]interface (dataMap) | map of data to be passed to view |
string ('site/about.html') | Path to the view file ( relative from view ) |
View(w, r, dataMap, "site/about.html")
Example
// dynamicRoutes requires route
// and handling function for response.
func dynamicRoutes(router *mux.Rout
router.HandleFunc("/example-route",
func(w http.ResponseWriter, r *http.Request){
// rendering view
data := make(map[string]interface)
data["user"] = "john"
View(w,r, data, "user.html")
})
}
routes.go
JSON
JSON function expected to be used in routes. JSON function returns data as json
JSON function required 2 paramter in order defiened as below
http.ResponseWriter (w) | Where output has to be written |
interface (data) | data to be passed as json |
JSON(w, data)
Example
// dynamicRoutes requires route
// and handling function for response.
func dynamicRoutes(router *mux.Rout
router.HandleFunc("/example-route",
func(w http.ResponseWriter, r *http.Request){
// rendering view
data := make(map[string]interface)
data["user"] = "john"
JSON(w, data)
})
}
routes.go
HTMLString
HTMLString function is similar to View function. Differance is that View function returns http response after execution. While HTMLString returns string of generated html instead of sending it as http response.
Example of use,
Mostly email service provider accept html as input for mail. You can use HTMLString function to generate HTML for you mail.
JSON function required 2 paramter in order defiened as below
interface (data) | data to be passed as json |
string ('mails/invitation.html') | Where output has to be written |
HTMLString(w, 'mails/invitation.html')
Example
func SendMail(template, from, subject, info string, to string, data interface{}) {
message := mailgun.NewMessage(
from,
subject,
info,
to)
html := HTMLString(data, template)
// Sends the mail using the mailgun
// it uses the mailgun configurations for it
message.SetHtml(html)
var mg = mailgun.NewMailgun(Config.Mail.Domain, Config.Mail.Key, Config.Mail.PublicKey)
_, _, err := mg.Send(message)
if err != nil {
DefaultLogger.Error(err.Error())
}
}
Render Time Variables
Go-boiler uses Cloudykit/JetTemplating enginer for rendering view and generating html string. Go-boiler injects certain render time variables to template.
Here is the list of varaibles currenlty available.
config | config object defined in app.json |
token | CSRF token value |
currentPATH | path of currently visible page |