grim/hgkeeper

Parents f3e037634b20
Children 6b8a0acef0f0
Serve all static files from /static and handle it from go instead of passing it to python. Fixes HGKEEPER-7
--- a/access/access.go Thu Apr 23 05:01:09 2020 -0500
+++ b/access/access.go Fri Apr 24 22:34:27 2020 -0500
@@ -29,7 +29,7 @@
adminRepoName = adminRepo
adminRepoPath = filepath.Join(reposPath, adminRepo)
- configPath, err := ioutil.TempFile("", "hgkeeper-hgweb-*.config")
+ configPath, err := ioutil.TempFile("", "hgkeeper-hgweb-access-*.config")
if err != nil {
return err
}
@@ -75,7 +75,7 @@
return err
}
- if err := refreshHgWeb(reposPath, adminRepoPath); err != nil {
+ if err := refreshHgWeb(reposPath); err != nil {
return err
}
--- a/access/hgweb.go Thu Apr 23 05:01:09 2020 -0500
+++ b/access/hgweb.go Fri Apr 24 22:34:27 2020 -0500
@@ -8,19 +8,13 @@
"strings"
)
-func refreshHgWeb(reposPath, adminRepoPath string) error {
+func refreshHgWeb(reposPath string) error {
fp, err := os.OpenFile(hgwebConfigPath, os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer fp.Close()
- absAdminRepoPath, err := filepath.Abs(adminRepoPath)
- if err != nil {
- return err
- }
- fmt.Fprintf(fp, "%%include %s\n\n", filepath.Join(absAdminRepoPath, "site.hgrc"))
-
fmt.Fprintf(fp, "[paths]\n")
absReposPath, err := filepath.Abs(reposPath)
--- a/hg/hg.go Thu Apr 23 05:01:09 2020 -0500
+++ b/hg/hg.go Fri Apr 24 22:34:27 2020 -0500
@@ -47,6 +47,15 @@
}
}
+func (c *Command) Exec() (string, error) {
+ c.Setup()
+ defer c.Teardown()
+
+ raw, err := c.Cmd().CombinedOutput()
+
+ return string(raw), err
+}
+
func (c *Command) Cmd() *exec.Cmd {
return c.cmd
}
@@ -84,3 +93,14 @@
cmd: exec.Command("hg", args...),
}
}
+
+func Config(setting string) *Command {
+ args := []string{
+ "config",
+ setting,
+ }
+
+ return &Command{
+ cmd: exec.Command("hg", args...),
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/hg/hgweb.go Fri Apr 24 22:34:27 2020 -0500
@@ -0,0 +1,43 @@
+package hg
+
+import (
+ "fmt"
+ "os/exec"
+ "path/filepath"
+ "strings"
+
+ log "github.com/sirupsen/logrus"
+)
+
+func TemplatesDir() (string, error) {
+ templatesDir, err := Config("web.templates").Exec()
+
+ if err != nil || templatesDir == "" {
+ if err != nil {
+ log.Warnf("failed to get web.templates config setting: %v", err)
+ }
+
+ args := []string{
+ "/usr/bin/env",
+ "python3",
+ "-c",
+ "import mercurial;print(mercurial.__path__[0])",
+ }
+
+ cmd := exec.Command(args[0], args[1:]...)
+ raw, err := cmd.CombinedOutput()
+
+ if err != nil {
+ return "", err
+ }
+
+ templatesDir = strings.TrimSpace(string(raw))
+ if templatesDir == "" {
+ return "", fmt.Errorf("failed to find the templates directory")
+ }
+
+ templatesDir = filepath.Join(templatesDir, "templates")
+ }
+
+ return templatesDir, nil
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/hgweb/files/hgweb.config Fri Apr 24 22:34:27 2020 -0500
@@ -0,0 +1,8 @@
+%include {{.HGWEB_ACCESS_CONFIG}}
+
+[web]
+staticurl = /static
+
+# include the site config last as it should override everything
+%include {{.SITE_HGRC}}
+
--- a/hgweb/hgweb.go Thu Apr 23 05:01:09 2020 -0500
+++ b/hgweb/hgweb.go Fri Apr 24 22:34:27 2020 -0500
@@ -6,16 +6,20 @@
"net/http"
"net/http/cgi"
"os"
+ "path/filepath"
"text/template"
log "github.com/sirupsen/logrus"
"bitbucket.org/rw_grim/hgkeeper/access"
+ "bitbucket.org/rw_grim/hgkeeper/hg"
)
type Server struct {
listenAddr string
server *http.Server
+
+ configPath string
cgiPath string
cacheSize int
@@ -31,6 +35,28 @@
}, nil
}
+func (s *Server) createConfig() error {
+ configPath, err := ioutil.TempFile("", "hgkeeper-hgweb-*.config")
+ if err != nil {
+ return err
+ }
+ defer configPath.Close()
+
+ t := template.Must(template.New("hgweb-config").Parse(FSMustString(false, "/hgweb.config")))
+ data := map[string]string{
+ "HGWEB_ACCESS_CONFIG": access.HgwebConfigPath(),
+ "SITE_HGRC": filepath.Join(access.AdminRepoPath(), "site.hgrc"),
+ }
+
+ if err := t.Execute(configPath, data); err != nil {
+ return err
+ }
+
+ s.configPath = configPath.Name()
+
+ return nil
+}
+
func (s *Server) createCGI() error {
cgiPath, err := ioutil.TempFile("", "hgkeeper-hgweb-*.cgi")
if err != nil {
@@ -48,7 +74,7 @@
// create our data
data := map[string]string{
- "HGWEB_CONFIG": access.HgwebConfigPath(),
+ "HGWEB_CONFIG": s.configPath,
}
if err := t.Execute(cgiPath, data); err != nil {
@@ -61,6 +87,10 @@
}
func (s *Server) Listen() error {
+ if err := s.createConfig(); err != nil {
+ return err
+ }
+
if err := s.createCGI(); err != nil {
return err
}
@@ -70,11 +100,19 @@
return err
}
- s.server.Handler = Logger(
- cache.middleware(
- &cgi.Handler{Path: s.cgiPath},
- ),
- )
+ templatesDir, err := hg.TemplatesDir()
+ if err != nil {
+ return err
+ }
+ staticPath := filepath.Join(templatesDir, "static")
+ log.Infof("serving static files from %s", staticPath)
+ fileServer := http.FileServer(http.Dir(staticPath))
+
+ mux := http.NewServeMux()
+ mux.Handle("/static/", http.StripPrefix("/static", fileServer))
+ mux.Handle("/", &cgi.Handler{Path: s.cgiPath})
+
+ s.server.Handler = Logger(cache.middleware(mux))
log.Infof("http listening on %s", s.listenAddr)
--- a/ssh/commands/run.go Thu Apr 23 05:01:09 2020 -0500
+++ b/ssh/commands/run.go Fri Apr 24 22:34:27 2020 -0500
@@ -16,6 +16,7 @@
if err := hgCmd.Setup(); err != nil {
return err
}
+ defer hgCmd.Teardown()
stdout, err := cmd.StdoutPipe()
if err != nil {