Spaces:
Paused
Paused
ML Intern deploy
Deploy httpSMS gateway to Hugging Face Spaces (Docker SDK): nginx :7860 -> Go API :8000, Nuxt SPA, Android APK, bootstrap-mode entrypoint
83a59ca Download api/pkg/services/emulator_push_queue.go from red-kit/httpsms-gateway: direct link, hf CLI and curl.
- Browser
- Download file 1.98 kB
-
https://huggingface.co/spaces/red-kit/httpsms-gateway/resolve/6917738ca6db1603dee9e07994b4e42d5e1ae9b2/api/pkg/services/emulator_push_queue.go
- Command line
-
hf download hf://spaces/red-kit/httpsms-gateway@6917738ca6db1603dee9e07994b4e42d5e1ae9b2/api/pkg/services/emulator_push_queue.go
-
curl -L -o emulator_push_queue.go https://huggingface.co/spaces/red-kit/httpsms-gateway/resolve/6917738ca6db1603dee9e07994b4e42d5e1ae9b2/api/pkg/services/emulator_push_queue.go
1.98 kB
| package services | |
| import ( | |
| "context" | |
| "fmt" | |
| "net/http" | |
| "time" | |
| "github.com/NdoleStudio/stacktrace" | |
| "github.com/carlmjohnson/requests" | |
| "github.com/NdoleStudio/httpsms/pkg/telemetry" | |
| "github.com/google/uuid" | |
| ) | |
| type emulatorPushQueue struct { | |
| config PushQueueConfig | |
| client *http.Client | |
| logger telemetry.Logger | |
| tracer telemetry.Tracer | |
| } | |
| // EmulatorPushQueue creates a new googlePushQueue | |
| func EmulatorPushQueue( | |
| logger telemetry.Logger, | |
| tracer telemetry.Tracer, | |
| client *http.Client, | |
| config PushQueueConfig, | |
| ) PushQueue { | |
| return &emulatorPushQueue{ | |
| tracer: tracer, | |
| logger: logger.WithService(fmt.Sprintf("%T", emulatorPushQueue{})), | |
| client: client, | |
| config: config, | |
| } | |
| } | |
| // Enqueue a task to the queue | |
| func (queue *emulatorPushQueue) Enqueue(ctx context.Context, task *PushQueueTask, timeout time.Duration) (queueID string, err error) { | |
| ctx, span, ctxLogger := queue.tracer.StartWithLogger(ctx, queue.logger) | |
| defer span.End() | |
| queueID = uuid.New().String() | |
| time.AfterFunc(timeout, queue.push(*task, queueID)) | |
| ctxLogger.Info(fmt.Sprintf( | |
| "task added to [%s] queue with ID [%s] and scheduled at [%s]", | |
| queue.config.Name, | |
| queueID, | |
| time.Now().UTC().Add(timeout), | |
| )) | |
| return queueID, nil | |
| } | |
| func (queue *emulatorPushQueue) push(task PushQueueTask, queueID string) func() { | |
| return func() { | |
| ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | |
| defer cancel() | |
| request := requests. | |
| URL(task.URL). | |
| Client(queue.client). | |
| Method(task.Method). | |
| BodyBytes(task.Body) | |
| // add headers | |
| for key, value := range task.Headers { | |
| request.Header(key, value) | |
| } | |
| // add content type | |
| request.Header("Content-Type", "application/json") | |
| if err := request.Fetch(ctx); err != nil { | |
| queue.logger.Error(stacktrace.Propagatef(err, "cannot send http request to [%s] for queue task [%s]", task.URL, queueID)) | |
| return | |
| } | |
| queue.logger.Info(fmt.Sprintf("queue task [%s] sent to URL [%s]", queueID, task.URL)) | |
| } | |
| } | |