app

package
v1.1.30 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2024 License: MIT Imports: 7 Imported by: 0

README

app

Start and stop services gracefully, using errgroup to ensure that multiple services are started properly at the same time.


Example of use

import "github.com/18721889353/sunshine/pkg/app"

func main() {
    initApp()
    servers := registerServers()
    closes := registerCloses(servers)

    a := app.New(servers, closes)
    a.Run()
}

func initApp() {
    // get configuration

    // initializing log

    // initializing database

    // ......
}

func registerServers() []app.IServer {
    var servers []app.IServer

    // create a http service
    servers = append(servers, server.NewHTTPServer(

    ))

    // create a grpc service
    servers = append(servers, server.NewGRPCServer(

    ))

    // ......

    return servers
}

func registerCloses(servers []app.IServer) []app.Close {
    var closes []app.Close

    // close server
    for _, server := range servers {
        closes = append(closes, server.Stop)
    }

    // close other resource
    closes = append(closes, func() error {

    })

    // ......

    return closes
}

Documentation

Overview

Package app 负责优雅地启动和停止服务,使用 golang.org/x/sync/errgroup 确保多个服务同时启动。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

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

App 管理一组服务和关闭函数。

func New

func New(servers []IServer, closes []Close) *App

New 创建一个新的 App 实例,传入服务列表和关闭函数列表。

func (*App) Run

func (a *App) Run()

Run 启动所有服务,并监控信号以停止应用。

type Close

type Close func() error

Close 定义了一个关闭资源的函数类型。

type IServer

type IServer interface {
	Start() error   // 启动服务
	Stop() error    // 停止服务
	String() string // 返回服务的字符串表示
}

IServer 定义了服务接口。

Jump to

Keyboard shortcuts

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