mirror of
https://github.com/nadoo/glider.git
synced 2025-02-22 17:05:42 +08:00
30 lines
659 B
Go
30 lines
659 B
Go
package service
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
)
|
|
|
|
var creators = make(map[string]Creator)
|
|
|
|
// Service is a server that can be run.
|
|
type Service interface{ Run() }
|
|
|
|
// Creator is a function to create services.
|
|
type Creator func(args ...string) (Service, error)
|
|
|
|
// Register is used to register a service.
|
|
func Register(name string, c Creator) {
|
|
creators[strings.ToLower(name)] = c
|
|
}
|
|
|
|
// New calls the registered creator to create services.
|
|
func New(s string) (Service, error) {
|
|
args := strings.Split(s, ",")
|
|
c, ok := creators[strings.ToLower(args[0])]
|
|
if ok {
|
|
return c(args[1:]...)
|
|
}
|
|
return nil, errors.New("unknown service name: '" + args[0] + "'")
|
|
}
|