server

package module
v2.0.0-rc9 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2025 License: 0BSD Imports: 11 Imported by: 0

README

server

Use any http.Handler implementation of your choice. Register the handler for a given path using the server.RegisterHandler function.

Check out these premade handlers to get you going!

Running and Stopping

To stop the server send the program an SIGINT or SIGKILL - either natively through another process or with a Ctrl-C.

Stopping the server is not "graceful" - it does not await any open connections and will likely attempt to close down IO/handles without consideration of consumers waiting their results.

HTTP/2 and Certs

Deploy production-grade cert files (backed by a trusted CA) to the same machine as the server binary.

Load them in and pass them to the server.Run function.

NOTE Still in process of testing.

Consuming

This is a very high-level server library. Using it requires the import and use of a single Run function:

import (
  "context"
  "crypto/tls"
  "os"

  "git.sonicoriginal.software/server/v2"
)

const (
	portEnvKey = "APP_PORT"
  enableHeartBeat = true
)

var (
	certs               []tls.Certificate
	mux                 = http.NewServeMux()
	ctx, cancelFunction = context.WithCancel(context.Background())
)

func main() {
	defer cancelFunction()

  // TODO Import your desired handlers and register them here
  // e.g. if importing the 'app' handler, use
  // _ = app.New()

  // TODO Load your cert and key or skip and just use
  // cert, err := tls.X509KeyPair(cert, key)
  // if err != nil {
  //   // Handle a certificate server failure for your app here
  // }

	address, serverErrorChannel := server.Run(ctx, &certs, mux, portEnvKey)
	logger.DefaultLogger.Info("Serving on [%v]\n", address)

	serverError := <-serverErrorChannel
	contextError := serverError.Context.Error()

	if serverError.Close != nil {
		logger.DefaultLogger.Error("Error closing server: %v", serverError.Close.Error())
	}

	if contextError != server.ErrContextCancelled.Error() {
		logger.DefaultLogger.Error("Server failed unexpectedly: %v", contextError)
	}
}

Documentation

Index

Constants

View Source
const (
	// LocalHost is the name of the localhost
	LocalHost = "localhost"
	// DefaultPort is the default port used for service
	DefaultPort = "4430"
	// HeartBeatName is the name of the heartbeat service
	HeartBeatName = "heartbeat"
	// ServerContextCancelled denotes when a server run returns because its context is cancelled
	ServerContextCancelled = "Server context cancelled"
	// ServerReceivedInterrupt denotes when a server run returns because its context is cancelled
	ServerReceivedInterrupt = "Server received interrupt signal"
)

Variables

View Source
var (

	// ErrContextCancelled - a server returns because its context is cancelled
	ErrContextCancelled = fmt.Errorf(ServerContextCancelled)
	// ErrReceivedInterrupt - a server returns because it received an interrupt signal
	ErrReceivedInterrupt = fmt.Errorf(ServerReceivedInterrupt)
)

Functions

func RegisterHeartBeat

func RegisterHeartBeat(mux *http.ServeMux)

RegisterHeartBeat handler

func Run

func Run(
	ctx context.Context,
	certs *[]tls.Certificate,
	mux *http.ServeMux,
	portEnvKey string,
	enableHeartBeat bool,
) (address string, reportedError chan Error)

Run executes the main server loop in a goroutine

It allows consumer cancellation through the context and server-side cancellation notification via the returned `reportedError` channel

Fatal errors will be sent to the returned channel and the server will shutdown

Types

type Error

type Error struct {
	Context error
	Close   error
}

Error contains the errors applicable from running and stopping a server

Jump to

Keyboard shortcuts

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