service

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2024 License: MIT Imports: 9 Imported by: 1

README

ACSL-GO Service Framework

See API reference Here.

Introduction

ACSL-GO Service Framework is a tiny framework for services. It focuses on the following aspects:

  • Sub-service scheduling, in a common scenario, we may need to run an HTTP server and schedule some timer tasks. ACSL-GO Service Framework provides a simple way to do this.
  • Graceful-shutdown HTTP server, a simple implementation of graceful-shutdown HTTP server as a sub-service (task).
  • Timer task, a simple implementation of timer task as a sub-service (task).

Usage

Integration
import (
    "github.com/acsl-go/service"
)
Create an HTTP server
var httpHandler http.Handler
httpServer := service.HttpServer("http-server", ":8080", httpHandler)

Or, if you work with Gin:

router := gin.New()
httpServer := service.HttpServer("http-server", ":8080", router)
Create a timer task
timerTask := service.Timer(5 * time.Second, func() {
    // do something
})
Run the HTTP server and timer task
service.Run(httpServer, timerTask)
Sample code
package main

import (
    "fmt"
    "time"
	"github.com/acsl-go/service"
	"github.com/gin-gonic/gin"
)

func apiInit(router *gin.Engine) {
    router.GET("/ping", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "pong",
        })
    })
}

func ginTask(name, addr string, initializer func(*gin.Engine)) service.ServiceTask {
	router := gin.New()
	initializer(router)
	return service.HttpServer(name, addr, router)
}

func main() {
	service.Run(
		ginTask("API", ":8080", apiInit),
		service.Timer(5*time.Second, func() {
            fmt.Println("timer task")
        }),
	)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(tasks ...ServiceTask)

Types

type ServiceTask

type ServiceTask func(*sync.WaitGroup, chan os.Signal)

func HttpServer

func HttpServer(name, addr string, router http.Handler) ServiceTask

func Timer

func Timer(interval time.Duration, task func()) ServiceTask

Jump to

Keyboard shortcuts

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