Gin-bat adds the missing batteries to Gin Web Framework. Gin Web Framework is the fastest go web framework available, however it doesn't provide the most required features out-of-box. It's maily inspired by Django.
Features:
- MVC structure
- Database connection
- Basic routes
- Authentication system
- Project command line management
- Admin user interface
Provides the following sub-commands:
create-project
: create a new project, copies all the required files for building a new server, to the given pathcreate-user
: create a new user profileupdate-user
: modify an existing profiledelete-user
: delete a userchange-password
: change password access for a usercreate-token
: create a token for an existing user, to enable api usagedisplay-token
: display the token for a given userrunserver
: run the server
Once the project has been created, the following files are available for working on your web-application:
urls.go
: defines the routecontroller.go
: defines the back-end source code (handlers)models.go
: defines database tables golang objects (User
struct with its methods are provided by default)common.go
: includes function which are shared in the project. You add more there.
The remaining go files are supposed to be untouched, unless there are behaviours you wish to change to the core:
main.go
cmd.go
built-in.go
authentication.go
-
Create a project with
ginbat create-project <project-path>
<project-path>
will contain all the necessary files for running the server -
Review the
settings.json
file for database connection and host/port settings. Note, thesettings.json
supports comments starting with//
(they are discarded while reading the lines) -
Run the server
go run . runserver
Alternatively provide
-H
and/or-P
for specifying respectively Host and Portgo run . runserver -H 0.0.0.0 -P 8080
Modify templates/structure/topbar.html
Modify templates/structure/middle.html
For convenience, all imports are located in templates/structure/imports.html
- edit it for adding more.
- Create html file in
templates
folder - For giving the same style, include the "structure" html files using the
template
keyword (remember to include the dot.
character for passing the variables from the first html template to the nested ones).{{ template "Header" . }} {{ template "Imports" . }} {{ template "Middle" . }} {{ template "Topbar" . }} <!-- Content --> <h1>Home</h1> {{ template "Footer" . }} </body> </html>
- Customize the content below the comment
<!-- Content -->
leaving the inclusions untouched.
ginbat ships a javascript function raiseAlert()
(defined in templates/structure/alert.html
) which renders bootstrap5 alerts
From the controller, return
func myHandler(c *gin.Context) {
message := "My message"
status := "0" // successful status
c.HTML(http.StatusOK, "page.html", gin.H{"Feedback": map[string]string{message: status}})
}
Accepted status are:
"0"
: 🟢 success ➡️ Green"1"
: 🔴 danger ➡️ Red"2"
: 🟠 warning ➡️ Orange
Hint: multiple (unique) messages can be provided in the map.
Gin redirect works fine, however the page rendering doesn't allow to pass parameters. As workaround, you can render the page you wish and pass "Url"
parameter for adjusting the url in the browser.
This is defined in templates/structure/footer.html
From the controller, return
func myHandler(c *gin.Context) {
c.HTML(http.StatusOK, "page.html", gin.H{"Url": "/"})
}
- Use
`
as string limitator instead of"
myQuery := `SELECT id, username FROM "Users"`
- Table must be in double quotes
SELECT id, username FROM "Users"
- Avoid keyword as fields or table names (e.g:
key
andUsers
are used by Postgres) - Parameter must be specified in the query string with
$1
,$2
etc., not with?
- Bool values needs to be expressed with
0
(False) and1
(True)