service

package
v0.0.19 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2023 License: MPL-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package service provides a service definition.

Index

Constants

View Source
const (
	InvalidCommit       = "A commit must be a valid git commit hash."
	InvalidPlatform     = "A platform must be a valid, supported platforms are: linux/amd64, linux/arm64."
	InvalidOS           = "An operating system must be a valid, supported operating systems are: linux."
	InvalidArchitecture = "An architecture must be a valid, supported architectures are: amd64, arm64."
)
View Source
const (
	DefaultName                    = "service"
	DefaultPort                    = 3000
	DefaultNetwork                 = "tcp4"
	DefaultBodyLimit               = 4 * 1024 * 1024
	DefaultConcurrency             = 256 * 1024
	DefaultReadTimeout             = 5 * time.Second
	DefaultWriteTimeout            = 5 * time.Second
	DefaultIdleTimeout             = 65 * time.Second
	DefaultShutdownTimeout         = 10 * time.Second
	DefaultReadBufferSize          = 4 * 1024
	DefaultWriteBufferSize         = 4 * 1024
	DefaultEnableTrustedProxyCheck = false
	DefaultCompressedFileSuffix    = ".gz"

	DefaultConfigDirectory = "/etc/leliuga"
	DefaultConfigFile      = "config.yaml"
)

Default values for the HTTP server

View Source
const (
	DefaultPathMonitoring = "/monitoring"
	DefaultPathDiscovery  = "/discovery"
)

Default paths for the service

View Source
const (
	DefaultServiceNamespace                        = "leliuga"
	DefaultServiceAccountName                      = "leliuga"
	DefaultServiceReplicas                         = 1
	DefaultServiceResourcesLimitCPU                = "2000m"
	DefaultServiceResourcesLimitMemory             = "1Gi"
	DefaultServiceResourcesLimitEphemeralStorage   = "1Gi"
	DefaultServiceResourcesRequestCPU              = "100m"
	DefaultServiceResourcesRequestMemory           = "32Mi"
	DefaultServiceResourcesRequestEphemeralStorage = "100Mi"
	DefaultServiceProbeInitialDelaySeconds         = 3
	DefaultServiceProbeTimeoutSeconds              = 1
	DefaultServiceProbePeriodSeconds               = 10
	DefaultServiceProbeSuccessThreshold            = 1
	DefaultServiceProbeFailureThreshold            = 3
)

Default values for the Service runtime

View Source
const (
	// DefaultImageVendor is the default image vendor for the service container
	DefaultImageVendor = "ghcr.io/leliuga"

	// DefaultImagePrefix is the default image prefix for the service container
	DefaultImagePrefix = DefaultImageVendor + "/service"

	// DefaultBaseImage is the default base image for the service container
	DefaultBaseImage = DefaultImageVendor + "/base"

	// DefaultGolangImage is the default golang image for the service container
	DefaultGolangImage = DefaultImageVendor + "/golang"
)

Default values for the image

View Source
const (
	DefaultDomain          = "leliuga.com"
	DefaultApplicationName = "Leliuga"
	DefaultVendor          = `Leliuga`
)
View Source
const (
	InvalidNamespace = "A namespace must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character."
)

Variables

View Source
var (
	CommitRegex       = regexp.MustCompile(`^[a-f0-9]{7,40}$`)
	PlatformRegex     = regexp.MustCompile(`^(linux\/(amd64|arm64))$`)
	ArchitectureRegex = regexp.MustCompile(`^(amd64|arm64)$`)
	OSRegex           = regexp.MustCompile(`^linux$`)
)
View Source
var (
	EngineNames = map[Engine]string{
		EngineKubernetes:  "Kubernetes",
		EngineDockerSwarm: "Docker Swarm",
	}
)
View Source
var (
	EnvironmentNames = map[Environment]string{
		EnvironmentDevelopment: "development",
		EnvironmentStaging:     "staging",
		EnvironmentProduction:  "production",
	}
)
View Source
var (
	NamespaceRegex = regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?$`)
)
View Source
var (
	ProviderNames = map[Provider]string{
		ProviderAws:       "Amazon Web Service",
		ProviderAzure:     "Azure",
		ProviderBareMetal: "Bare Metal",
		ProviderDo:        "Digital Ocean",
		ProviderGcp:       "Google Cloud Platform",
	}
)

Functions

This section is empty.

Types

type BuildInfo

type BuildInfo struct {
	Repository   string `json:"repository"`
	Commit       string `json:"commit"`
	When         string `json:"when"`
	GoVersion    string `json:"go_version"`
	Platform     string `json:"platform"`
	OS           string `json:"os"`
	Architecture string `json:"architecture"`
}

BuildInfo defines the build information for a Service.

func NewBuildInfo

func NewBuildInfo(repository, commit, when string) *BuildInfo

NewBuildInfo creates a new BuildInfo.

func (*BuildInfo) Validate

func (b *BuildInfo) Validate() error

Validate returns true if the BuildInfo is valid.

type Engine

type Engine uint8

Engine defines the engine for a Service runtime.

const (
	EngineInvalid Engine = iota //
	EngineKubernetes
	EngineDockerSwarm
)

func ParseEngine

func ParseEngine(value string) Engine

ParseEngine parses the Engine string.

func (Engine) MarshalJSON

func (e Engine) MarshalJSON() ([]byte, error)

MarshalJSON outputs the Engine as a json.

func (Engine) String

func (e Engine) String() string

String outputs the Engine as a string.

func (*Engine) UnmarshalJSON

func (e *Engine) UnmarshalJSON(data []byte) error

UnmarshalJSON parses the Engine from json.

func (Engine) Validate

func (e Engine) Validate() bool

Validate returns true if the Engine is valid.

type Environment

type Environment uint8

Environment defines the environment in which the Service is running.

const (
	EnvironmentInvalid Environment = iota //
	EnvironmentDevelopment
	EnvironmentStaging
	EnvironmentProduction
)

func ParseEnvironment

func ParseEnvironment(value string) Environment

ParseEnvironment parses the Environment from string.

func (Environment) MarshalJSON

func (e Environment) MarshalJSON() ([]byte, error)

MarshalJSON outputs the Environment as a json.

func (Environment) String

func (e Environment) String() string

String outputs the Environment as a string.

func (*Environment) UnmarshalJSON

func (e *Environment) UnmarshalJSON(data []byte) error

UnmarshalJSON parses the Environment from json.

func (Environment) Validate

func (e Environment) Validate() bool

Validate returns true if the Environment is valid.

type IKernel added in v0.0.3

type IKernel interface {
	// Boot the kernel.
	Boot(*Service) error

	// Shutdown the kernel.
	Shutdown(context.Context) error

	// Set an instance to the kernel.
	Set(key string, instance any)

	// Get an instance from the kernel.
	Get(key string) any

	// Has an instance from the kernel.
	Has(key string) bool

	// Instances returns all instances from the kernel.
	Instances() types.Map[any]
}

IKernel represents the service kernel interface.

type Kernel added in v0.0.3

type Kernel struct {
	IKernel
	// contains filtered or unexported fields
}

Kernel represents the service kernel.

func NewKernel added in v0.0.3

func NewKernel() *Kernel

NewKernel returns a new kernel.

func (*Kernel) Boot added in v0.0.3

func (k *Kernel) Boot(*Service) error

Boot the kernel.

func (*Kernel) Get added in v0.0.3

func (k *Kernel) Get(key string) any

Get an instance from the kernel.

func (*Kernel) Has added in v0.0.3

func (k *Kernel) Has(key string) bool

Has an instance from the kernel.

func (*Kernel) Instances added in v0.0.3

func (k *Kernel) Instances() types.Map[any]

Instances returns all instances from the kernel.

func (*Kernel) Set added in v0.0.4

func (k *Kernel) Set(key string, instance any)

Set an instance to the kernel.

func (*Kernel) Shutdown added in v0.0.3

func (k *Kernel) Shutdown(context.Context) error

Shutdown the kernel.

type Option

type Option func(o *Options)

Option represents the service option.

func WithBodyLimit

func WithBodyLimit(value int) Option

WithBodyLimit sets the body limit for the service.

func WithBuildInfo

func WithBuildInfo(repository, commit, when string) Option

WithBuildInfo sets the build info for the service.

func WithCertificateFile

func WithCertificateFile(value string) Option

WithCertificateFile sets the certificate file for the service.

func WithCertificateKeyFile

func WithCertificateKeyFile(value string) Option

WithCertificateKeyFile sets the certificate key file for the service.

func WithConcurrency

func WithConcurrency(value int) Option

WithConcurrency sets the concurrency for the service.

func WithDatabase

func WithDatabase(value *database.Options) Option

WithDatabase sets the database for the service.

func WithDescription added in v0.0.9

func WithDescription(value string) Option

WithDescription sets the description for the service.

func WithDisableStartupMessage added in v0.0.3

func WithDisableStartupMessage(value bool) Option

WithDisableStartupMessage sets the disable startup message for the service.

func WithDomain

func WithDomain(value string) Option

WithDomain sets the domain for the service.

func WithEnablePrintRoutes

func WithEnablePrintRoutes(value bool) Option

WithEnablePrintRoutes sets the enable print routes for the service.

func WithEnableTrustedProxyCheck

func WithEnableTrustedProxyCheck(value bool) Option

WithEnableTrustedProxyCheck sets the enable trusted proxy check for the service.

func WithErrorHandler

func WithErrorHandler(value func(*fiber.Ctx, error) error) Option

WithErrorHandler sets the error handler for the service.

func WithIdleTimeout

func WithIdleTimeout(value time.Duration) Option

WithIdleTimeout sets the idle timeout for the service.

func WithKernel added in v0.0.3

func WithKernel(value IKernel) Option

WithKernel sets the kernel for the service.

func WithName

func WithName(value string) Option

WithName sets the name for the service.

func WithNetwork

func WithNetwork(value string) Option

WithNetwork sets the network for the service.

func WithPort

func WithPort(value int32) Option

WithPort sets the port for the service.

func WithReadBufferSize

func WithReadBufferSize(value int) Option

WithReadBufferSize sets the read buffer size for the service.

func WithReadTimeout

func WithReadTimeout(value time.Duration) Option

WithReadTimeout sets the read timeout for the service.

func WithRuntime

func WithRuntime(value *Runtime) Option

WithRuntime sets the runtime for the service.

func WithShutdownTimeout

func WithShutdownTimeout(value time.Duration) Option

WithShutdownTimeout sets the shutdown timeout for the service.

func WithTrustedProxies

func WithTrustedProxies(values []string) Option

WithTrustedProxies sets the trusted proxies for the service.

func WithViews

func WithViews(value fiber.Views) Option

WithViews sets the views for the service.

func WithWriteBufferSize

func WithWriteBufferSize(value int) Option

WithWriteBufferSize sets the write buffer size for the service.

func WithWriteTimeout

func WithWriteTimeout(value time.Duration) Option

WithWriteTimeout sets the write timeout for the service.

type Options

type Options struct {
	Name                    string                        `json:"name"`
	Description             string                        `json:"description"`
	Port                    int32                         `json:"port"                       env:"PORT"`
	Network                 string                        `json:"network"`
	Domain                  string                        `json:"domain"                     env:"DOMAIN"`
	CertificateFile         string                        `json:"certificate_file"           env:"CERTIFICATE_FILE"`
	CertificateKeyFile      string                        `json:"certificate_key_file"       env:"CERTIFICATE_KEY_FILE"`
	Views                   fiber.Views                   `json:"-"`
	BodyLimit               int                           `json:"body_limit"                 env:"BODY_LIMIT"`
	Concurrency             int                           `json:"concurrency"                env:"CONCURRENCY"`
	ReadTimeout             time.Duration                 `json:"read_timeout"               env:"READ_TIMEOUT"`
	WriteTimeout            time.Duration                 `json:"write_timeout"              env:"WRITE_TIMEOUT"`
	IdleTimeout             time.Duration                 `json:"idle_timeout"               env:"IDLE_TIMEOUT"`
	ShutdownTimeout         time.Duration                 `json:"shutdown_timeout"           env:"SHUTDOWN_TIMEOUT"`
	ReadBufferSize          int                           `json:"read_buffer_size"           env:"READ_BUFFER_SIZE"`
	WriteBufferSize         int                           `json:"write_buffer_size"          env:"WRITE_BUFFER_SIZE"`
	EnableTrustedProxyCheck bool                          `json:"enable_trusted_proxy_check" env:"ENABLE_TRUSTED_PROXY_CHECK"`
	TrustedProxies          []string                      `json:"trusted_proxies"            env:"TRUSTED_PROXIES"`
	DisableStartupMessage   bool                          `json:"disable_startup_message"    env:"DISABLE_STARTUP_MESSAGE"`
	EnablePrintRoutes       bool                          `json:"enable_print_routes"        env:"ENABLE_PRINT_ROUTES"`
	BuildInfo               *BuildInfo                    `json:"build_info"`
	Runtime                 *Runtime                      `json:"runtime"                    env:"RUNTIME"`
	Database                *database.Options             `json:"database"                   env:"DATABASE"`
	ErrorHandler            func(*fiber.Ctx, error) error `json:"-"`
	Kernel                  IKernel                       `json:"-"`
}

Options represents the service options.

func NewOptions

func NewOptions(options ...Option) *Options

NewOptions creates a new options.

func NewOptionsFromConfig added in v0.0.6

func NewOptionsFromConfig(cfgName string, options ...Option) (*Options, error)

NewOptionsFromConfig creates a new options from config.

type Provider

type Provider uint8

Provider defines the cloud provider for a Service runtime.

const (
	ProviderInvalid Provider = iota //
	ProviderAws
	ProviderAzure
	ProviderBareMetal
	ProviderDo
	ProviderGcp
)

func ParseProvider

func ParseProvider(value string) Provider

ParseProvider parses the Provider from string.

func (Provider) MarshalJSON

func (p Provider) MarshalJSON() ([]byte, error)

MarshalJSON outputs the Provider as a json.

func (Provider) String

func (p Provider) String() string

String outputs the Provider as a string.

func (*Provider) UnmarshalJSON

func (p *Provider) UnmarshalJSON(data []byte) error

UnmarshalJSON parses the Provider from json.

func (Provider) Validate

func (p Provider) Validate() bool

Validate returns true if the Provider is valid.

type ResourceRequirements

type ResourceRequirements struct {
	Limits   corev1.ResourceList `json:"limits"   env:"LIMITS"`
	Requests corev1.ResourceList `json:"requests" env:"REQUESTS"`
}

ResourceRequirements defines the resource requirements for a Service.

type Runtime

type Runtime struct {
	Provider           Provider              `json:"provider"             env:"PROVIDER"`
	Region             string                `json:"region"               env:"REGION"`
	Zone               string                `json:"zone"                 env:"ZONE"`
	Namespace          string                `json:"namespace"            env:"NAMESPACE"`
	ServiceAccountName string                `json:"service_account_name" env:"SERVICE_ACCOUNT_NAME"`
	Engine             Engine                `json:"engine"               env:"ENGINE"`
	Replicas           int32                 `json:"replicas"             env:"REPLICAS"`
	Resources          *ResourceRequirements `json:"resources"            env:"RESOURCES"`
	Probe              *RuntimeProbe         `json:"probe"                env:"PROBE"`
}

Runtime defines the runtime for a Service.

func NewRuntime

func NewRuntime() *Runtime

NewRuntime creates a new Runtime.

func (*Runtime) ToResourceRequirements

func (r *Runtime) ToResourceRequirements() corev1.ResourceRequirements

ToResourceRequirements converts the Runtime to a ResourceRequirements.

func (*Runtime) Validate

func (r *Runtime) Validate() error

Validate makes Runtime validatable by implementing validation.Validatable interface.

type RuntimeProbe

type RuntimeProbe struct {
	InitialDelaySeconds int32 `json:"initial_delay_seconds" env:"INITIAL_DELAY_SECONDS"`
	TimeoutSeconds      int32 `json:"timeout_seconds"       env:"TIMEOUT_SECONDS"`
	PeriodSeconds       int32 `json:"period_seconds"        env:"PERIOD_SECONDS"`
	SuccessThreshold    int32 `json:"success_threshold"     env:"SUCCESS_THRESHOLD"`
	FailureThreshold    int32 `json:"failure_threshold"     env:"FAILURE_THRESHOLD"`
}

RuntimeProbe defines the runtime probe for a Service.

type Service

type Service struct {
	*Options
	*fiber.App
}

Service represents the service.

func NewService

func NewService(options *Options) *Service

NewService creates a new service.

func (*Service) Serve

func (s *Service) Serve() error

Serve the service

Directories

Path Synopsis
Package cmd provides the service cli commands.
Package cmd provides the service cli commands.
middleware

Jump to

Keyboard shortcuts

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