caddy_webhook

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2021 License: MIT Imports: 19 Imported by: 0

README

caddy-webhook

Build

Caddy v2 module for serving a webhook.

Installation

Build with xcaddy
xcaddy build \
  --with github.com/WingLim/caddy-webhook
Run in docker

See caddy-docker for Dockerfile.

DockerHub: winglim/caddy

GitHub Package: winglim/caddy

Usage

Now supported webhook type:

  • github
  • gitlab
  • gitee
  • bitbucket
Caddyfile Format

Notice: webhook block should be the last handler of route. After receive request and handle it, we return nil instead of the next middleware. So, the rest handler after webhook will not work.

webhook [<url> <path>] {
    repo    <text>
    path    <text>
    branch  <text>
    depth   <int>
    type    <text>
    secret  <text>
    command <text>...
    submodule
}
  • repo - git repository url.
  • path - path to clone and update repository.
  • branch - branch to pull. Default is main.
  • depth - depth for pull. Default is 0.
  • type - webhook type. Default is github.
  • secret - secret to verify webhook request.
  • submodule - enable recurse submodules
  • command - the command run when repo initializes or get the correct webhook request
Example

The full example to run a hugo blog:

Caddyfile:

example.com

root www
file_server

route /webhook {
    webhook {
        repo https://github.com/WingLim/winglim.github.io.git
        path blog
        branch hugo
        command hugo --destination ../www
        submodule   
    }
}

With the config above, webhook module will do things:

  1. Clone https://github.com/WingLim/winglim.github.io.git to blog when initializes.

    1. If the repo is already exist, will fetch and checkout to the branch you set.
  2. Run the command hugo --destination ../www inside the blog directory.

  3. Listen and serve at /webhook and handle the webhook request.

    1. When receive correct webhook request, will update repo and do step 2 again.

Thanks to

Documentation

Index

Constants

View Source
const (
	DefaultRemote = "origin"
	DefaultBranch = "main"
)

Variables

This section is empty.

Functions

func ValidateRequest

func ValidateRequest(r *http.Request) error

ValidateRequest validates webhook request, the webhook request should be POST.

Types

type Cmd

type Cmd struct {
	Command string
	Args    []string
	Path    string
}

func (*Cmd) AddCommand

func (c *Cmd) AddCommand(command []string, path string)

func (*Cmd) Run

func (c *Cmd) Run(logger *zap.Logger)

type Repo

type Repo struct {
	URL       string
	Path      string
	Branch    string
	Depth     int
	Secret    string
	Submodule git.SubmoduleRescursivity
	// contains filtered or unexported fields
}

Repo tells information about the git repository.

func NewRepo

func NewRepo(w *WebHook) *Repo

NewRepo creates a new repo with options.

func (*Repo) Setup

func (r *Repo) Setup(ctx context.Context) error

Setup initializes the git repository by either cloning or opening it.

func (*Repo) Update

func (r *Repo) Update(ctx context.Context) error

Update pulls updates from the remote repository into current worktree.

type WebHook

type WebHook struct {
	Repository string   `json:"repo,omitempty"`
	Path       string   `json:"path,omitempty"`
	Branch     string   `json:"branch,omitempty"`
	Type       string   `json:"type,omitempty"`
	Secret     string   `json:"secret,omitempty"`
	Depth      string   `json:"depth,omitempty"`
	Submodule  bool     `json:"submodule,omitempty"`
	Command    []string `json:"command,omitempty"`
	// contains filtered or unexported fields
}

WebHook is the module configuration.

func (*WebHook) CaddyModule

func (*WebHook) CaddyModule() caddy.ModuleInfo

CaddyModule returns the Caddy module information.

func (*WebHook) Provision

func (w *WebHook) Provision(ctx caddy.Context) error

Provision set's up webhook configuration.

func (*WebHook) ServeHTTP

func (w *WebHook) ServeHTTP(rw http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error

ServeHTTP implements caddyhttp.MiddlewareHandler.

func (*WebHook) UnmarshlCaddyfile

func (w *WebHook) UnmarshlCaddyfile(d *caddyfile.Dispenser) error

UnmarshCaddyfile configures the handler directive from Caddyfile. Syntax:

webhook [<url> <path>] {
	repo		<text>
	path 		<text>
	branch 		<text>
	depth		<int>
	type 		<text>
	secret		<text>
	command		<test>...
	submodule
}

func (*WebHook) Validate

func (w *WebHook) Validate() error

Validate ensures webhook's configuration is valid.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL