mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-24 01:26:47 +01:00
git: Service routine, customizable logger, no more HTTP handler
This commit is contained in:
parent
53a89c953a
commit
49bb3f1387
2 changed files with 31 additions and 36 deletions
|
@ -2,29 +2,19 @@ package git
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/mholt/caddy/middleware"
|
||||
"net/http"
|
||||
"log"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/mholt/caddy/middleware"
|
||||
)
|
||||
|
||||
// Git represents a middleware instance that pulls git repository.
|
||||
type Git struct {
|
||||
Next middleware.Handler
|
||||
Repo *Repo
|
||||
}
|
||||
|
||||
// ServeHTTP satisfies the middleware.Handler interface.
|
||||
func (g Git) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
if err := g.Repo.Pull(); err != nil {
|
||||
return 500, err
|
||||
}
|
||||
return g.Next.ServeHTTP(w, r)
|
||||
}
|
||||
// Logger is used to log errors; if nil, the default log.Logger is used.
|
||||
var Logger *log.Logger
|
||||
|
||||
// New creates a new instance of git middleware.
|
||||
func New(c middleware.Controller) (middleware.Middleware, error) {
|
||||
|
@ -32,10 +22,30 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = repo.Pull()
|
||||
return func(next middleware.Handler) middleware.Handler {
|
||||
return Git{Next: next, Repo: repo}
|
||||
}, err
|
||||
|
||||
c.Startup(func() error {
|
||||
// Startup functions are blocking; start
|
||||
// service routine in background
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(repo.Interval)
|
||||
|
||||
err := repo.Pull()
|
||||
if err != nil {
|
||||
if Logger == nil {
|
||||
log.Println(err)
|
||||
} else {
|
||||
Logger.Println(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// Do a pull right away to return error
|
||||
return repo.Pull()
|
||||
})
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func parse(c middleware.Controller) (*Repo, error) {
|
||||
|
|
|
@ -31,28 +31,13 @@ type Repo struct {
|
|||
Branch string // Git branch
|
||||
KeyPath string // Path to private ssh key
|
||||
Interval time.Duration // Interval between pulls
|
||||
pulled bool // true if there is a successful pull
|
||||
pulled bool // true if there was a successful pull
|
||||
lastPull time.Time // time of the last successful pull
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
// Pull requests a repository pull.
|
||||
// If it has been performed previously, it returns
|
||||
// and requests another pull in background.
|
||||
// Otherwise it waits until the pull is done.
|
||||
// Pull performs git clone, or git pull if repository exists
|
||||
func (r *Repo) Pull() error {
|
||||
// if site is not pulled, pull
|
||||
if !r.pulled {
|
||||
return pull(r)
|
||||
}
|
||||
|
||||
// request pull in background
|
||||
go pull(r)
|
||||
return nil
|
||||
}
|
||||
|
||||
// pull performs git clone, or git pull if repository exists
|
||||
func pull(r *Repo) error {
|
||||
r.Lock()
|
||||
defer r.Unlock()
|
||||
// if it is less than interval since last pull, return
|
||||
|
|
Loading…
Reference in a new issue