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

Change a user password​

To change the password of a user account run the following command:

goffee user:password [username] [newpassword] [dev|prod]

Example

goffee user:password admin 'newpass123' prod

where:

  • username: is the name of the user to update
  • newpassword: is the new password to set
  • dev|prod: selects the environment file to load (dev uses .env-dev, prod uses .env)

Managing the scheduler​

The scheduler commands operate on the scheduler tables of the database configured in the selected environment. They must be run from the project directory.

goffee scheduler:queue     dev          # list pending/processing tasks
goffee scheduler:processed dev 20 # list the last 20 processed executions
goffee scheduler:semaphore dev # show the semaphore state
goffee scheduler:semaphore dev red # block execution (takes effect after restart)
goffee scheduler:truncate dev # empty the queue and processed logs

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 Handler​

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

goffee gen:handler UsersLogin -f users.go

where: UsersLogin: is the name of the handler func -f users.go: -f is a flag that indicates the file which the handler func will be placed in, users.go is the name of the file to place the handler 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/v2"
)

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

return nil
}

Generate a hook​

A hook (formerly called middleware) is a core.Hook function that runs before or after the handling of a request. To generate a hook with the name AuthCheck run the following command

goffee gen:hook AuthCheck

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

package hooks

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

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

To learn more about hooks check the hooks docs section.

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

type User struct {
BaseModel
// add your field here...
}

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

The generated model embeds the shared BaseModel (defined in models/base.go), which provides the ID, Created and Updated fields used across all models in the project.

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/v2"
)

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/v2"
"git.smarteching.com/goffee/[myapp]/events"
"git.smarteching.com/goffee/[myapp]/events/eventjobs"
)

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

// register your event here...
eventsManager.Register(events.USER_REGISTERED, 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/eventjobs/send-welcome-email.go with the following content

package eventjobs

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

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