read header.gmi and set header variable to that or to the default

This commit is contained in:
nytpu
2021-03-22 08:19:41 -06:00
parent 1eda2eaea8
commit 8d9892b0a1
+29
View File
@@ -28,9 +28,14 @@ var Data = FullData{
// Maps are created in init() // Maps are created in init()
} }
// the header of feeds.gmi.
// set in Init, either to the contents of header.gmi or to the default string
var Header string
// have to do this instead of the automatically called init() because // have to do this instead of the automatically called init() because
// initialization stuff requires parsing of command line args first // initialization stuff requires parsing of command line args first
func Init(dataPath string) error { func Init(dataPath string) error {
// initialize comitium.json & data maps
f, err := os.Open(filepath.Join(dataPath, "comitium.json")) f, err := os.Open(filepath.Join(dataPath, "comitium.json"))
if err == nil { if err == nil {
// File exists and could be opened // File exists and could be opened
@@ -59,6 +64,30 @@ func Init(dataPath string) error {
if Data.Pages == nil { if Data.Pages == nil {
Data.Pages = make(map[string]*Page) Data.Pages = make(map[string]*Page)
} }
// initialize header.gmi
f, err = os.Open(filepath.Join(dataPath, "header.gmi"))
if err == nil {
fi, err := f.Stat()
if err == nil && fi.Size() > 0 {
headerBytes, err := ioutil.ReadAll(f)
f.Close()
if err != nil {
return fmt.Errorf("read header.gmi error: %w", err)
}
Header = string(headerBytes)
}
f.Close()
} else if !os.IsNotExist(err) {
return fmt.Errorf("open header.gmi error: %w", err)
}
if Header == "" {
Header = `# comitium subscriptions
`
}
return nil return nil
} }