diff --git a/CHANGELOG b/CHANGELOG index b06524c..c29fc4d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +1.0.2: +- Support bookmarks + 1.0.1: - Add option to allow local file access - Display navigation bar in pages diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 19139c7..df685f8 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -3,6 +3,14 @@ default. You may specify a different location via the `--config` argument. # Configuration options +## Bookmarks + +Bookmarks are defined as a list of URLs and corresponding label. + +Defining bookmarks manually via configuration file is possible, however it is +not required as the gmitohtml configuration file is updated when bookmarks are +modified using the web interface. + ## Client certificates Client certificates may be specified via the `Certs` option. @@ -30,6 +38,10 @@ For example, to view `/home/dioscuri/sites/gemlog/index.gmi`, navigate to # Example config.yaml ```yaml +bookmarks: + gemini://gemini.circumlunar.space/: Gemini protocol + gemini://gus.guru/: GUS - Gemini Universal Search + certs: astrobotany.mozz.us: cert: /home/dioscuri/.config/gmitohtml/astrobotany.mozz.us.crt diff --git a/config.go b/config.go index 0e19dcf..f750142 100644 --- a/config.go +++ b/config.go @@ -3,8 +3,12 @@ package main import ( "crypto/tls" "errors" + "fmt" "io/ioutil" + "os" + "path" + "gitlab.com/tslocum/gmitohtml/pkg/gmitohtml" "gopkg.in/yaml.v3" ) @@ -16,13 +20,25 @@ type certConfig struct { } type appConfig struct { + Bookmarks map[string]string + Certs map[string]*certConfig } var config = &appConfig{ + Bookmarks: make(map[string]string), + Certs: make(map[string]*certConfig), } +func defaultConfigPath() string { + homedir, err := os.UserHomeDir() + if err == nil && homedir != "" { + return path.Join(homedir, ".config", "gmitohtml", "config.yaml") + } + return "" +} + func readconfig(configPath string) error { if configPath == "" { return errors.New("file unspecified") @@ -42,3 +58,20 @@ func readconfig(configPath string) error { return nil } + +func saveConfig(configPath string) error { + config.Bookmarks = gmitohtml.GetBookmarks() + + out, err := yaml.Marshal(config) + if err != nil { + return fmt.Errorf("failed to marshal configuration: %s", err) + } + + os.MkdirAll(path.Dir(configPath), 0755) // Ignore error + + err = ioutil.WriteFile(configPath, out, 0644) + if err != nil { + return fmt.Errorf("failed to save configuration to %s: %s", configPath, err) + } + return nil +} diff --git a/main.go b/main.go index da503f0..80fbf3f 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,6 @@ import ( "net/url" "os" "os/exec" - "path" "runtime" "gitlab.com/tslocum/gmitohtml/pkg/gmitohtml" @@ -43,20 +42,26 @@ func main() { // TODO option to include response header in page flag.Parse() + defaultConfig := defaultConfigPath() if configFile == "" { - homedir, err := os.UserHomeDir() - if err == nil && homedir != "" { - defaultConfig := path.Join(homedir, ".config", "gmitohtml", "config.yaml") - if _, err := os.Stat(defaultConfig); !os.IsNotExist(err) { - configFile = defaultConfig - } - } + configFile = defaultConfig } if configFile != "" { - err := readconfig(configFile) - if err != nil { - log.Fatalf("failed to read configuration file at %s: %v\nSee CONFIGURATION.md for information on configuring gmitohtml", configFile, err) + var configExists bool + if _, err := os.Stat(defaultConfig); !os.IsNotExist(err) { + configExists = true + } + + if configExists || configFile != defaultConfig { + err := readconfig(configFile) + if err != nil { + log.Fatalf("failed to read configuration file at %s: %v\nSee CONFIGURATION.md for information on configuring gmitohtml", configFile, err) + } + + for u, label := range config.Bookmarks { + gmitohtml.AddBookmark(u, label) + } } } @@ -78,6 +83,15 @@ func main() { } if daemon != "" { + gmitohtml.SetOnBookmarksChanged(func() { + config.Bookmarks = gmitohtml.GetBookmarks() + + err := saveConfig(configFile) + if err != nil { + log.Fatal(err) + } + }) + err := gmitohtml.StartDaemon(daemon, allowFile) if err != nil { log.Fatal(err) diff --git a/pkg/gmitohtml/assets.go b/pkg/gmitohtml/assets.go index dc0a1e5..33d55fe 100644 --- a/pkg/gmitohtml/assets.go +++ b/pkg/gmitohtml/assets.go @@ -12,22 +12,15 @@ const pageHeader = `
-