forge

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2023 License: MIT Imports: 15 Imported by: 0

README

WIP

🔥 Forge

A Go library/tool for building backend for fullstack apps.

Usage

Forge can be used either directly as a tool or as a library.

As a tool
  1. Download & unpack a Go release from releases section.
  2. Tune the forge.yml file as per your needs.
  3. Run ./forge serve -c forge.yml

If you have frontend build, use --static=./ui to serve as static files.

As a library [Goal]
package main

import (
	"github.com/gin-gonic/gin"
	"github.com/spy16/forge"
	"github.com/spy16/forge/builtins/firebase"
	"github.com/spy16/forge/core"
)

func main() {
	cli := forge.CLI("myapp",
		forge.WithAuth(&firebase.Auth{
			ProjectID: "foo",
		}),
		forge.WithPostHook(func(app core.App, conf core.ConfigLoader) error {
			r := app.Chi()
			r.Use(app.Authenticate())
			r.Get("/api/my-endpoint", func(w http.ResponseWriter, r *http.Request) {
				// Only accessible with firebase auth token
            })
			return nil
		}),
	)
	_ = cli.Execute()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CLI added in v0.1.1

func CLI(name string, forgeOpts ...Option) *cobra.Command

CLI returns a new Cobra CLI that can be used directly.

func Forge

func Forge(name string, opts ...Option) (chi.Router, error)

Forge forges a new application using given options. If 'conf' is nil, viper-based config loader will be initialised. Config file discovery will be done based on the 'name'.

Types

type Middleware added in v0.4.0

type Middleware func(http.Handler) http.Handler

type Option

type Option func(app *appForge) error

Option can be passed to Forge() to control the forging process.

func WithConfLoader added in v0.1.1

func WithConfLoader(confL core.ConfLoader) Option

WithConfLoader can be used to set a custom config loader.

func WithPostHook added in v0.1.1

func WithPostHook(hook func(app PostContext) error) Option

WithPostHook can be used to set a post-hook for Forge(). This hook will be invoked when all modules and base router is initialised. This can be used to set-up additional routes, etc.

func WithPreHook added in v0.4.0

func WithPreHook(hook func(app PreContext) error) Option

WithPreHook can be used to set a pre-hook for Forge(). This hook will be invoked when config-loader is initialized. Auth and other modules can be initialized here.

type PostContext added in v0.4.0

type PostContext interface {
	Auth() core.Auth
	Router() chi.Router
	Configs() core.ConfLoader
	Authenticate() Middleware
}

PostContext is the app state after fully initialised.

type PreContext added in v0.4.0

type PreContext interface {
	Configs() core.ConfLoader
	SetAuth(auth core.Auth)
	SetRouter(r chi.Router)
}

PreContext is the app state before fully initialised.

Directories

Path Synopsis
builtins
cmd
log

Jump to

Keyboard shortcuts

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