Skip to content

Commit

Permalink
add routeless config handling
Browse files Browse the repository at this point in the history
Resolves: greenpau#10
  • Loading branch information
greenpau committed Mar 12, 2022
1 parent a125acd commit b15e3b5
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 14 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Configuration examples:
* [Private or public repo over SSH with key-based authentication](./assets/config/ssh/Caddyfile)
* [Repo with Webhooks](./assets/config/webhook/Caddyfile)
* [Repo with post pull execution scripts](./assets/config/post_cmd_exec/Caddyfile)
* [Routeless config](./assets/config/routeless/Caddyfile)

For example, the following configuration sets up a definition for `authp.github.io`
repo. The request to `authp.myfiosgateway.com/update/authp.github.io` trigger
Expand All @@ -49,11 +50,19 @@ repo. The request to `authp.myfiosgateway.com/update/authp.github.io` trigger
base_dir /tmp
url https://github.com/authp/authp.github.io.git
branch gh-pages
post pull exec {
name Pager
command /usr/bin/echo
args "pulled authp.github.io repo"
}
}
}
}
authp.myfiosgateway.com {
route /version* {
respond * "1.0.0" 200
}
route /update/authp.github.io {
git update repo authp.github.io
}
Expand Down
4 changes: 2 additions & 2 deletions assets/config/Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

git {
repo authp.github.io {
base_dir ./tmp
base_dir {$HOME}/tmp
url https://github.com/authp/authp.github.io.git
branch gh-pages
}
Expand All @@ -22,7 +22,7 @@
}
route {
file_server {
root ./tmp/authp.github.io
root {$HOME}/tmp/authp.github.io
}
}
}
4 changes: 2 additions & 2 deletions assets/config/post_cmd_exec/Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

git {
repo authp.github.io {
base_dir ./tmp
base_dir {$HOME}/tmp
url https://github.com/authp/authp.github.io.git
branch gh-pages
post pull exec {
Expand All @@ -27,7 +27,7 @@
}
route {
file_server {
root ./tmp/authp.github.io
root {$HOME}/tmp/authp.github.io
}
}
}
28 changes: 28 additions & 0 deletions assets/config/routeless/Caddyfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
debug
local_certs
http_port 8080
https_port 8443

order git before respond

git {
repo authp.github.io {
base_dir {$HOME}/tmp/authp/gitcfg/
url https://github.com/authp/authp.github.io.git
branch gh-pages
post pull exec {
name Pager
command /usr/bin/echo
args "pulled authp.github.io repo"
}
}
}
}

127.0.0.1, localhost {
respond /version* "1.0.0" 200
git /update/authp.github.io update repo authp.github.io
root * {$HOME}/tmp/authp/gitcfg/authp.github.io
file_server
}
10 changes: 5 additions & 5 deletions assets/config/ssh/Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

git {
repo authp.github.io {
base_dir ./tmp/ssh
base_dir {$HOME}/tmp/ssh
url [email protected]:authp/authp.github.io.git
# auth key ~/.ssh/id_rsa passphrase {env.MY_SSH_KEY_PASSPHRASE}
# auth key ~/.ssh/id_rsa passphrase {env.MY_SSH_KEY_PASSPHRASE} no_strict_host_key_check
auth key ~/.ssh/id_rsa
# auth key {$HOME}/.ssh/id_rsa passphrase {env.MY_SSH_KEY_PASSPHRASE}
# auth key {$HOME}/.ssh/id_rsa passphrase {env.MY_SSH_KEY_PASSPHRASE} no_strict_host_key_check
auth key {$HOME}/.ssh/id_rsa
branch gh-pages
}
}
Expand All @@ -25,7 +25,7 @@
}
route {
file_server {
root ./tmp/ssh/authp.github.io
root {$HOME}/tmp/ssh/authp.github.io
}
}
}
4 changes: 2 additions & 2 deletions assets/config/webhook/Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

git {
repo authp.github.io {
base_dir ./tmp
base_dir {$HOME}/tmp
url https://github.com/authp/authp.github.io.git
# webhook Github X-Hub-Signature-256 {env.MY_GITHUB_WEBHOOK_KEY}
webhook Gitlab X-Gitlab-Token barbaz
Expand All @@ -24,7 +24,7 @@
}
route {
file_server {
root ./tmp/authp.github.io
root {$HOME}/tmp/authp.github.io
}
}
}
20 changes: 17 additions & 3 deletions caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,25 @@ func parseCaddyfileHandlerConfig(h httpcaddyfile.Helper) (*service.Endpoint, err

for h.Next() {
args := h.RemainingArgs()
strArgs := strings.Join(args, " ")
if !strings.Contains(strArgs, "update repo ") {
return nil, h.Errf("unsupported config: git %s", strArgs)
}
switch {
case strings.HasPrefix(strings.Join(args, " "), "update repo "):
case args[0] == "update" && args[1] == "repo":
if len(args) != 3 {
return nil, h.Errf("malformed config: git %s", strArgs)
}
endpoint.Path = "*"
endpoint.RepositoryName = args[2]
case args[1] == "update" && args[2] == "repo":
if len(args) != 4 {
return nil, h.Errf("malformed config: git %s", strArgs)
}
endpoint.Path = args[0]
endpoint.RepositoryName = args[3]
default:
return nil, h.Errf("unsupported config: git %s", strings.Join(args, " "))
return nil, h.Errf("malformed config: git %s", strArgs)
}
}

Expand All @@ -227,7 +241,7 @@ func getRouteFromParseCaddyfileHandlerConfig(h httpcaddyfile.Helper) ([]httpcadd
return nil, err
}
pathMatcher := caddy.ModuleMap{
"path": h.JSON(caddyhttp.MatchPath{"*"}),
"path": h.JSON(caddyhttp.MatchPath{endpoint.Path}),
}
route := caddyhttp.Route{
HandlersRaw: []json.RawMessage{
Expand Down
2 changes: 2 additions & 0 deletions pkg/service/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
type Endpoint struct {
mu sync.Mutex
Name string `json:"-"`
Path string `json:"path,omitempty" xml:"path,omitempty" yaml:"path,omitempty"`
RepositoryName string
logger *zap.Logger
startedAt time.Time
Expand All @@ -45,6 +46,7 @@ func (m *Endpoint) Provision() error {
m.logger.Info(
"provisioned plugin instance",
zap.String("instance_name", m.Name),
zap.String("path", m.Path),
zap.Time("started_at", m.startedAt),
)
return nil
Expand Down

0 comments on commit b15e3b5

Please sign in to comment.