Skip to content

Commit

Permalink
add caddy app and plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
greenpau committed Jan 12, 2022
1 parent 1217742 commit 2f467e8
Show file tree
Hide file tree
Showing 19 changed files with 2,747 additions and 4 deletions.
71 changes: 71 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
name: build

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
core:
strategy:
matrix:
go-version: [1.16.x]
platform: [ubuntu-latest]
name: Build
runs-on: ${{ matrix.platform }}
env:
GOBIN: /home/runner/.local/bin
steps:
- name: Install Go
uses: actions/setup-go@v1
with:
go-version: ${{ matrix.go-version }}
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v2
- name: Amend Environment Path
run: |
mkdir -p /home/runner/.local/bin
echo "/home/runner/.local/bin" >> $GITHUB_PATH
- name: Setup Environment
run: |
mkdir -p .coverage
echo "*** Current Directory ***"
pwd
echo "*** Environment Variables ***"
env | sort
echo "*** Executable Path ***"
echo "$PATH" | tr ':' '\n'
echo "*** Workspace Files ***"
find .
which make
- name: Install prerequisites
run: |
sudo apt-get --assume-yes install make
sudo apt-get --assume-yes install libnss3-tools
sudo apt-get update
- name: Install Go modules
run: |
make dep
go mod tidy
go mod verify
go mod download
- name: Validate prerequisites
run: |
echo "*** Local binaries ***"
find /home/runner/.local/bin
- name: Run tests
run: |
make test || true
make test
- name: Generate coverage report
run: make coverage
- name: Upload coverage report
uses: actions/upload-artifact@v1
with:
name: Test Coverage Report
path: .coverage/coverage.html
105 changes: 105 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
.PHONY: test ctest covdir bindir coverage docs linter qtest clean dep release logo license
PLUGIN_NAME="caddy-git"
PLUGIN_VERSION:=$(shell cat VERSION | head -1)
GIT_COMMIT:=$(shell git describe --dirty --always)
GIT_BRANCH:=$(shell git rev-parse --abbrev-ref HEAD -- | head -1)
LATEST_GIT_COMMIT:=$(shell git log --format="%H" -n 1 | head -1)
BUILD_USER:=$(shell whoami)
BUILD_DATE:=$(shell date +"%Y-%m-%d")
BUILD_DIR:=$(shell pwd)
CADDY_VERSION="v2.4.6"

all: info
@mkdir -p bin/
@rm -rf ./bin/caddy
@rm -rf ../xcaddy-$(PLUGIN_NAME)/*
@mkdir -p ../xcaddy-$(PLUGIN_NAME) && cd ../xcaddy-$(PLUGIN_NAME) && \
xcaddy build $(CADDY_VERSION) --output ../$(PLUGIN_NAME)/bin/caddy \
--with github.com/greenpau/caddy-git@$(LATEST_GIT_COMMIT)=$(BUILD_DIR)
@#bin/caddy run -config assets/config/Caddyfile

info:
@echo "DEBUG: Version: $(PLUGIN_VERSION), Branch: $(GIT_BRANCH), Revision: $(GIT_COMMIT)"
@echo "DEBUG: Build on $(BUILD_DATE) by $(BUILD_USER)"

linter:
@echo "DEBUG: running lint checks"
@golint -set_exit_status ./...
@echo "DEBUG: completed $@"

test: covdir linter
@echo "DEBUG: running tests"
@go test -v -coverprofile=.coverage/coverage.out ./...
@echo "DEBUG: completed $@"

ctest: covdir linter
@echo "DEBUG: running tests"
@time richgo test -v -coverprofile=.coverage/coverage.out ./...
@echo "DEBUG: completed $@"

covdir:
@echo "DEBUG: creating .coverage/ directory"
@mkdir -p .coverage
@echo "DEBUG: completed $@"

bindir:
@echo "DEBUG: creating bin/ directory"
@mkdir -p bin/
@echo "DEBUG: completed $@"

coverage: covdir
@echo "DEBUG: running coverage"
@go tool cover -html=.coverage/coverage.out -o .coverage/coverage.html
@go test -covermode=count -coverprofile=.coverage/coverage.out ./...
@go tool cover -func=.coverage/coverage.out | grep -v "100.0"
@echo "DEBUG: completed $@"

clean:
@rm -rf .coverage/
@rm -rf bin/
@echo "DEBUG: completed $@"

qtest: covdir
@echo "DEBUG: perform quick tests ..."
@#go test -v -coverprofile=.coverage/coverage.out -run TestApp ./*.go
@#go test -v -coverprofile=.coverage/coverage.out -run TestParseCaddyfile ./*.go
@time richgo test -v -coverprofile=.coverage/coverage.out -run TestParseCaddyfileAppConfig ./*.go
@#go test -v -coverprofile=.coverage/coverage.out -run Test* ./pkg/services/...
@go tool cover -html=.coverage/coverage.out -o .coverage/coverage.html
@go tool cover -func=.coverage/coverage.out | grep -v "100.0"
@echo "DEBUG: completed $@"

dep:
@echo "Making dependencies check ..."
@go get -u golang.org/x/lint/golint
@go get -u github.com/caddyserver/xcaddy/cmd/xcaddy@latest
@go get -u github.com/greenpau/versioned/cmd/versioned@latest
@go get -u github.com/kyoh86/richgo

release:
@echo "Making release"
@go mod tidy
@go mod verify
@if [ $(GIT_BRANCH) != "main" ]; then echo "cannot release to non-main branch $(GIT_BRANCH)" && false; fi
@git diff-index --quiet HEAD -- || ( echo "git directory is dirty, commit changes first" && false )
@versioned -patch
@echo "Patched version"
@git add VERSION
@git commit -m "released v`cat VERSION | head -1`"
@git tag -a v`cat VERSION | head -1` -m "v`cat VERSION | head -1`"
@git push
@git push --tags
@@echo "If necessary, run the following commands:"
@echo " git push --delete origin v$(PLUGIN_VERSION)"
@echo " git tag --delete v$(PLUGIN_VERSION)"

logo:
@mkdir -p assets/docs/images
@gm convert -background black -font Bookman-Demi \
-size 640x320 "xc:black" \
-pointsize 72 \
-draw "fill white gravity center text 0,0 'caddy\ngit'" \
assets/docs/images/logo.png

license:
@for f in `find ./ -type f -name '*.go'`; do versioned -addlicense -copyright="Paul Greenberg [email protected]" -year=2022 -filepath=$$f; done
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

Git Plugin for [Caddy v2](https://github.com/caddyserver/caddy).

Inspired by [this comment](https://github.com/vrongmeal/caddygit/pull/5#issuecomment-1010440830).

Please ask questions either here or via LinkedIn. I am happy to help you! @greenpau

Please see other plugins:
Expand Down Expand Up @@ -38,19 +40,18 @@ repo. The request to `authp.myfiosgateway.com/update/authp.github.io` trigger
{
git {
repo authp.github.io {
base path /tmp
base_dir /tmp
url https://github.com/authp/authp.github.io.git
branch gh-pages
access token FOOBAR
single branch
depth 1
update every 600 # 10 minutes
}
}
}
authp.myfiosgateway.com {
route /update/authp.github.io {
git update repo authp.github.io every 5 minutes
git update repo authp.github.io
}
route {
file_server {
Expand Down
131 changes: 131 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Copyright 2022 Paul Greenberg [email protected]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package git

import (
"fmt"
"github.com/caddyserver/caddy/v2"
"github.com/greenpau/caddy-git/pkg/service"
"go.uber.org/zap"
)

var (
appName = "git"

// Interface guards
_ caddy.Provisioner = (*App)(nil)
_ caddy.Module = (*App)(nil)
_ caddy.App = (*App)(nil)
)

func init() {
caddy.RegisterModule(App{})
}

// App implements git repository manager.
type App struct {
Name string `json:"-"`
Config *service.Config `json:"config,omitempty"`
manager *service.Manager
logger *zap.Logger
}

// CaddyModule returns the Caddy module information.
func (App) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: caddy.ModuleID(appName),
New: func() caddy.Module { return new(App) },
}
}

// Provision sets up the repo manager.
func (app *App) Provision(ctx caddy.Context) error {
app.Name = appName
app.logger = ctx.Logger(app)

app.logger.Info(
"provisioning app instance",
zap.String("app", app.Name),
)

manager, err := service.NewManager(app.Config, app.logger)
if err != nil {
app.logger.Error(
"failed configuring app instance",
zap.String("app", app.Name),
zap.Error(err),
)
}
app.manager = manager

app.logger.Info(
"provisioned app instance",
zap.String("app", app.Name),
)
return nil
}

// Start starts the App.
func (app App) Start() error {
app.logger.Debug(
"starting git repo manager",
zap.String("app", app.Name),
)

if msgs := app.manager.Start(); msgs != nil {
for _, msg := range msgs {
app.logger.Error(
"failed managing git repo",
zap.String("app", app.Name),
zap.String("repo", msg.Repository),
zap.Error(msg.Error),
)
}
return fmt.Errorf("git repo manager failed to start")
}

app.logger.Debug(
"started git repo manager",
zap.String("app", app.Name),
)

return nil
}

// Stop stops the App.
func (app App) Stop() error {
app.logger.Debug(
"stopping git repo manager",
zap.String("app", app.Name),
)

if msgs := app.manager.Stop(); msgs != nil {
for _, msg := range msgs {
app.logger.Error(
"failed stoppint git repo manager",
zap.String("app", app.Name),
zap.String("repo", msg.Repository),
zap.Error(msg.Error),
)
}
return fmt.Errorf("git repo manager failed to stop properly")
}

app.logger.Debug(
"stopped git repo manager",
zap.String("app", app.Name),
)
return nil
}
Loading

0 comments on commit 2f467e8

Please sign in to comment.