copper

package module
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2021 License: MIT Imports: 9 Imported by: 14

README ΒΆ

Copper logo

Go Report Card Go Doc


Copper

Copper is a Go toolkit complete with everything you need to build web apps. It focuses on developer productivity and makes building web apps in Go more fun with less boilerplate and out-of-the-box support for common needs.

πŸš€ Fullstack Toolkit

Copper provides a toolkit complete with everything you need to build web apps quickly.

πŸ“¦ One Binary

Build frontend apps along with your backend and ship everything in a single binary.

πŸ”¨ Frontend Tooling

Comes with frontend tooling built-in for vanilla JS, React, Vue and other frameworks.

πŸ“ Server-side HTML

Copper includes utilities that help build web apps with server rendered HTML pages.

πŸ’‘ Auto Restarts

Copper detects changes and automatically restarts server to save time.

πŸ— Scaffolding

Skip boilerplate and scaffold code for your packages, database queries and routes.

πŸ”‹ Batteries Included

Includes CLI, lint, dev server, frontend tooling, config management, and more!

πŸ”© First-party packages

Includes packages for authentication, pub/sub, queues, emails, and websockets.


Current Status

πŸ’ŽοΈ Copper is currently in preview as new features are added. While the APIs are unlikely to change in any major ways, some details may change as they are refined. Feedback and contributions are welcome!


Getting Started

Copper requires Go 1.16+, Node 12+


  1. Install the Copper CLI
❯ go install github.com/gocopper/cli/cmd/copper@latest
  1. Install Wire CLI
❯ go install github.com/google/wire/cmd/wire@latest
  1. Scaffold your project
❯ copper init
? What's the module name for your project? xnotes

# Create Project Files

 SUCCESS  Create xnotes/config/local.toml (Took 1ms)
 SUCCESS  Create xnotes/go.mod (Took 0s)
 SUCCESS  Create xnotes/pkg/app/handler.go (Took 0s)
 SUCCESS  Create xnotes/pkg/app/wire.go (Took 0s)
 SUCCESS  Create xnotes/pkg/web/public/favicon.svg (Took 1ms)
 SUCCESS  Create xnotes/pkg/web/public/logo.svg (Took 0s)
 SUCCESS  Create xnotes/pkg/web/src/pages/index.html (Took 0s)
 SUCCESS  Create xnotes/config/prod.toml (Took 0s)
 SUCCESS  Create xnotes/cmd/migrate/wire.go (Took 0s)
 SUCCESS  Create xnotes/pkg/app/migrations.go (Took 0s)
 SUCCESS  Create xnotes/pkg/web/wire.go (Took 0s)
 SUCCESS  Create xnotes/.golangci.yaml (Took 0s)
 SUCCESS  Create xnotes/cmd/app/main.go (Took 1ms)
 SUCCESS  Create xnotes/cmd/app/wire.go (Took 0s)
 SUCCESS  Create xnotes/cmd/migrate/main.go (Took 0s)
 SUCCESS  Create xnotes/config/dev.toml (Took 0s)
 SUCCESS  Create xnotes/pkg/web/src/layouts/main.html (Took 0s)
 SUCCESS  Create xnotes/pkg/web/src/main.js (Took 0s)
 SUCCESS  Create xnotes/pkg/web/vite.config.js (Took 0s)
 SUCCESS  Create xnotes/.gitignore (Took 0s)
 SUCCESS  Create xnotes/pkg/web/package.json (Took 0s)
 SUCCESS  Create xnotes/pkg/web/router.go (Took 0s)
 SUCCESS  Create xnotes/pkg/web/src/styles.css (Took 0s)
 SUCCESS  Create xnotes/config/base.toml (Took 0s)

# First Commands

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
| cd xnotes    |
| copper build |
| copper watch |
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  1. Start Project
❯ cd xnotes
❯ copper build
❯ copper watch
  1. Open http://localhost:5901 in your browser

  2. Watch the tour on https://gocopper.dev


License

MIT

Documentation ΒΆ

Overview ΒΆ

Package copper encapsulates everything you need to build apps quickly

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

View Source
var WireModule = wire.NewSet(wire.FieldsOf(new(*App), "Lifecycle", "Config", "Logger"))

WireModule can be used as part of google/wire setup to include the app's lifecycle, config, and logger.

Functions ΒΆ

This section is empty.

Types ΒΆ

type App ΒΆ

type App struct {
	Lifecycle *Lifecycle
	Config    cconfig.Config
	Logger    clogger.Logger
}

App defines a Copper app container that can run provided code in its managed lifecycle. It provides functionality to read config in multiple environments as defined by command-line flags.

func InitApp ΒΆ

func InitApp() (*App, error)

InitApp creates a new Copper app along with its dependencies.

func NewApp ΒΆ

func NewApp(lifecycle *Lifecycle, config cconfig.Config, logger clogger.Logger) *App

NewApp creates a new Copper app and returns it along with the app's lifecycle manager, config, and the logger.

func (*App) Run ΒΆ

func (a *App) Run(fns ...Runner)

Run runs the provided funcs. Once all of the functions complete their run, the lifecycle's stop funcs are also called. If any of the fns return an error, the app exits with an exit code 1. Run should be used when none of the fn are long-running. For long-running funcs like an HTTP server, use Start.

func (*App) Start ΒΆ

func (a *App) Start(fns ...Runner)

Start runs the provided fns and then waits on the OS's INT and TERM signals from the user to exit. Once the signal is received, the lifecycle's stop funcs are called. If any of the fns fail to run and returns an error, the app exits with exit code 1.

type Flags ΒΆ

type Flags struct {
	Env        cconfig.Env
	ConfigDir  cconfig.Dir
	ProjectDir cconfig.ProjectDir
}

Flags holds flag values passed in via command line. These can be used to configure the app environment and override the config directory.

func NewFlags ΒΆ

func NewFlags() *Flags

NewFlags reads the command line flags and returns Flags with the values set.

type Lifecycle ΒΆ

type Lifecycle struct {
	// contains filtered or unexported fields
}

Lifecycle represents the lifecycle of an app. Most importantly, it allows various parts of the app to register stop funcs that are run before the app exits. Packages such as chttp use Lifecycle to gracefully stop the HTTP server before the app exits.

func NewLifecycle ΒΆ

func NewLifecycle(logger clogger.Logger) *Lifecycle

NewLifecycle instantiates and returns a new Lifecycle that can be used with New to create a Copper app.

func (*Lifecycle) OnStop ΒΆ

func (lc *Lifecycle) OnStop(fn func(ctx context.Context) error)

OnStop registers the provided fn to run before the app exits. The fn is given a context with a deadline. Once the deadline expires, the app may exit forcefully.

func (*Lifecycle) Stop ΒΆ

func (lc *Lifecycle) Stop()

Stop runs all of the registered stop funcs in order along with a context with a configured timeout.

type Runner ΒΆ

type Runner interface {
	Run() error
}

Runner provides an interface that can be run by a Copper app using the Run or Start funcs. This interface is implemented by various packages within Copper including chttp.Server.

Directories ΒΆ

Path Synopsis
Package cconfig helps read app config easily with support for different environments
Package cconfig helps read app config easily with support for different environments
cconfigtest
Package cconfigtest provides helper methods to test the cconfig package
Package cconfigtest provides helper methods to test the cconfig package
Package cerrors provides a custom error type that can hold more context than the stdlib error package.
Package cerrors provides a custom error type that can hold more context than the stdlib error package.
Package chtml provides utilities to render HTML pages and for single-page apps
Package chtml provides utilities to render HTML pages and for single-page apps
chtmltest
Package chtmltest provides utilities to test the chtml package
Package chtmltest provides utilities to test the chtml package
Package chttp helps setup a http server with routing, middlewares, and more.
Package chttp helps setup a http server with routing, middlewares, and more.
chttptest
Package chttptest provides utility functions that are useful when testing chttp
Package chttptest provides utility functions that are useful when testing chttp
Package clogger provides a Logger interface that can be used to log messages and errors
Package clogger provides a Logger interface that can be used to log messages and errors

Jump to

Keyboard shortcuts

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