Tonic is a web application framework that supplies tools and libraries to give an expressive, elegant syntax.
Take a look at .env.example for an example of how to set up environment variables. Copy it to .env
and modify them to your needs
- Any ENV variable you do not set will default to what is in
.env.example
Define an endpoint that you can call locally and remotely.
- In this example we bind
/
to a standard JSON response - You can specify params in the bind with
:
- ex:/search/:query
can be access viac.Param("query")
- With
tonic.Init()
your database and other connections are ready to use
import (
fume "github.com/fumeapp/fiber"
"github.com/fumeapp/tonic"
"github.com/fumeapp/tonic/render"
"github.com/gofiber/fiber/v2"
)
func main() {
app := tonic.Init(&fiber.Config{})
app.Get("/", func(c *fiber.Ctx) { render.Render(c, render.H{"message": "Hello World"}) })
fume.Start(app, fume.Options{})
}
Connect to both a MySQL and OpenSearch database - other engines coming soon.
- access your databases via the
database
package - Make sure to set
DB_CONNECT=true
if you want to use the MySQL database database.Db
is your mysql connection- Make sure to set
OS_CONNECT=true
if you want to use the Opensearch database database.Os
is your opensearch connection
import (
. "github.com/fumeapp/tonic/database"
)
func main() {
tonic.Init()
Db.Create(&models.User{Name: "John Doe"})
fmt.Println(Os.Info())
}
You can find the following .env
variables in .env.example
AWS_CONNECT=true
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_REGION=us-east-1
AWS_BUCKET=
- You can access your AWS Config via
aws.Config
, your AWS S3 client viaaws.S3
, and your Upload Manager viaaws.Uploader
- An upload helper can be found as
aws.Upload(url)
- this will upload a file to your bucket, mark it public, and return the URL
Easily bind a gorm Model to a controller and have Index, Show, Update, and Delete methods
route.Init(app)
route.ApiResource(app, "user", &models.User{}, controllers.UserResources())
-
4 Routes will be created:
-
GET /user
binds toIndex
-
GET /user/:id
binds toShow
-
PUT /user/:id
binds toUpdate
-
DELETE /user/:id
binds toDelete
-
Show, Update, and Delete will have the model passed in as a parameter ready to be re-casted, otherwise return a 404
func index(c *fiber.Ctx) {
var users = []models.User{}
database.Db.Find(&users)
render.Render(c, users)
}
func show(c *fiber.Ctx, value any) {
user := value.(*models.User)
render.Render(c, user)
}
func update(c *fiber.Ctx, value any) {
user := value.(*models.User)
render.Render(c, user)
}
func UserResources() route.ApiResourceStruct {
return route.ApiResourceStruct{Index: index, Show: show, Update: update}
}