@@ -12,7 +12,9 @@ import (
1212 "github.com/w-h-a/workflow/internal/engine/config"
1313 httphandlers "github.com/w-h-a/workflow/internal/engine/handlers/http"
1414 "github.com/w-h-a/workflow/internal/engine/services/coordinator"
15+ "github.com/w-h-a/workflow/internal/engine/services/streamer"
1516 "github.com/w-h-a/workflow/internal/engine/services/worker"
17+ "github.com/w-h-a/workflow/internal/log"
1618 "github.com/w-h-a/workflow/internal/task"
1719 "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
1820 "go.opentelemetry.io/otel"
@@ -122,3 +124,53 @@ func NewWorker(
122124
123125 return httpServer , workerService
124126}
127+
128+ func NewStreamer (
129+ brokerClient broker.Broker ,
130+ ) (serverv2.Server , * streamer.Service ) {
131+ // services
132+ streamerService := streamer .New (
133+ brokerClient ,
134+ map [string ]int {
135+ string (log .Queue ): 1 ,
136+ },
137+ )
138+
139+ // base server options
140+ opts := []serverv2.ServerOption {
141+ serverv2 .ServerWithNamespace (config .Env ()),
142+ serverv2 .ServerWithName (config .Name ()),
143+ serverv2 .ServerWithVersion (config .Version ()),
144+ }
145+
146+ // create http router
147+ router := mux .NewRouter ()
148+
149+ httpStatus := httphandlers .NewStatusHandler (streamerService )
150+ router .Methods (http .MethodGet ).Path ("/status" ).HandlerFunc (httpStatus .GetStatus )
151+
152+ httpLogs := httphandlers .NewLogsHandler (streamerService )
153+ router .Methods (http .MethodGet ).Path ("/logs/{id}" ).HandlerFunc (httpLogs .StreamLogs )
154+
155+ // create http server
156+ httpOpts := []serverv2.ServerOption {
157+ serverv2 .ServerWithAddress (config .StreamerHttp ()),
158+ }
159+
160+ httpOpts = append (httpOpts , opts ... )
161+
162+ httpServer := httpserver .NewServer (httpOpts ... )
163+
164+ handler := otelhttp .NewHandler (
165+ router ,
166+ "" ,
167+ otelhttp .WithSpanNameFormatter (func (operation string , r * http.Request ) string { return r .URL .Path }),
168+ otelhttp .WithTracerProvider (otel .GetTracerProvider ()),
169+ otelhttp .WithPropagators (otel .GetTextMapPropagator ()),
170+ otelhttp .WithFilter (func (r * http.Request ) bool { return r .URL .Path != "/status" }),
171+ )
172+
173+ httpServer .Handle (handler )
174+
175+ return httpServer , streamerService
176+ }
0 commit comments