This repository serves as an example of a RESTful API written in golang
- bunorm for database connections
- bunrouter for http routing
- golang-jwt/v5 for authentication
- urfavcli/v2 for command line interface integration
Install golang and make binaries.
sudo apt update && sudo apt upgrade
sudo apt install golang makefilePull the code to local directory ~/GolandProjects/bunapp-api.
git clone https://github.com/alpha-omega-corp/bunapp-api.git ~/GolandProjects/bunapp-apiOpen the project in Goland and install dependencies.
go mod download
go mod tidyCreate a dev config file from the test config file in app/config.
cp ./app/config/test.yaml ./app/config/dev.yamlCreate a dev database from the docker-compose file.
make db_createCreate migrations table for bun.
make db_initRun the database reset command once to create required tables.
make db_resetStart the application using the Makefile.
make serverOr manually using go run.
go run cmd/main.go -env=dev servermain.go handles the application's cli commands.
Top levels commands:
go run cmd/main.go -env=dev server
go run cmd/main.go -env=dev dbDatabase commands:
go run cmd/main.go -env=dev db init
go run cmd/main.go -env=dev db resetServer commands:
go run cmd/main.go -env=dev server startThe app directory contains the code responsible for creating an app *App instance.
-
Create a directory
exampleinsidegithub.com/alpha-omega-corp/bunapp-api -
From
examplecreate ainit.gofile with the following code:
package example
import (
"context"
"github.com/alpha-omega-corp/bunapp-api/app"
)
func Bootstrap() {
app.OnStart("example.init", func (ctx context.Context, app *app.App) error {
//app.Router()
//app.ApiRouter()
//app.Database()
//app.Repositories()
//app.GptClient()
//app.PromptManager()
})
}Example: api-app
The callback function gives you access to the app *App instance
To start the example application call the public Bootstrap function from the cmd/main.go file.
package main
import (
"github.com/alpha-omega-corp/bunapp-api/example"
)
func main() {
//...
Action:
func(c *cli.Context) error {
example.Bootstrap()
} //...
}Example: cmd/main.go