grim/hgkeeper

Split the server pieces out of hgweb.go

2022-03-07, Gary Kramlich
334e63a51155
Split the server pieces out of hgweb.go
package hgweb
import (
"embed"
"io/ioutil"
"path/filepath"
"text/template"
"keep.imfreedom.org/grim/hgkeeper/access"
)
//go:embed files/*
var files embed.FS
func (s *Server) createConfig() error {
configPath, err := ioutil.TempFile("", "hgkeeper-hgweb-*.config")
if err != nil {
return err
}
defer configPath.Close()
contents, err := files.ReadFile("files/hgweb.config")
if err != nil {
return err
}
t := template.Must(template.New("hgweb-config").Parse(string(contents)))
data := map[string]string{
"HGWEB_ACCESS_CONFIG": access.HgwebConfigPath(),
"SITE_HGRC": filepath.Join(access.AdminRepoPath(), "site.hgrc"),
"GLOBAL_HGRC": filepath.Join(access.AdminRepoPath(), "config/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 {
return err
}
defer cgiPath.Close()
// make the cgi file executable
if err := cgiPath.Chmod(0755); err != nil {
return err
}
// create a template based for the cgi file
contents, err := files.ReadFile("files/hgweb.cgi")
if err != nil {
return err
}
t := template.Must(template.New("cgi").Parse(string(contents)))
// create our data
data := map[string]string{
"HGWEB_CONFIG": s.configPath,
}
if err := t.Execute(cgiPath, data); err != nil {
return err
}
s.cgiPath = cgiPath.Name()
return nil
}