Inital commits
This commit is contained in:
55
main.go
Normal file
55
main.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/mmcdole/gofeed"
|
||||
)
|
||||
|
||||
//go:embed version
|
||||
var version string
|
||||
|
||||
type Data struct {
|
||||
Version string
|
||||
Articles []Article
|
||||
}
|
||||
|
||||
type Article struct {
|
||||
Title string
|
||||
Link string
|
||||
}
|
||||
|
||||
var data Data = Data{
|
||||
Version: version,
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", newsHandler)
|
||||
|
||||
fp := gofeed.NewParser()
|
||||
feed, _ := fp.ParseURL("https://rss.nytimes.com/services/xml/rss/nyt/US.xml")
|
||||
|
||||
for i, v := range feed.Items {
|
||||
data.Articles = append(data.Articles, Article{
|
||||
Title: v.Title,
|
||||
Link: v.Link,
|
||||
})
|
||||
|
||||
if i == 9 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user