Skip to content

Commit

Permalink
service_handler を整理する
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexa committed Mar 7, 2024
1 parent 4e1401b commit 9fd3e17
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 22 deletions.
4 changes: 2 additions & 2 deletions amazon_transcribe_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func init() {
NewServiceHandlerFuncs.register("aws", NewAmazonTranscribeHandler)
ServiceHandlers.Register("aws", new(AmazonTranscribeHandler))
}

type AmazonTranscribeHandler struct {
Expand All @@ -29,7 +29,7 @@ type AmazonTranscribeHandler struct {
OnResultFunc func(context.Context, io.WriteCloser, string, string, string, any) error
}

func NewAmazonTranscribeHandler(config Config, channelID, connectionID string, sampleRate uint32, channelCount uint16, languageCode string, onResultFunc any) serviceHandlerInterface {
func (h *AmazonTranscribeHandler) New(config Config, channelID, connectionID string, sampleRate uint32, channelCount uint16, languageCode string, onResultFunc any) serviceHandlerInterface {
return &AmazonTranscribeHandler{
Config: config,
ChannelID: channelID,
Expand Down
2 changes: 1 addition & 1 deletion cmd/suzu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {

// bin/suzu -C config.ini
configFilePath := flag.String("C", "./config.ini", "設定ファイルへのパス")
serviceType := flag.String("service", "aws", fmt.Sprintf("音声文字変換のサービス(%s)", strings.Join(suzu.NewServiceHandlerFuncs.GetNames([]string{"test", "dump"}), ", ")))
serviceType := flag.String("service", "aws", fmt.Sprintf("音声文字変換のサービス(%s)", strings.Join(suzu.ServiceHandlers.GetNames([]string{"test", "dump"}), ", ")))
flag.Parse()

if *showVersion {
Expand Down
4 changes: 2 additions & 2 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ func NewSuzuErrorResponse(err error) TranscriptionResult {
}

func getServiceHandler(serviceType string, config Config, channelID, connectionID string, sampleRate uint32, channelCount uint16, languageCode string, onResultFunc any) (serviceHandlerInterface, error) {
newHandlerFunc, err := NewServiceHandlerFuncs.get(serviceType)
handler, err := ServiceHandlers.Get(serviceType)
if err != nil {
return nil, err
}

return (*newHandlerFunc)(config, channelID, connectionID, sampleRate, channelCount, languageCode, onResultFunc), nil
return handler.New(config, channelID, connectionID, sampleRate, channelCount, languageCode, onResultFunc), nil
}

// https://echo.labstack.com/cookbook/streaming-response/
Expand Down
4 changes: 2 additions & 2 deletions packet_dump_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func init() {
NewServiceHandlerFuncs.register("dump", NewPacketDumpHandler)
ServiceHandlers.Register("dump", new(PacketDumpHandler))
}

type PacketDumpHandler struct {
Expand All @@ -27,7 +27,7 @@ type PacketDumpHandler struct {
OnResultFunc func(context.Context, io.WriteCloser, string, string, string, any) error
}

func NewPacketDumpHandler(config Config, channelID, connectionID string, sampleRate uint32, channelCount uint16, languageCode string, onResultFunc any) serviceHandlerInterface {
func (h *PacketDumpHandler) New(config Config, channelID, connectionID string, sampleRate uint32, channelCount uint16, languageCode string, onResultFunc any) serviceHandlerInterface {
return &PacketDumpHandler{
Config: config,
ChannelID: channelID,
Expand Down
30 changes: 19 additions & 11 deletions service_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

var (
NewServiceHandlerFuncs = make(newServiceHandlerFuncs)
ServiceHandlers = NewServiceHandlers()

ErrServiceNotFound = fmt.Errorf("SERVICE-NOT-FOUND")
)
Expand All @@ -19,27 +19,35 @@ type serviceHandlerInterface interface {
UpdateRetryCount() int
GetRetryCount() int
ResetRetryCount() int
New(Config, string, string, uint32, uint16, string, any) serviceHandlerInterface
}

type newServiceHandlerFunc func(Config, string, string, uint32, uint16, string, any) serviceHandlerInterface
type serviceHandlers struct {
Handlers map[string]serviceHandlerInterface
}

type newServiceHandlerFuncs map[string]newServiceHandlerFunc
func NewServiceHandlers() serviceHandlers {
return serviceHandlers{
Handlers: make(map[string]serviceHandlerInterface),
}
}

func (sh *newServiceHandlerFuncs) register(name string, f newServiceHandlerFunc) {
(*sh)[name] = f
func (h *serviceHandlers) Register(name string, f serviceHandlerInterface) {
h.Handlers[name] = f
}

func (sh *newServiceHandlerFuncs) get(name string) (*newServiceHandlerFunc, error) {
h, ok := (*sh)[name]
func (h *serviceHandlers) Get(name string) (serviceHandlerInterface, error) {
handler, ok := h.Handlers[name]
if !ok {
return nil, ErrServiceNotFound
}
return &h, nil
return handler, nil
}

func (sh *newServiceHandlerFuncs) GetNames(exclude []string) []string {
names := make([]string, 0, len(*sh))
for name := range *sh {
func (h *serviceHandlers) GetNames(exclude []string) []string {
handlers := h.Handlers
names := make([]string, 0, len(handlers))
for name := range handlers {
if slices.Contains(exclude, name) {
continue
}
Expand Down
4 changes: 2 additions & 2 deletions speech_to_text_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func init() {
NewServiceHandlerFuncs.register("gcp", NewSpeechToTextHandler)
ServiceHandlers.Register("gcp", new(SpeechToTextHandler))
}

type SpeechToTextHandler struct {
Expand All @@ -32,7 +32,7 @@ type SpeechToTextHandler struct {
OnResultFunc func(context.Context, io.WriteCloser, string, string, string, any) error
}

func NewSpeechToTextHandler(config Config, channelID, connectionID string, sampleRate uint32, channelCount uint16, languageCode string, onResultFunc any) serviceHandlerInterface {
func (h *SpeechToTextHandler) New(config Config, channelID, connectionID string, sampleRate uint32, channelCount uint16, languageCode string, onResultFunc any) serviceHandlerInterface {
return &SpeechToTextHandler{
Config: config,
ChannelID: channelID,
Expand Down
4 changes: 2 additions & 2 deletions test_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func init() {
NewServiceHandlerFuncs.register("test", NewTestHandler)
ServiceHandlers.Register("test", new(TestHandler))
}

type TestHandler struct {
Expand All @@ -28,7 +28,7 @@ type TestHandler struct {
OnResultFunc func(context.Context, io.WriteCloser, string, string, string, any) error
}

func NewTestHandler(config Config, channelID, connectionID string, sampleRate uint32, channelCount uint16, languageCode string, onResultFunc any) serviceHandlerInterface {
func (h *TestHandler) New(config Config, channelID, connectionID string, sampleRate uint32, channelCount uint16, languageCode string, onResultFunc any) serviceHandlerInterface {
return &TestHandler{
Config: config,
ChannelID: channelID,
Expand Down

0 comments on commit 9fd3e17

Please sign in to comment.