-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
51 lines (40 loc) · 1.13 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
package main
import (
"log"
"os"
"github.com/woodpecker-ci/example-extensions/config"
"github.com/woodpecker-ci/example-extensions/secrets"
"github.com/woodpecker-ci/example-extensions/utils"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func main() {
log.Println("Woodpecker sample extensions server")
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file. Copy '.env.example' to '.env': %v", err)
}
pubKey, err := utils.GetPubKey()
if err != nil {
log.Fatalf("Error getting public key: %v", err)
}
r := gin.Default()
// IMPORTANT: We verify all incoming requests to our api to ensure they are signed and coming from Woodpecker
r.Use(func(c *gin.Context) {
err := utils.Verify(pubKey, c.Request)
if err != nil {
log.Printf("Failed to verify request: %v", err)
c.JSON(401, gin.H{"error": "Failed to verify request"})
c.Abort()
return
}
})
g := r.Group("/repo/:repoId")
config.RegisterConfigExtension(g, pubKey)
secrets.RegisterSecretsExtension(g, pubKey)
host := os.Getenv("EXTENSION_HOST")
if host != "" {
log.Printf("Starting server on %s\n", host)
}
r.Run(host)
}