github.com/ipfans/trojan-go@v0.11.0/api/api.go (about)

     1  package api
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/ipfans/trojan-go/log"
     7  	"github.com/ipfans/trojan-go/statistic"
     8  )
     9  
    10  type Handler func(ctx context.Context, auth statistic.Authenticator) error
    11  
    12  var handlers = make(map[string]Handler)
    13  
    14  func RegisterHandler(name string, handler Handler) {
    15  	handlers[name] = handler
    16  }
    17  
    18  func RunService(ctx context.Context, name string, auth statistic.Authenticator) error {
    19  	if h, ok := handlers[name]; ok {
    20  		log.Debug("api handler found", name)
    21  		return h(ctx, auth)
    22  	}
    23  	log.Debug("api handler not found", name)
    24  	return nil
    25  }