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