Fab.io
Fab.io is a lightweight game backend framework written in Go (Golang).
- MVC Pattern
- Actor Model
- Powered by socket.io
Installation
To install Fab.io package, you need to install Go and set your Go workspace first.
- Intstall fab.io
$ go get -u github.com/kooinam/fabio
- Import it in your code:
import (
fab "github.com/kooinam/fabio"
)
Quick Start
- Create an empty folder
$ mkdir fabio-chat-demo
$ cd fabio-chat-demo
- Start by creating an simple Javascript chatroom application which will be connecting to our backend services.
- Create an empty directory
demo
to hold our Javascript application codes.
$ mkdir demo
- Create an HTML file
chat.html
in demo
folder and copy the snippet content over to chat.html
.
- Now proceed to setup our backend services. Use
go mod
to manage our package dependencies.
$ go mod init
- Install fab.io.
$ go get -u github.com/kooinam/fabio
- Create an empty directory
controllers
to hold our controllers. An controller is reponsible for handling any request and producing the appropriate output. Every controller should implement two functions AddBeforeActions
and AddActions
.
$ mkdir controllers
- Create an go file
chat_controller.go
in controllers
folder. Put the following snippet content into chat_controller.go
.
package controllers
import (
fab "github.com/kooinam/fabio"
"github.com/kooinam/fabio/controllers"
)
// ChatController used for chat purposes
type ChatController struct {
}
// AddBeforeActions used to add before actions callbacks
func (controller *ChatController) AddBeforeActions(callbacksHandler *controllers.CallbacksHandler) {
}
// AddActions used to add actions
func (controller *ChatController) AddActions(actionsHandler *controllers.ActionsHandler) {
actionsHandler.AddAction("Join", controller.join)
actionsHandler.AddAction("Message", controller.message)
}
// join used for player to join a room
func (controller *ChatController) join(connection *controllers.Connection) (interface{}, error) {
var err error
roomID := connection.ParamsStr("roomID")
// leave all previously joined rooms, and join new room
connection.SingleJoin(roomID)
return nil, err
}
// message used for player to send message message to room
func (controller *ChatController) message(connection *controllers.Connection) (interface{}, error) {
var err error
roomID := connection.ParamsStr("roomID")
message := connection.ParamsStr("message")
// broadcast message to room
fab.BroadcastEvent("chat", roomID, "Message", nil, fab.H{
"message": message,
})
return nil, err
}
- Lastly, create
main.go
in root directory and put the following snippet content into main.go
.
package main
import (
"fabio-chat-demo/controllers"
"net/http"
fab "github.com/kooinam/fabio"
)
func main() {
fab.Setup()
fab.RegisterController("chat", &controllers.ChatController{})
fab.Serve(func() {
fs := http.FileServer(http.Dir("./demo"))
http.Handle("/demo/", http.StripPrefix("/demo/", fs))
})
}
- Start our application by running
go run main.go
- Navigate your browser to
http://0.0.0.0:8000
to see our chatroom application in action.
Examples
Dependencies
Package |
Link |
go-socket.io |
github.com/googollee/go-socket.io |
Todos
- Write MORE Tests
- Tutorials and Documentations
- Containerize Solutions
- Distributed Solutions
- Graceful Shutdown
- Actor Model
License
MIT