Skip to content

Commit

Permalink
feat: make cmd start server instead of webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
spoukke committed Feb 7, 2023
1 parent 023ace8 commit dc1bc9c
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 45 deletions.
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package cmd
import (
"github.com/padok-team/burrito/cmd/controllers"
"github.com/padok-team/burrito/cmd/runner"
"github.com/padok-team/burrito/cmd/webhook"
"github.com/padok-team/burrito/cmd/server"
"github.com/padok-team/burrito/internal/burrito"

"github.com/spf13/cobra"
Expand All @@ -27,6 +27,6 @@ func buildBurritoCmd(app *burrito.App) *cobra.Command {

cmd.AddCommand(controllers.BuildControllersCmd(app))
cmd.AddCommand(runner.BuildRunnerCmd(app))
cmd.AddCommand(webhook.BuildWebhookCmd(app))
cmd.AddCommand(server.BuildServerCmd(app))
return cmd
}
15 changes: 15 additions & 0 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package server

import (
"github.com/padok-team/burrito/internal/burrito"
"github.com/spf13/cobra"
)

func BuildServerCmd(app *burrito.App) *cobra.Command {
cmd := &cobra.Command{
Use: "server",
Short: "cmd to use burrito's server",
}
cmd.AddCommand(buildServerStartCmd(app))
return cmd
}
21 changes: 21 additions & 0 deletions cmd/server/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package server

import (
"github.com/padok-team/burrito/internal/burrito"
"github.com/spf13/cobra"
)

func buildServerStartCmd(app *burrito.App) *cobra.Command {
cmd := &cobra.Command{
Use: "start",
Short: "Start burrito's server",
RunE: func(cmd *cobra.Command, args []string) error {
app.StartServer()
return nil
},
}

cmd.Flags().StringVar(&app.Config.Server.Port, "port", "80", "port the server listens on")

return cmd
}
18 changes: 0 additions & 18 deletions cmd/webhook/start.go

This file was deleted.

15 changes: 0 additions & 15 deletions cmd/webhook/webhook.go

This file was deleted.

7 changes: 7 additions & 0 deletions internal/burrito/burrito.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/padok-team/burrito/internal/burrito/config"
"github.com/padok-team/burrito/internal/controllers"
"github.com/padok-team/burrito/internal/runner"
"github.com/padok-team/burrito/internal/server"
"github.com/padok-team/burrito/internal/webhook"
)

Expand All @@ -16,6 +17,7 @@ type App struct {
Runner Runner
Controllers Controllers
Webhook Webhook
Server Server

Out io.Writer
Err io.Writer
Expand All @@ -25,6 +27,10 @@ type Webhook interface {
Exec()
}

type Server interface {
Exec()
}

type Runner interface {
Exec()
}
Expand All @@ -40,6 +46,7 @@ func New() (*App, error) {
Runner: runner.New(c),
Controllers: controllers.New(c),
Webhook: webhook.New(c),
Server: server.New(c),
Out: os.Stdout,
Err: os.Stderr,
}
Expand Down
5 changes: 5 additions & 0 deletions internal/burrito/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package burrito

func (app *App) StartServer() {
app.Server.Exec()
}
5 changes: 0 additions & 5 deletions internal/burrito/webhook.go

This file was deleted.

6 changes: 6 additions & 0 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ type Server struct {
config *config.Config
}

func New(c *config.Config) *Server {
return &Server{
config: c,
}
}

func (s *Server) Exec() {
http.HandleFunc("/healthz", handleHealthz)
http.HandleFunc("/webhook", handleWebhook)
Expand Down
10 changes: 5 additions & 5 deletions internal/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ type Handler interface {
Handle()
}

type WebhookHandler struct {
type Webhook struct {
client.Client
config *config.Config
github *github.Webhook
gitlab *gitlab.Webhook
}

func New(c *config.Config) *WebhookHandler {
return &WebhookHandler{
func New(c *config.Config) *Webhook {
return &Webhook{
config: c,
}
}

func (w *WebhookHandler) Init() error {
func (w *Webhook) Init() error {
githubWebhook, err := github.New(github.Options.Secret(w.config.Webhook.Github.Secret))
if err != nil {
return err
Expand All @@ -47,7 +47,7 @@ func (w *WebhookHandler) Init() error {
return nil
}

func (w *WebhookHandler) Handle(payload interface{}) {
func (w *Webhook) Handle(payload interface{}) {
webUrls, revision, change, touchedHead, changedFiles := affectedRevisionInfo(payload)
if len(webUrls) == 0 {
fmt.Println("Ignoring webhook event")
Expand Down

0 comments on commit dc1bc9c

Please sign in to comment.