Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
49bb3f13
Commit
49bb3f13
authored
May 01, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
git: Service routine, customizable logger, no more HTTP handler
parent
53a89c95
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
36 deletions
+31
-36
middleware/git/git.go
middleware/git/git.go
+29
-19
middleware/git/gitclient.go
middleware/git/gitclient.go
+2
-17
No files found.
middleware/git/git.go
View file @
49bb3f13
...
@@ -2,29 +2,19 @@ package git
...
@@ -2,29 +2,19 @@ package git
import
(
import
(
"fmt"
"fmt"
"github.com/mholt/caddy/middleware"
"log"
"net/http"
"net/url"
"net/url"
"path/filepath"
"path/filepath"
"runtime"
"runtime"
"strconv"
"strconv"
"strings"
"strings"
"time"
"time"
"github.com/mholt/caddy/middleware"
)
)
// Git represents a middleware instance that pulls git repository.
// Logger is used to log errors; if nil, the default log.Logger is used.
type
Git
struct
{
var
Logger
*
log
.
Logger
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
)
}
// New creates a new instance of git middleware.
// New creates a new instance of git middleware.
func
New
(
c
middleware
.
Controller
)
(
middleware
.
Middleware
,
error
)
{
func
New
(
c
middleware
.
Controller
)
(
middleware
.
Middleware
,
error
)
{
...
@@ -32,10 +22,30 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
...
@@ -32,10 +22,30 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
err
=
repo
.
Pull
()
return
func
(
next
middleware
.
Handler
)
middleware
.
Handler
{
c
.
Startup
(
func
()
error
{
return
Git
{
Next
:
next
,
Repo
:
repo
}
// Startup functions are blocking; start
},
err
// 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
)
{
func
parse
(
c
middleware
.
Controller
)
(
*
Repo
,
error
)
{
...
...
middleware/git/gitclient.go
View file @
49bb3f13
...
@@ -31,28 +31,13 @@ type Repo struct {
...
@@ -31,28 +31,13 @@ type Repo struct {
Branch
string
// Git branch
Branch
string
// Git branch
KeyPath
string
// Path to private ssh key
KeyPath
string
// Path to private ssh key
Interval
time
.
Duration
// Interval between pulls
Interval
time
.
Duration
// Interval between pulls
pulled
bool
// true if there
i
s a successful pull
pulled
bool
// true if there
wa
s a successful pull
lastPull
time
.
Time
// time of the last successful pull
lastPull
time
.
Time
// time of the last successful pull
sync
.
Mutex
sync
.
Mutex
}
}
// Pull requests a repository pull.
// Pull performs git clone, or git pull if repository exists
// If it has been performed previously, it returns
// and requests another pull in background.
// Otherwise it waits until the pull is done.
func
(
r
*
Repo
)
Pull
()
error
{
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
()
r
.
Lock
()
defer
r
.
Unlock
()
defer
r
.
Unlock
()
// if it is less than interval since last pull, return
// if it is less than interval since last pull, return
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment