Skip to content

Commit

Permalink
handler と handler 作成を分ける
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexa committed Mar 7, 2024
1 parent 9fd3e17 commit 8c1d7d8
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 26 deletions.
6 changes: 4 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() {
ServiceHandlers.Register("aws", new(AmazonTranscribeHandler))
ServiceHandlerMakers.Register("aws", new(AmazonTranscribeHandlerMaker))
}

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

func (h *AmazonTranscribeHandler) New(config Config, channelID, connectionID string, sampleRate uint32, channelCount uint16, languageCode string, onResultFunc any) serviceHandlerInterface {
type AmazonTranscribeHandlerMaker struct{}

func (h *AmazonTranscribeHandlerMaker) 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.ServiceHandlers.GetNames([]string{"test", "dump"}), ", ")))
serviceType := flag.String("service", "aws", fmt.Sprintf("音声文字変換のサービス(%s)", strings.Join(suzu.ServiceHandlerMakers.GetNames([]string{"test", "dump"}), ", ")))
flag.Parse()

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

func getServiceHandler(serviceType string, config Config, channelID, connectionID string, sampleRate uint32, channelCount uint16, languageCode string, onResultFunc any) (serviceHandlerInterface, error) {
handler, err := ServiceHandlers.Get(serviceType)
handler, err := ServiceHandlerMakers.Get(serviceType)
if err != nil {
return nil, err
}
Expand Down
6 changes: 4 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() {
ServiceHandlers.Register("dump", new(PacketDumpHandler))
ServiceHandlerMakers.Register("dump", new(PacketDumpHandlerMaker))
}

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

func (h *PacketDumpHandler) New(config Config, channelID, connectionID string, sampleRate uint32, channelCount uint16, languageCode string, onResultFunc any) serviceHandlerInterface {
type PacketDumpHandlerMaker struct{}

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

var (
ServiceHandlers = NewServiceHandlers()
ServiceHandlerMakers = NewServiceHandlerMakers()

ErrServiceNotFound = fmt.Errorf("SERVICE-NOT-FOUND")
)

type serviceHandlerMakerInterface interface {
New(Config, string, string, uint32, uint16, string, any) serviceHandlerInterface
}

type serviceHandlerInterface interface {
Handle(context.Context, io.Reader) (*io.PipeReader, error)
UpdateRetryCount() int
GetRetryCount() int
ResetRetryCount() int
New(Config, string, string, uint32, uint16, string, any) serviceHandlerInterface
}

type serviceHandlers struct {
Handlers map[string]serviceHandlerInterface
type serviceHandlerMakers struct {
Makers map[string]serviceHandlerMakerInterface
}

func NewServiceHandlers() serviceHandlers {
return serviceHandlers{
Handlers: make(map[string]serviceHandlerInterface),
func NewServiceHandlerMakers() serviceHandlerMakers {
return serviceHandlerMakers{
Makers: make(map[string]serviceHandlerMakerInterface),
}
}

func (h *serviceHandlers) Register(name string, f serviceHandlerInterface) {
h.Handlers[name] = f
func (h *serviceHandlerMakers) Register(name string, f serviceHandlerMakerInterface) {
h.Makers[name] = f
}

func (h *serviceHandlers) Get(name string) (serviceHandlerInterface, error) {
handler, ok := h.Handlers[name]
func (h *serviceHandlerMakers) Get(name string) (serviceHandlerMakerInterface, error) {
maker, ok := h.Makers[name]
if !ok {
return nil, ErrServiceNotFound
}
return handler, nil
return maker, nil
}

func (h *serviceHandlers) GetNames(exclude []string) []string {
handlers := h.Handlers
names := make([]string, 0, len(handlers))
for name := range handlers {
func (h *serviceHandlerMakers) GetNames(exclude []string) []string {
makers := h.Makers
names := make([]string, 0, len(makers))
for name := range makers {
if slices.Contains(exclude, name) {
continue
}
Expand Down
6 changes: 4 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() {
ServiceHandlers.Register("gcp", new(SpeechToTextHandler))
ServiceHandlerMakers.Register("gcp", new(SpeechToTextHandlerMaker))
}

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

func (h *SpeechToTextHandler) New(config Config, channelID, connectionID string, sampleRate uint32, channelCount uint16, languageCode string, onResultFunc any) serviceHandlerInterface {
type SpeechToTextHandlerMaker struct{}

func (h *SpeechToTextHandlerMaker) New(config Config, channelID, connectionID string, sampleRate uint32, channelCount uint16, languageCode string, onResultFunc any) serviceHandlerInterface {
return &SpeechToTextHandler{
Config: config,
ChannelID: channelID,
Expand Down
6 changes: 4 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() {
ServiceHandlers.Register("test", new(TestHandler))
ServiceHandlerMakers.Register("test", new(TestHandlerMaker))
}

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

func (h *TestHandler) New(config Config, channelID, connectionID string, sampleRate uint32, channelCount uint16, languageCode string, onResultFunc any) serviceHandlerInterface {
type TestHandlerMaker struct{}

func (h *TestHandlerMaker) 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 8c1d7d8

Please sign in to comment.