grim/hgkeeper

285592accfa6
Parents 0e9bc5f6e28f
Children 476489023b40
Convert hgweb from esc to the stdlib embed package
--- a/hgweb/files.go Mon Mar 07 01:07:26 2022 -0600
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,257 +0,0 @@
-// Code generated by "esc -o files.go -pkg hgweb -include files\/.+ -prefix files/ ."; DO NOT EDIT.
-
-package hgweb
-
-import (
- "bytes"
- "compress/gzip"
- "encoding/base64"
- "fmt"
- "io"
- "io/ioutil"
- "net/http"
- "os"
- "path"
- "sync"
- "time"
-)
-
-type _escLocalFS struct{}
-
-var _escLocal _escLocalFS
-
-type _escStaticFS struct{}
-
-var _escStatic _escStaticFS
-
-type _escDirectory struct {
- fs http.FileSystem
- name string
-}
-
-type _escFile struct {
- compressed string
- size int64
- modtime int64
- local string
- isDir bool
-
- once sync.Once
- data []byte
- name string
-}
-
-func (_escLocalFS) Open(name string) (http.File, error) {
- f, present := _escData[path.Clean(name)]
- if !present {
- return nil, os.ErrNotExist
- }
- return os.Open(f.local)
-}
-
-func (_escStaticFS) prepare(name string) (*_escFile, error) {
- f, present := _escData[path.Clean(name)]
- if !present {
- return nil, os.ErrNotExist
- }
- var err error
- f.once.Do(func() {
- f.name = path.Base(name)
- if f.size == 0 {
- return
- }
- var gr *gzip.Reader
- b64 := base64.NewDecoder(base64.StdEncoding, bytes.NewBufferString(f.compressed))
- gr, err = gzip.NewReader(b64)
- if err != nil {
- return
- }
- f.data, err = ioutil.ReadAll(gr)
- })
- if err != nil {
- return nil, err
- }
- return f, nil
-}
-
-func (fs _escStaticFS) Open(name string) (http.File, error) {
- f, err := fs.prepare(name)
- if err != nil {
- return nil, err
- }
- return f.File()
-}
-
-func (dir _escDirectory) Open(name string) (http.File, error) {
- return dir.fs.Open(dir.name + name)
-}
-
-func (f *_escFile) File() (http.File, error) {
- type httpFile struct {
- *bytes.Reader
- *_escFile
- }
- return &httpFile{
- Reader: bytes.NewReader(f.data),
- _escFile: f,
- }, nil
-}
-
-func (f *_escFile) Close() error {
- return nil
-}
-
-func (f *_escFile) Readdir(count int) ([]os.FileInfo, error) {
- if !f.isDir {
- return nil, fmt.Errorf(" escFile.Readdir: '%s' is not directory", f.name)
- }
-
- fis, ok := _escDirs[f.local]
- if !ok {
- return nil, fmt.Errorf(" escFile.Readdir: '%s' is directory, but we have no info about content of this dir, local=%s", f.name, f.local)
- }
- limit := count
- if count <= 0 || limit > len(fis) {
- limit = len(fis)
- }
-
- if len(fis) == 0 && count > 0 {
- return nil, io.EOF
- }
-
- return fis[0:limit], nil
-}
-
-func (f *_escFile) Stat() (os.FileInfo, error) {
- return f, nil
-}
-
-func (f *_escFile) Name() string {
- return f.name
-}
-
-func (f *_escFile) Size() int64 {
- return f.size
-}
-
-func (f *_escFile) Mode() os.FileMode {
- return 0
-}
-
-func (f *_escFile) ModTime() time.Time {
- return time.Unix(f.modtime, 0)
-}
-
-func (f *_escFile) IsDir() bool {
- return f.isDir
-}
-
-func (f *_escFile) Sys() interface{} {
- return f
-}
-
-// FS returns a http.Filesystem for the embedded assets. If useLocal is true,
-// the filesystem's contents are instead used.
-func FS(useLocal bool) http.FileSystem {
- if useLocal {
- return _escLocal
- }
- return _escStatic
-}
-
-// Dir returns a http.Filesystem for the embedded assets on a given prefix dir.
-// If useLocal is true, the filesystem's contents are instead used.
-func Dir(useLocal bool, name string) http.FileSystem {
- if useLocal {
- return _escDirectory{fs: _escLocal, name: name}
- }
- return _escDirectory{fs: _escStatic, name: name}
-}
-
-// FSByte returns the named file from the embedded assets. If useLocal is
-// true, the filesystem's contents are instead used.
-func FSByte(useLocal bool, name string) ([]byte, error) {
- if useLocal {
- f, err := _escLocal.Open(name)
- if err != nil {
- return nil, err
- }
- b, err := ioutil.ReadAll(f)
- _ = f.Close()
- return b, err
- }
- f, err := _escStatic.prepare(name)
- if err != nil {
- return nil, err
- }
- return f.data, nil
-}
-
-// FSMustByte is the same as FSByte, but panics if name is not present.
-func FSMustByte(useLocal bool, name string) []byte {
- b, err := FSByte(useLocal, name)
- if err != nil {
- panic(err)
- }
- return b
-}
-
-// FSString is the string version of FSByte.
-func FSString(useLocal bool, name string) (string, error) {
- b, err := FSByte(useLocal, name)
- return string(b), err
-}
-
-// FSMustString is the string version of FSMustByte.
-func FSMustString(useLocal bool, name string) string {
- return string(FSMustByte(useLocal, name))
-}
-
-var _escData = map[string]*_escFile{
-
- "/hgweb.cgi": {
- name: "hgweb.cgi",
- local: "files/hgweb.cgi",
- size: 213,
- modtime: 1586835329,
- compressed: `
-H4sIAAAAAAAC/1yOMWvDMBBG9/sVVzrYhiIPHYuXlsbJkowZgyTL8oF0ErIdE4z/eyBOIMn4+N6D7/Oj
-HPtUKuLS8BnjZegCf4MO3JLFClU2z2JbH/9/T3+H/WZXL0sG0Kbg0Zukx0TSIfkY0oCN8ZKbFX5eSBiW
-ypm8eCtFZyejHv0NvnDqLWlLADJGR1oOFBirdc3XYwXcJeHkyLrLn9QC4BoAAP//rsn9QdUAAAA=
-`,
- },
-
- "/hgweb.config": {
- name: "hgweb.config",
- local: "files/hgweb.config",
- size: 151,
- modtime: 1587785601,
- compressed: `
-H4sIAAAAAAAC/0zNwQqCQBCH8fs8xR+iaz1Bh1pMvRRk0CFCTCcdWHZhd9YI8d0jIuj4HT5+S3GtTR1j
-mlZFfsl29daYrKpqczzsy3yeia5Pvt8oaqPSpmCxwfobRAv8dh0YUZTReveQHraJiiZCFHHwyXbwI4cg
-HYNHDi8dxPX0r1flOauL/GQ+5jsAAP//rRIl8JcAAAA=
-`,
- },
-
- "/": {
- name: "/",
- local: `.`,
- isDir: true,
- },
-
- "/files": {
- name: "files",
- local: `files`,
- isDir: true,
- },
-}
-
-var _escDirs = map[string][]os.FileInfo{
-
- ".": {},
-
- "files": {
- _escData["/hgweb.cgi"],
- _escData["/hgweb.config"],
- },
-}
--- a/hgweb/hgweb.go Mon Mar 07 01:07:26 2022 -0600
+++ b/hgweb/hgweb.go Mon Mar 07 01:20:11 2022 -0600
@@ -2,6 +2,7 @@
package hgweb
import (
+ "embed"
"io/ioutil"
"net/http"
"net/http/cgi"
@@ -16,6 +17,9 @@
hgkHttp "keep.imfreedom.org/grim/hgkeeper/http"
)
+//go:embed files/*
+var files embed.FS
+
type Server struct {
listenAddr string
server *http.Server
@@ -48,7 +52,12 @@
}
defer configPath.Close()
- t := template.Must(template.New("hgweb-config").Parse(FSMustString(false, "/hgweb.config")))
+ 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"),
@@ -77,7 +86,12 @@
}
// create a template based for the cgi file
- t := template.Must(template.New("cgi").Parse(FSMustString(false, "/hgweb.cgi")))
+ 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{