Skip to content

fumeapp/tonic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

f3c8a18 Β· Aug 14, 2024
Aug 24, 2022
Aug 6, 2022
Aug 14, 2024
Aug 16, 2022
Oct 8, 2022
Dec 21, 2023
Sep 14, 2023
Oct 27, 2022
Mar 7, 2024
Aug 27, 2022
Aug 31, 2022
Aug 14, 2024
Apr 25, 2023
Apr 25, 2023
Aug 18, 2022
Jun 8, 2023
Aug 21, 2023
Aug 20, 2022

Repository files navigation

Tonic Web Framework

Tonic is a web application framework that supplies tools and libraries to give an expressive, elegant syntax.

Go Reference Go Report Card format lint GitHub issues GitHub license

Getting Started

Environment variables

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

Endpoint Configuration

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 via c.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{})
}

Database Connectivity

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())
}

AWS Access and Configuration

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 via aws.S3, and your Upload Manager via aws.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

Router Features

Route Model Binding

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 to Index

  • GET /user/:id binds to Show

  • PUT /user/:id binds to Update

  • DELETE /user/:id binds to Delete

  • 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}
}