run

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2024 License: MIT Imports: 4 Imported by: 7

README

run

run is a tiny module which makes spinning up and tearing down for a server program written in Go be more easier. See an example below for how to use.

import (
	"context"

	"github.com/110y/run"
)

func main() {
	run.Run(func(ctx context.Context) int {
		// Spin up your server here.

		// This blocks until one of termination signals (unix.SIGHUP, unix.SIGINT, unix.SIGTERM or unix.SIGQUIT) will be passed.
		<-ctx.Done()

		// Tear down your server here.

		// After this function has finished, run.Run exits the process with returned value of this function as its exit code.
		return 0
	})
}

See https://pkg.go.dev/github.com/110y/run for more details.

Documentation

Overview

run is a tiny module which makes spinning up and tearing down for a server program written in Go be more easier. See an example below for how to use.

Example
package main

import (
	"context"

	"github.com/110y/run"
)

func main() {
	run.Run(func(ctx context.Context) int {
		// Spin up your server here.

		// This blocks until one of termination signals (unix.SIGHUP, unix.SIGINT, unix.SIGTERM or unix.SIGQUIT) will be passed.
		<-ctx.Done()

		// Tear down your server here.

		// After this function has finished, run.Run exits the process with returned value of this function as its exit code.
		return 0
	})
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(f func(context.Context) int, opts ...Option)

Run runs given f with a context created by context.Background and signal.NotifyContext. After f has finished, then Run exits the process by calling os.Exit with returned value of f as its exit code.

By default, signals will be passed to the signal.NotifyContext are unix.SIGHUP, unix.SIGINT, unix.SIGTERM and unix.SIGQUIT. You can change which signals will Run wait for by passing an Option created by WithSignals.

Types

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option controls how Run behaves.

func WithSignals

func WithSignals(signals ...os.Signal) Option

WithSignals returns an Option which specifies which signals will Run wait for with signal.NotifyContext.

Jump to

Keyboard shortcuts

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