Start refactor
This commit is contained in:
49
internal/articles/articles.go
Normal file
49
internal/articles/articles.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package articles
|
||||
|
||||
import (
|
||||
"log"
|
||||
"wire/internal/api"
|
||||
)
|
||||
|
||||
type Articles []Article
|
||||
|
||||
type Article struct {
|
||||
Title string
|
||||
Link string
|
||||
Description string
|
||||
Content string
|
||||
}
|
||||
|
||||
type ArticleParser struct {
|
||||
url string
|
||||
limit int
|
||||
}
|
||||
|
||||
func NewParser(url string) *ArticleParser {
|
||||
ap := ArticleParser{
|
||||
url: url,
|
||||
limit: 15,
|
||||
}
|
||||
return &ap
|
||||
}
|
||||
|
||||
func (ap *ArticleParser) GetArticles() (as Articles) {
|
||||
items, err := api.FetchRSS(ap.url)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for i, v := range items {
|
||||
if i == ap.limit {
|
||||
break
|
||||
}
|
||||
as = append(as, Article{
|
||||
Title: v.Title,
|
||||
Link: v.Link,
|
||||
Description: v.Description,
|
||||
Content: v.Content,
|
||||
})
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -2,3 +2,6 @@
|
||||
|
||||
news=true
|
||||
quote=true
|
||||
ramblings=true
|
||||
|
||||
ramblings_path=${XDG_DOCUMENTS_DIR}/wire/
|
||||
|
||||
24
main.go
24
main.go
@@ -2,30 +2,29 @@ package main
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"flag"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"wire/internal/api"
|
||||
"wire/internal/articles"
|
||||
"wire/internal/config"
|
||||
)
|
||||
|
||||
//go:embed version
|
||||
var version string
|
||||
|
||||
var verboseFlag = flag.Bool("verbose", false, "Enable verbose logging")
|
||||
|
||||
type Data struct {
|
||||
Version string
|
||||
Quote template.HTML
|
||||
Ramblings string
|
||||
Articles []Article
|
||||
Articles articles.Articles
|
||||
Config config.Config
|
||||
}
|
||||
|
||||
type Article struct {
|
||||
Title string
|
||||
Link string
|
||||
}
|
||||
|
||||
type Quote struct {
|
||||
Q string `json:"q"`
|
||||
A string `json:"a"`
|
||||
@@ -37,19 +36,14 @@ var data Data = Data{
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", newsHandler)
|
||||
flag.Parse()
|
||||
|
||||
cfg := config.NewConfig()
|
||||
cfg.ReadConfig()
|
||||
data.Config = cfg
|
||||
|
||||
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,
|
||||
})
|
||||
}
|
||||
ap := articles.NewParser("https://rss.nytimes.com/services/xml/rss/nyt/US.xml")
|
||||
data.Articles = ap.GetArticles()
|
||||
|
||||
quote, err := api.FetchJSON[[]Quote]("https://zenquotes.io/api/today")
|
||||
if err != nil {
|
||||
@@ -57,6 +51,8 @@ func main() {
|
||||
}
|
||||
data.Quote = template.HTML(quote[0].H)
|
||||
|
||||
http.HandleFunc("/", newsHandler)
|
||||
|
||||
fmt.Println("wire: starting server at http://127.0.0.1:1337")
|
||||
log.Fatal(http.ListenAndServe("127.0.0.1:1337", nil))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user