Skip to main content

Goffee cli

Goffee is a cli tool, it helps with creating new Goffee projects and performing other tasks. Here is how you can install Goffee

go install git.smarteching.com/goffee/goffee@latest

Create a new project using Goffee

Here is how you can create new Goffee's projects using goffee

goffee new [project-name] [project-remote-repository]

Example

goffee new myapp git.smarteching.com/goffee/myapp

where: project-name is the name of your project remote-repository is the remote repository that will host the project.

Running the project in live reloading mode

For development purposes, you may prefer to run your project in live reloading mode to streamline the development process. To enable this, navigate to the project directory and execute the following command:

goffee run:dev

In this mode, the application will utilize the configuration file .env-dev

Running the project in production mode

To deploy the application in production mode, navigate to the project directory and run the following command:

goffee run:prod

In this mode, the application will use the configuration file .env

Generator commands

In goffee there is a way to generate different files with boilerplate code to speed up your development process, below are the different generator commands in goffee

Generate a Controller

To generate a request controller with the name UsersLogin placed in the file controllers/users.go run the following command

goffee gen:controller UsersLogin -f users.go

where: UsersLogin: is the name of the controller func -f users.go: -f is a flag that indicates the file which the controller func will be placed in, users.go is the name of the file to place the controller in, new file will be created if it does not exist. the result will be the following file.

#file: controllers/users.go
package controllers

import (
"git.smarteching.com/goffee/core"
)

func UsersLogin(c *core.Context) *core.Response {
// logic implementation goes here...

return nil
}

Generate a middleware

To generate a middleware with the name AuthCheck run the following command

goffee gen:middleware AuthCheck

this command will generate the file middlewares/auth-check.go with the following code

package middlewares

import (
"git.smarteching.com/goffee/core"
)

var AuthCheck core.Middleware = func (c *core.Context) {
c.Next()
}

Generate a model

To generate a model with the name user run the following command

goffee gen:model User

this command will generate the file models/user.go with the following code

package models

import "gorm.io/gorm"

type User struct {
gorm.Model
// add your field here...
}

// Override the table name
func (User) TableName() string {
return "users"
}

Generate Event

You can use the command goffee gen:event to generate both an event and an event job to be executed when the event is triggered, here is an example

goffee gen:event user-signedup -j SendWelcomeEmail

where: user-signedup: is the name of the event and it will be appended as constant to the file events\event-names.go SendWelcomeEmail is the job name to be executed when the event is triggered, Note: event jobs are simply functions assigned to variables of type core.EventJob, here is how the content of the event job will look like

package eventjobs

import (
"git.smarteching.com/goffee/core"
)

var SendWelcomeEmail core.EventJob = func(event *core.Event, c *core.Context) {
// logic implementation goes here...
}

Next, you need to assign the event job to the event using the events manager in the file register-events.go, here is how:

package main

import (
"git.smarteching.com/goffee/core"
"git.smarteching.com/goffee/goffee/events"
eventjobs "it.smarteching.com/goffee/goffee/events/jobs"
)

// Register events
func registerEvents() {
eventsManager := core.ResolveEventsManager()
//########################################
//# events registration #####
//########################################

// register your event here...
eventsManager.Register(events.USER_SIGNEDUP, eventjobs.SendWelcomeEmail)
}

Generate event job

you can use the command goffee gen:eventjob to generate an event job, here is how

goffee gen:eventjob SendWelcomeEmail

it will generate the file events/jobs/send-welcome-email.go with the following content

package eventjobs

import (
"git.smarteching.com/goffee/core"
)

var SendWelcomeEmail core.EventJob = func(event *core.Event, c *core.Context) {
// logic implementation goes here...
}