gobaboon

package module
v1.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2023 License: MIT Imports: 13 Imported by: 3

README ΒΆ

Gobaboon

Baboon aims to kickstart the development of go web and api applications by laying out a project structure and doing the scafolding for tasks that could be seen as repetitive in various projects.

At the moment of writing I am using Baboon for a (real-world) REST API.

The Baboon project evolved from https://github.com/martijnkorbee/goracoon which is created following a golang course. Compared to Racoon, the project structure and inner workings had a major overhaul, inspired by: https://github.com/golang-standards/project-layout.

🟠 Gopher by: https://github.com/MariaLetta/free-gophers-pack

Tech-stack

🐍 CLI-tools with: https://github.com/spf13/cobra
πŸ•ΈοΈ Routing with: https://github.com/go-chi/chi
πŸ“° Logging with: https://github.com/rs/zerolog and https://github.com/natefinch/lumberjack
🏠 Databases with: https://github.com/upper/db
πŸ’‘ Sessions with: https://github.com/alexedwards/scs
πŸ›‘ CSRF protection with: https://github.com/justinas/nosurf

Supported databases

  • Postgresql
  • Mysql/MariaDB
  • SQLite3
  • SQL Server -> (not supported by bobo-cli)

Supported caching

  • Redis
  • BadgerDB

GOTO

Bobo-cli

Refer to the full documentation: bobo-cli docs

Getting started

Clone the repo.

git clone https://github.com/martijnkorbee/gobaboon

CD to the repo directory.

cd gobaboon

Build will compile the cli in ./cmd/cli/bobo/bin.
After you can cp or mv the bin to your required bin directory.

make build_cli 

Install will build the file to ${HOME}/bin (the directory should exist and exported to your $PATH.

make install_cli

Create a new skeleton app in the current directory.

bobo make new -n <appname>

Project structure

This is the project structure of a fresh baboon kickstarted project.

β”œβ”€β”€ cmd
β”‚Β Β  └── web
β”‚Β Β      β”œβ”€β”€ app
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ app.go
β”‚Β Β      β”‚Β Β  └── init-app.go
β”‚Β Β      β”œβ”€β”€ bin
β”‚Β Β      β”‚Β Β  └── testapp
β”‚Β Β      └── main.go
β”œβ”€β”€ database
β”‚Β Β  └── models
β”‚Β Β      └── models.go
β”œβ”€β”€ go.mod
β”œβ”€β”€ go.sum
β”œβ”€β”€ http
β”‚Β Β  β”œβ”€β”€ handlers
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ handlers-api.go
β”‚Β Β  β”‚Β Β  └── handlers.go
β”‚Β Β  β”œβ”€β”€ middleware
β”‚Β Β  β”‚Β Β  └── middleware.go
β”‚Β Β  └── routes
β”‚Β Β      β”œβ”€β”€ routes-api.go
β”‚Β Β      └── routes.go
β”œβ”€β”€ LICENSE.md
β”œβ”€β”€ Makefile
β”œβ”€β”€ public
β”‚Β Β  └── static
β”‚Β Β      β”œβ”€β”€ html
β”‚Β Β      β”‚Β Β  └── maintenance.html
β”‚Β Β      └── images
β”‚Β Β          └── gobaboon.jpg
β”œβ”€β”€ README.md
└── templates
    β”œβ”€β”€ mail
    β”‚Β Β  β”œβ”€β”€ mail.html.tmpl
    β”‚Β Β  └── mail.plain.tmpl
    └── views
        β”œβ”€β”€ home.jet
        β”œβ”€β”€ home.page.tmpl
        └── layouts
            └── base.jet

Authentication

Make sure your database is available (setup the .env), and in your project root directory execute the bobo command. This will create migration files, runs the migrations and adds the user and token models and middleware to your project.

bobo make auth

Note: you still have to add the models in your models type and activate the middleware on your desired routes.

Sessions

Make sure your cache/database is available (setup the .env). For cache store no extra steps are required.
For SQL DB; in your project root directory execute the bobo command. This will create migration files and runs the migrations.

bobo make session

Note: Set persistent session store by configuring the correct session type in the .env file.

Middleware

Add your middleware to http/middleware.

  • CSRF protection is automatically enforced by the baboon server, exempts all API routes.
  • CORS is automatically enabled by the baboon server on API routes.

Routes and Handlers

Add your routes and handlers to their respective folders in the http directory.

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

This section is empty.

Types ΒΆ

type Baboon ΒΆ

type Baboon struct {
	// Config holds all baboon required config settings
	Config Config

	// Log is the default logger for baboon
	// Applications using baboon should assign their own loggers as in the skeleton app.
	Log *logger.Logger

	// Scheduler can be used to schedule tasks (like cron jobs)
	Scheduler *cron.Cron

	// Server is the baboon app server.
	Server *server.Server

	// RPCServer is baboon's RPC server
	RPCServer *rpc.RPCServer

	// Mailer is the baboon app mailer
	Mailer *mail.Mailer

	// Database holds baboon's main database
	Database *db.Database

	// Cache is baboon's cache client
	Cache cache.Cache
}

func (*Baboon) Init ΒΆ

func (b *Baboon) Init(c Config) error

New creates a new baboon app

func (*Baboon) Run ΒΆ

func (b *Baboon) Run() error

type Config ΒΆ

type Config struct {
	AppName string

	// Rootpath is the rootpath of the application. Usually this the full path to the bin file.
	Rootpath string

	// Debug is used to set some functionality in debug mode. Mostly being more explicit in log info and errors.
	Debug bool

	// Host will be used by the server.
	Host string

	// Port will be used by the server.
	Port string

	// RPC port if set baboon also starts an rpc listener
	RPCport string

	// EncryptionKey is used to encrypt and decrypt with their respective functions.
	EncryptionKey string

	// Renderer sets which type of template engine will be used.
	Renderer string

	// SessionType sets which type of session store to use i.e. cookie, cache, db.
	SessionType string

	// CacheType sets which cache client to use
	CacheType string

	// CachePrefix sets the cache prefix for the server
	CachePrefix string

	// RedisConfig holds redis client configuration
	Redis cache.RedisConfig

	// Cookie holds cookie configuration
	Cookie CookieConfig

	// DatabaseConfig holds the database configuration.
	DatabaseConfig db.DatabaseConfig

	// MailerService sets the mailer service
	MailerService string

	// MailerSettings holds the mailer settings
	MailerSettings mail.MailerSettings
}

Config holds all configuration settings to be used throughout baboon.

type CookieConfig ΒΆ

type CookieConfig struct {
	// Name defaults to baboon
	Name string

	// Domain defaults to localhost
	Domain string

	// Lifetime defaults to 1440 minutes
	LifeTime int // time in minutes

	// Secure defaults to false
	Secure bool

	// Persist defaults to false
	Persist bool

	// SameSite defaults to SameSiteStrict mode
	SameSite http.SameSite
}

Directories ΒΆ

Path Synopsis
cmd
cli/bobo
Bobo is a CLI tool used with the gobaboon app framework, check the README.md for more info.
Bobo is a CLI tool used with the gobaboon app framework, check the README.md for more info.
internal
pkg/render
Render package renders HTML templates found in the rootpath/views folder of the project.
Render package renders HTML templates found in the rootpath/views folder of the project.
pkg/server
Server package is responsible for creating the server for the baboon application.
Server package is responsible for creating the server for the baboon application.
pkg
db
Package db facades the creation of different db connections and adapter imports in order to return a generalised db type.
Package db facades the creation of different db connections and adapter imports in order to return a generalised db type.
logger
Logger package abstracts the creation of service oriented loggers with log rotating build in.
Logger package abstracts the creation of service oriented loggers with log rotating build in.
mail
Package mail contains a simple mailer that connects to your mail service and sends emails through the jobs channel or direct methods calls.
Package mail contains a simple mailer that connects to your mail service and sends emails through the jobs channel or direct methods calls.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL