-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
92 lines (72 loc) · 1.97 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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"github.com/cloudflare/cloudflare-go"
"github.com/gin-gonic/gin"
_ "github.com/heroku/x/hmetrics/onload"
)
type Sender struct {
Login string `json:"login" binding:"required"`
}
type JSONGithubWebhook struct {
Sender Sender `json:"sender" binding:"required"`
Action string `json:"action" binding:"required"`
}
func main() {
port := os.Getenv("PORT")
if port == "" {
log.Fatal("$PORT must be set")
}
dnsRecordContent := os.Getenv("DNS_RECORD_CONTENT")
if dnsRecordContent == "" {
log.Fatal("$DNS_RECORD_CONTENT must be set")
}
api, err := cloudflare.NewWithAPIToken(os.Getenv("CLOUDFLARE_API_TOKEN"))
if err != nil {
log.Fatal(err)
}
cloudflareZoneID, err := api.ZoneIDByName(os.Getenv("CLOUDFLARE_DOMAIN"))
if err != nil {
log.Fatal(err)
}
router := gin.New()
router.Use(gin.Logger())
router.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "OK"})
})
router.POST("/", func(c *gin.Context) {
var json JSONGithubWebhook
if err := c.ShouldBindJSON(&json); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if json.Action != "created" {
c.JSON(http.StatusOK, gin.H{"message": "only created events are accepted"})
return
}
name := "*." + json.Sender.Login + ".github"
fmt.Println("Attempting to create dns record for " + name)
proxied := true
var createDNSRecordParams = cloudflare.CreateDNSRecordParams{
Content: dnsRecordContent,
Name: name,
Type: "CNAME",
Comment: "Github user wildcard",
Proxied: &proxied,
TTL: 3600,
}
record, err := api.CreateDNSRecord(context.Background(), cloudflare.ZoneIdentifier(cloudflareZoneID), createDNSRecordParams)
if err != nil {
fmt.Println(err)
c.JSON(http.StatusCreated, gin.H{"message": "star gazer not created"})
return
}
fmt.Println(record)
c.JSON(http.StatusCreated, gin.H{"message": "star gazer created"})
})
router.Run(":" + port)
}