fab

package module
v1.0.55 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2020 License: MIT Imports: 9 Imported by: 0

README

Contributors Forks Stargazers Issues MIT License

Fab.io

Fab.io is a lightweight real-time game backend framework written in Go (Golang).

Table of Contents

Getting Started

Prerequisites

To install Fab.io package, you will need to:

  1. Have Go installed. Head over to Go's download page here to install it.
  2. Setup your Go workspace.
Installation
  1. Install Fab.io
$ go get -u github.com/kooinam/fab.io
  1. Import it in your code:
import (
	fab "github.com/kooinam/fab.io"
)

Usage

Example - (Simple JavaScript Chatroom App):

In our first example, we start by creating an simple JavaScript chatroom application which will be connecting to backend services using Fab.io.

Setting up your workspace:
  1. Create an empty directory. In this example we shall use fabio-chat-demo:
$ mkdir fabio-chat-demo
$ cd fabio-chat-demo
  1. Create an empty directory demo inside fabio-chat-demo to hold our Javascript application codes.
$ mkdir demo
  1. Create an HTML file chat.html in the demo folder and copy the snippet content over into chat.html.
Setting up backend services:

Now, let's proceed to setup our backend services.

  1. Use the go mod command to manage our package dependencies. Let's go ahead and initialize our package dependencies:
$ go mod init fabio-chat-demo
  1. Install Fab.io.
$ go get -u github.com/kooinam/fab.io
  1. Create an empty directory controllers inside fabio-chat-demo to hold our controllers. A controller is responsible for handling any request and producing the appropriate output. Every controller should implement two functions RegisterBeforeHooks and RegisterActions.
$ 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/fab.io"
	"github.com/kooinam/fab.io/controllers"
	"github.com/kooinam/fab.io/helpers"
)

// ChatController used for chat purposes
type ChatController struct {
}

// RegisterHooksAndActions used to register hooks and actions
func (controller *ChatController) RegisterHooksAndActions(hooksHandler *controllers.HooksHandler, actionsHandler *controllers.ActionsHandler) {
	actionsHandler.RegisterAction("Join", controller.join)
	actionsHandler.RegisterAction("Message", controller.message)
}

// join used for player to join a room
func (controller *ChatController) join(context *controllers.Context) {
	roomID := context.ParamsStr("roomID")

	// leave all previously joined rooms, and join new room
	context.SingleJoin(roomID)
}

// message used for player to send message message to room
func (controller *ChatController) message(context *controllers.Context) {
	roomID := context.ParamsStr("roomID")
	message := context.ParamsStr("message")

	// broadcast message to room
	fab.ControllerManager().BroadcastEvent("chat", roomID, "Message", nil, helpers.H{
		"message": message,
	})
}
  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/fab.io"
)

func main() {
	fab.Setup()

	fab.ControllerManager().RegisterController("chat", &controllers.ChatController{})

	fab.ControllerManager().Serve("8000", func() {
		fs := http.FileServer(http.Dir("./demo"))
		http.Handle("/demo/", http.StripPrefix("/demo/", fs))
	})
}
You are done!

Congrats! Now all that's left to do is run the app!

  1. Start our application by running:
go run main.go
  1. Navigate to http://localhost:8000/demo/chat.html on your browser to see your chatroom application in action!
Interested on other use cases?

Expore more example use cases by reading our Wiki!

Resources

Dependencies

Package Link
go-socket.io github.com/googollee/go-socket.io

Roadmap

Some of our upcoming key feature(s)/improvement(s) include:

  • Write MORE Tests
  • Tutorials and Documentations
  • Containerize Solutions
  • Distributed Solutions
  • Graceful Shutdown
  • Actor Model

License

Distributed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ActorManager

func ActorManager() *actors.Manager

ActorManager used to retrieve actor manager

func ConfigureAndServe

func ConfigureAndServe(initializer Intializer)

ConfigureAndServe used to setup applications and start server register adapters, collections for modelmanager register controllers, routings for controllermanager register views for viewmanager configure configuration

func ControllerManager

func ControllerManager() *controllers.Manager

ControllerManager used to retrieve controller manager

func ModelManager

func ModelManager() *models.Manager

ModelManager used to retrieve model manager

func Setup

func Setup()

Setup used to setup engine

func ViewManager

func ViewManager() *views.Manager

ViewManager used to retrieve view manager

Types

type Configuration

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

Configuration used to configure application wide settings

func (*Configuration) SetHttpHandler

func (configuration *Configuration) SetHttpHandler(httpHandler func())

func (*Configuration) SetPort

func (configuration *Configuration) SetPort(port string)

type Engine

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

Engine is the core engine for fab.io

type Intializer

type Intializer interface {
	// Configure used to configure configurations like port and httphandler
	Configure(*Configuration)

	// RegisterAdapters used to register adapters
	RegisterAdapters(*models.Manager)
	// RegisterCollections used to register collections
	RegisterCollections(*models.Manager)
	// RegisterControllers used to register controllers
	RegisterControllers(*controllers.Manager)
	// RegisterViews used to register views
	RegisterViews(*views.Manager)

	//BeforeServe used for custom initializations after fab.io initializes and before serving like loading some seed application data
	BeforeServe()
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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