Tour of gFly

Tour of gFly #

Assume you finish step Installation by using Docker Setup or Local Setup

Let open the source with any your favorite IDEs: VSCode, GoLand, NeoVim

We will experiment with the example of creating a page HelloPage and an API HelloApi controllers.

First of all. Need to start your application by below command:

# Local setup
make air
# Docker setup
make docker.start

Check browser http://localhost:7789/

Create a HelloPage #

Controller #

Create file hello_page.go in folder app/http/controllers/page/

package page

import (
    "app/core/gfly"
)

// ===============================================================================
//                                  Hello page
// ===============================================================================

// NewHelloPage As a constructor to create new Page.
func NewHelloPage() *HelloPage {
    return &HelloPage{}
}

type HelloPage struct {
    gfly.Page
}

func (m *HelloPage) Handle(c *gfly.Ctx) error {
    return c.HTML("<h2>Hello world</h2>")
}

Define router #

Declare HelloPage into app/http/routes/web_routes.go

package routes

import (
    "app/app/http/controllers/page"
    "app/core/gfly"
)

// WebRoutes func for describe a group of Web page routes.
func WebRoutes(f gfly.IFly) {
    // Web Routers
    f.GET("/hello", page.NewHelloPage())
}

Checking #

Browse http://localhost:7789/hello

Create a HelloAPI #

Controller #

Create file hello_api.go in folder app/http/controllers/api/

package api

import (
	"app/core/gfly"
	"fmt"
	"os"
)

// ===============================================================================
//                                  Hello API
// ===============================================================================


// NewHelloApi As a constructor to create new API.
func NewHelloApi() *HelloApi {
	return &HelloApi{}
}

// HelloApi API struct.
type HelloApi struct {
	gfly.Api
}

// Handle Process main logic for API.
func (h *HelloApi) Handle(c *gfly.Ctx) error {
	obj := map[string]any{
		"name": os.Getenv("API_NAME"),
		"server": os.Getenv("APP_NAME"),
	}

	return c.JSON(obj)
}

Define router #

Declare HelloApi into app/http/routes/api_routes.go

package routes

import (
	"app/app/http/controllers/api"
	"app/core/gfly"
	"fmt"
	"os"
)

// ApiRoutes func for describe a group of API routes.
func ApiRoutes(f gfly.IFly) {
	prefixAPI := fmt.Sprintf(
		"/%s/%s",
		os.Getenv("API_PREFIX"),
		os.Getenv("API_VERSION"),
	)

	f.Group(prefixAPI, func(apiRouter *gfly.Group) {
		// curl -v -X GET http://localhost:7789/api/v1/hello | jq
		apiRouter.GET("/hello", api.NewHelloApi())
	})
}

Checking #

Browse http://localhost:7789/api/v1/hello

Or terminal

curl -v -X GET http://localhost:7789/api/v1/hello