service

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package service provides primitives, structures and functions for building HTTP services. For more information look at https://github.com/ampho-cms/backend/wiki/Service.

Example (Extend)

This example shows how to extend a base service.

// Author:  Alexander Shepetko
// Email:   a@shepetko.com
// License: MIT

package main

import (
	"log"

	"ampho.xyz/core/config"
	"ampho.xyz/core/service"
)

type AwesomeService struct {
	service.Service
}

// This example shows how to extend a base service.
func main() {
	const svName = "hello"

	// Config
	cfg, err := config.New(svName, "yaml", config.DefaultSearchPaths(svName)...)
	if err != nil {
		log.Fatalf("failed to init config: %v", err)
	}

	// Create a base service instance
	base, err := service.New(cfg)
	if err != nil {
		log.Fatal(err)
	}

	// Embed base service
	svc := AwesomeService{base}

	// Base setup code here
	// ...

	// Run the service until SIGINT
	svc.Run()
}
Output:

Example (NewDefault)

This example shows how to instantiate and run a service using default configuration.

package main

import (
	"log"

	"ampho.xyz/core/config"
	"ampho.xyz/core/service"
)

func main() {
	const svName = "hello"

	// Config
	cfg, err := config.New(svName, "yaml", config.DefaultSearchPaths(svName)...)
	if err != nil {
		log.Fatalf("failed to init config: %v", err)
	}

	// Create a service instance
	svc, err := service.New(cfg)
	if err != nil {
		log.Fatal(err)
	}

	// Service setup code here
	// ...

	// Run the service until SIGINT
	svc.Run()
}
Output:

Example (NewTesting)

This example shows how to instantiate and use a service using testing configuration.

package main

import ()

func main() {
	// TODO
}
Output:

Index

Examples

Constants

View Source
const (
	DftShutdownTimeout = time.Second * 15 // service shutdown timeout

	DftAddress      = "127.0.0.1:8765" // HTTP server address
	DftReadTimeout  = time.Second * 15 // network read timeout
	DftWriteTimeout = time.Second * 15 // network write timeout
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Base

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

Base is the base service structure.

func New

func New(cfg config.Config) (*Base, error)

New creates a new base service instance using default configuration.

func (*Base) AfterStop

func (s *Base) AfterStop(fn func(svc Service))

AfterStop schedules a function to be called after service stop.

func (*Base) BeforeStart

func (s *Base) BeforeStart(fn func(svc Service))

BeforeStart schedules a function to be called before service start.

func (*Base) Config

func (s *Base) Config() config.Config

Config returns configuration.

func (*Base) Router

func (s *Base) Router() *mux.Router

Router returns router.

func (*Base) Run

func (s *Base) Run()

Run starts the service and blocks until SIGINT received. Usually it should be a last call in the `main()`.

func (*Base) Server

func (s *Base) Server() *http.Server

Server returns HTTP server.

func (*Base) Start

func (s *Base) Start()

Start starts the service. Assumed to be called in a goroutine.

func (*Base) Stop

func (s *Base) Stop()

Stop stops the service.

type Service

type Service interface {
	// Config returns configuration.
	Config() config.Config

	// Router returns router.
	Router() *mux.Router

	// Server returns HTTP server.
	Server() *http.Server

	// BeforeStart schedules a function to be called before service start.
	BeforeStart(fn func(svc Service))

	// AfterStop schedules a function to be called after service stop.
	AfterStop(fn func(svc Service))

	// Start starts the service. Assumed to be called in a goroutine.
	Start()

	// Stop stops the service.
	Stop()

	// Run starts the service and blocks until SIGINT received. Usually it should be a last call in the `main()`.
	Run()
}

Service is the service interface.

Jump to

Keyboard shortcuts

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