From cbf9f0e695276eea045af9d670f4a6a15f11f428 Mon Sep 17 00:00:00 2001 From: vxclutch Date: Sun, 24 May 2026 13:53:50 -0400 Subject: [PATCH] Same changes --- internal/api/fetch.go | 61 +++++++++++++++++++++++++++++++++ internal/config/conf.sh | 4 +++ internal/config/config.go | 70 ++++++++++++++++++++++++++++++++++++++ internal/config/wrapper.sh | 3 ++ main.go | 39 ++++++++++++++------- templates/main.html | 14 ++++++++ 6 files changed, 178 insertions(+), 13 deletions(-) create mode 100644 internal/api/fetch.go create mode 100644 internal/config/conf.sh create mode 100644 internal/config/config.go create mode 100644 internal/config/wrapper.sh diff --git a/internal/api/fetch.go b/internal/api/fetch.go new file mode 100644 index 0000000..f3d6393 --- /dev/null +++ b/internal/api/fetch.go @@ -0,0 +1,61 @@ +package api + +import ( + "encoding/json" + "fmt" + "io" + "log" + "net/http" + + "github.com/mmcdole/gofeed" +) + +func FetchPlain(url string) string { + response, err := http.Get(url) + if err != nil { + log.Fatal(err) + } + defer response.Body.Close() + + body, err := io.ReadAll(response.Body) + if err != nil { + log.Fatal(err) + } + return string(body) +} + +func FetchJSON[T any](url string) (T, error) { + var result T + + resp, err := http.Get(url) + if err != nil { + return result, err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return result, fmt.Errorf("unexpected status: %s", resp.Status) + } + + body, err := io.ReadAll(resp.Body) + if err != nil { + return result, err + } + + err = json.Unmarshal(body, &result) + if err != nil { + return result, err + } + + return result, nil +} + +func FetchRSS(url string) (ret []gofeed.Item, err error) { + fp := gofeed.NewParser() + feed, err := fp.ParseURL(url) + + for _, v := range feed.Items { + ret = append(ret, *v) + } + return +} diff --git a/internal/config/conf.sh b/internal/config/conf.sh new file mode 100644 index 0000000..15a9180 --- /dev/null +++ b/internal/config/conf.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +news=true +quote=true diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..71896e2 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,70 @@ +package config + +import ( + _ "embed" + "encoding/json" + "log" + "os" + "os/exec" + "path/filepath" +) + +//go:embed conf.sh +var defaultConfig []byte + +//go:embed wrapper.sh +var wrapper string + +type Config struct { + News bool `json:"news"` + Quotes bool `json:"quote"` + Ramblings bool `json:"ramblings"` + Ramblings_Path string `json:"ramblings_path"` +} + +func NewConfig() (c Config) { + c.News = true + c.Quotes = true + c.Ramblings = true + ramblingsDir := os.Getenv("XDG_DOCUMENTS_DIR") + if ramblingsDir == "" { + ramblingsDir = filepath.Join(os.Getenv("HOME"), "Documents") + } + c.Ramblings_Path = filepath.Join(ramblingsDir, "wire") + + return +} + +func (c *Config) ReadConfig() { + configDir := os.Getenv("XDG_CONFIG_HOME") + if configDir == "" { + configDir = filepath.Join(os.Getenv("HOME"), ".config") + } + + fp := filepath.Join(configDir, "wire", "conf.sh") + + dir := filepath.Dir(fp) + if err := os.MkdirAll(dir, 0755); err != nil { + log.Fatal(err) + } + + _, err := os.ReadFile(fp) + if err != nil { + if os.IsNotExist(err) { + if err := os.WriteFile(fp, defaultConfig, 0644); err != nil { + log.Fatal(err) + } + } else { + log.Fatal(err) + } + } + + out, err := exec.Command("sh", "-c", wrapper, "--", fp).Output() + if err != nil { + log.Fatal(err) + } + + if err := json.Unmarshal(out, c); err != nil { + log.Fatal(err) + } +} diff --git a/internal/config/wrapper.sh b/internal/config/wrapper.sh new file mode 100644 index 0000000..793d06c --- /dev/null +++ b/internal/config/wrapper.sh @@ -0,0 +1,3 @@ +#!/bin/sh +. $1 +printf '{"news":%s,"quote":%s,"ramblings":%s,"ramblings_path":"%s"}\n' "$news" "$quote" "$ramblings" "$ramblings_path" diff --git a/main.go b/main.go index d0481f6..b257eb3 100644 --- a/main.go +++ b/main.go @@ -6,21 +6,30 @@ import ( "html/template" "log" "net/http" - - "github.com/mmcdole/gofeed" + "wire/internal/api" + "wire/internal/config" ) //go:embed version var version string type Data struct { - Version string - Articles []Article + Version string + Quote template.HTML + Ramblings string + Articles []Article + Config config.Config } type Article struct { Title string - Link string + Link string +} + +type Quote struct { + Q string `json:"q"` + A string `json:"a"` + H string `json:"h"` } var data Data = Data{ @@ -30,20 +39,24 @@ var data Data = Data{ func main() { http.HandleFunc("/", newsHandler) - fp := gofeed.NewParser() - feed, _ := fp.ParseURL("https://rss.nytimes.com/services/xml/rss/nyt/US.xml") + cfg := config.NewConfig() + cfg.ReadConfig() + data.Config = cfg - for i, v := range feed.Items { + items, err := api.FetchRSS("https://rss.nytimes.com/services/xml/rss/nyt/US.xml") + for _, v := range items { data.Articles = append(data.Articles, Article{ Title: v.Title, - Link: v.Link, + Link: v.Link, }) - - if i == 9 { - break - } } + quote, err := api.FetchJSON[[]Quote]("https://zenquotes.io/api/today") + if err != nil { + log.Fatal(err) + } + data.Quote = template.HTML(quote[0].H) + fmt.Println("wire: starting server at http://127.0.0.1:1337") log.Fatal(http.ListenAndServe("127.0.0.1:1337", nil)) } diff --git a/templates/main.html b/templates/main.html index 1178361..444a5d1 100644 --- a/templates/main.html +++ b/templates/main.html @@ -6,12 +6,26 @@ Wire News

Wire {{.Version}}

+ +{{if .Config.News}}

Latest News

+{{end}} + +{{if .Config.Quotes}} +

Fortification of the Mind

+{{.Quote}} +{{end}} + +{{if .Config.Ramble}} +

Schizo Ramblings

+{{.Ramblings}} +{{end}} +