69 lines
1.2 KiB
Go
69 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
_ "embed"
|
|
"fmt"
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
"wire/internal/api"
|
|
"wire/internal/config"
|
|
)
|
|
|
|
//go:embed version
|
|
var version string
|
|
|
|
type Data struct {
|
|
Version string
|
|
Quote template.HTML
|
|
Ramblings string
|
|
Articles []Article
|
|
Config config.Config
|
|
}
|
|
|
|
type Article struct {
|
|
Title string
|
|
Link string
|
|
}
|
|
|
|
type Quote struct {
|
|
Q string `json:"q"`
|
|
A string `json:"a"`
|
|
H string `json:"h"`
|
|
}
|
|
|
|
var data Data = Data{
|
|
Version: version,
|
|
}
|
|
|
|
func main() {
|
|
http.HandleFunc("/", newsHandler)
|
|
|
|
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,
|
|
})
|
|
}
|
|
|
|
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))
|
|
}
|
|
|
|
func newsHandler(w http.ResponseWriter, r *http.Request) {
|
|
tmpl := template.Must(template.ParseFiles("templates/main.html"))
|
|
|
|
tmpl.ExecuteTemplate(w, "main.html", data)
|
|
}
|