fab

package module
v0.0.23 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 14, 2020 License: MIT Imports: 3 Imported by: 0

README

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.

  1. Intstall fab.io
$ go get -u github.com/kooinam/fabio
  1. Import it in your code:
import (
	fab "github.com/kooinam/fabio"
)
Quick Start
  1. Create an empty folder
$ mkdir fabio-chat-demo
$ cd fabio-chat-demo
  1. Start by creating an simple Javascript chatroom application which will be connecting to our backend services.
  2. Create an empty directory demo to hold our Javascript application codes.
$ mkdir demo
  1. Create an HTML file chat.html in demo folder and copy the snippet content over to chat.html.
  2. Now proceed to setup our backend services. Use go mod to manage our package dependencies.
$ go mod init
  1. Install fab.io.
$ go get -u github.com/kooinam/fabio
  1. 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
  1. 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
}

  1. 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))
	})
}
  1. Start our application by running
go run main.go
  1. 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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ActorManager added in v0.0.21

func ActorManager() *actors.Manager

ActorManager used to retrieve actor manager

func ControllerManager added in v0.0.21

func ControllerManager() *controllers.Manager

ControllerManager used to retrieve controller manager

func ModelManager added in v0.0.22

func ModelManager() *models.Manager

ModelManager used to retrieve model manager

func Setup

func Setup()

Setup used to setup engine

Types

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine is the core engine for fab.io

type H

type H map[string]interface{}

H is alias for map[string]interface{}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL