grim/hgkeeper

Add a caching layer to the hgweb portion. This should take some strain off of mercurial anf our cpu quota
package serve
import (
"os"
"os/signal"
"syscall"
log "github.com/sirupsen/logrus"
"bitbucket.org/rw_grim/hgkeeper/access"
"bitbucket.org/rw_grim/hgkeeper/globals"
"bitbucket.org/rw_grim/hgkeeper/hgweb"
"bitbucket.org/rw_grim/hgkeeper/ssh"
)
type Command struct {
SSHAddr string `kong:"flag,name='ssh-listen-addr',env='HGK_SSH_LISTEN_ADDR',short='l',help='what address to listen on',default=':22222'"`
SSHHostKeysPath string `kong:"flag,name='ssh-host-keys-path',env='HGK_SSH_HOST_KEYS_PATH',short='H',help='the path where host keys are kept',default='host-keys'"`
HTTPAddr string `kong:"flag,name='http-listen-addr',env='HGK_HTTP_LISTEN_ADDR',help='what address the http server listens on',default=':8080'"`
CacheSize int `kong:"flag,name='cache-size',env='HGK_HTTP_CACHE_SIZE',help='number of pages to cache',default='1000'"`
}
func (c *Command) Run(g *globals.Globals) error {
if err := access.Setup(g.ReposPath, g.AdminRepo); err != nil {
return err
}
defer access.Teardown()
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
errChan := make(chan error, 10)
ssh, err := ssh.NewServer(c.SSHHostKeysPath, g.ReposPath, access.AdminRepoPath())
if err != nil {
return err
}
defer ssh.Close()
hgweb, err := hgweb.NewServer(c.HTTPAddr, c.CacheSize)
if err != nil {
return err
}
defer hgweb.Close()
go func() {
if err := ssh.Listen(c.SSHAddr); err != nil {
errChan <- err
}
}()
go func() {
if err := hgweb.Listen(); err != nil {
errChan <- err
}
}()
for {
select {
case err := <-errChan:
if err != nil {
return err
}
case s := <-signalChan:
log.Infof("Captured %v signal. Exiting...", s)
return nil
}
}
}