-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
116 lines (95 loc) · 3.12 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2021 the GitHub Workflow Job to PubSub authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/sethvargo/github-workflow-job-to-pubsub/internal/logging"
"github.com/sethvargo/github-workflow-job-to-pubsub/internal/pubsub"
)
const (
// GitHubSignatureHeaderName is the header to search for the GitHub webhook
// payload signature. GitHubSignaturePrefix is the message prefix on the
// signature.
GitHubSignatureHeaderName = "x-hub-signature-256"
GitHubSignaturePrefix = "sha256="
// GitHubEventHeaderName is the header for the event name.
// GitHubEventWorkflowJob is the event name for job queueing.
GitHubEventHeaderName = "x-github-event"
GitHubEventWorkflowJob = "workflow_job"
// Status event types.
StatusQueued = "queued"
StatusInProgress = "in_progress"
StatusCompleted = "completed"
)
var (
// GitHubWebhookSecret is the secret key to use for authenticating the
// webhook's HMAC. This should be injected via Secret Manager or a similar
// process.
GitHubWebhookSecret = os.Getenv("GITHUB_WEBHOOK_SECRET")
// PubSubTopicName is the name of the topic on which to publish.
// PubSubSubscriptionName is the name of the subscription on which to pull.
// These should be the full topic and subscription including the project (e.g.
// "projects/p/topics/t").
PubSubTopicName = os.Getenv("PUBSUB_TOPIC_NAME")
PubSubSubscriptionName = os.Getenv("PUBSUB_SUBSCRIPTION_NAME")
// logger is the logging system.
logger = logging.NewLogger(os.Stdout, os.Stderr)
)
func main() {
ctx, done := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer done()
err := realMain(ctx)
done()
if err != nil {
logger.Fatal("application failure", "error", err)
}
logger.Info("shutting down")
}
func realMain(ctx context.Context) error {
pubsubClient, err := pubsub.NewClient()
if err != nil {
return fmt.Errorf("failed to create pubsub client: %w", err)
}
s := &Server{
pubsubClient: pubsubClient,
}
mux := http.NewServeMux()
mux.Handle("/", s.handleWebhook())
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
srv := &http.Server{
Addr: ":" + port,
Handler: mux,
}
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Error("failed to listen and serve", "error", err)
}
}()
<-ctx.Done()
shutdownCtx, done := context.WithTimeout(context.Background(), 5*time.Second)
defer done()
if err := srv.Shutdown(shutdownCtx); err != nil {
return fmt.Errorf("failed to shutdown server: %w", err)
}
return nil
}