From 2cad5c9f1b59233b11c979658287acb6c8187c8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Wojdy=C5=82o?= Date: Mon, 3 Dec 2018 17:40:35 +0000 Subject: [PATCH] =?UTF-8?q?Usu=C5=84=20'gotut.go'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gotut.go | 62 -------------------------------------------------------- 1 file changed, 62 deletions(-) delete mode 100755 gotut.go diff --git a/gotut.go b/gotut.go deleted file mode 100755 index 28527cb..0000000 --- a/gotut.go +++ /dev/null @@ -1,62 +0,0 @@ -package main - -import ( - "encoding/xml" - "fmt" - "html/template" - "io/ioutil" - "net/http" -) - -type NewsMap struct { - Keyword string - Location string -} - -type NewsAggPage struct { - Title string - News map[string]NewsMap -} - -type Sitemapindex struct { - Locations []string `xml:"sitemap>loc"` -} - -type News struct { - Titles []string `xml:"url>news>title"` - Keywords []string `xml:"url>news>keywords"` - Locations []string `xml:"url>loc"` -} - -func indexHandler(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "

Whoa, Go is neat!

") -} - -func newsAggHandler(w http.ResponseWriter, r *http.Request) { - var s Sitemapindex - var n News - resp, _ := http.Get("https://www.washingtonpost.com/news-sitemap-index.xml") - bytes, _ := ioutil.ReadAll(resp.Body) - xml.Unmarshal(bytes, &s) - news_map := make(map[string]NewsMap) - - for _, Location := range s.Locations { - resp, _ := http.Get(Location) - bytes, _ := ioutil.ReadAll(resp.Body) - xml.Unmarshal(bytes, &n) - - for idx, _ := range n.Keywords { - news_map[n.Titles[idx]] = NewsMap{n.Keywords[idx], n.Locations[idx]} - } - } - - p := NewsAggPage{Title: "Amazing News Aggregator", News: news_map} - t, _ := template.ParseFiles("newsaggtemplate.html") - t.Execute(w, p) -} - -func main() { - //http.HandleFunc("/", indexHandler) - http.HandleFunc("/", newsAggHandler) - http.ListenAndServe(":8000", nil) -}