Skip to content

ScottKGregory/tonic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🍸 Tonic

Build Status Go Report Card GitHub go.mod Go version GitHub

What is Tonic?

Tonic is a set of helpers and pre-built endpoints helpful for setting up a Gin based website/API. It's primarily set up to enable quick setup of a web API with cookie and bearer token auth using an OIDC provider.

Tonic is great for quickly getting something up and running, especially if you don't need to adjust how users and permissions are handled. In larger projects it works better as a kind of template where youc an pull useful handlers and services to modify

Technologies in use:

  • Gin
  • Zerolog: Each request will add a logger to the context which contains pre-populated values
  • Mabma: The options are all annotated ready to be supplied directly to mamba

Quick start

  1. Install Tonic
go get github.com/scottkgregory/mamba
  1. Call tonic.Init
router, authedRouter, logger, err := tonic.Init(cfg.Tonic)
if err != nil {
  panic(err)
}
  1. Use the Gin engine and router groups as normal
router.GET("/tonic", func(c *gin.Context) {
  // Calling GetLogger with context will populate the logger with request specific values
  logger := helpers.GetLogger(c)
  logger.Info().Msg("Cheers!")
  c.JSON(http.StatusOK, gin.H{"message": "Hello!"})
})
  1. Start the listener
// Get a pre-configured logger
logger := helpers.GetLogger()
logger.Info().Msg("Starting listener")
r.Run(fmt.Sprintf(":%d", cfg.Port))
  1. Visit the site, the homepage should show a Tonic default with a log in button. Logging in using your configured provider should insert the user details in to the provided backend and present you with a token.

What's not here?

There are a few things that aren't currently set up how I'd like and may change going forward:

  1. More configurable backends, currently defaults to storing user details in mongodb
  2. Configuration (or optional removal) of default pages