93 lines
1.8 KiB
Go
93 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"html"
|
|
"log"
|
|
"net/http"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
// type Nota struct {
|
|
// Medio string `json:"medio"`
|
|
// Title string `json:"title"`
|
|
// Link string `json:"link"`
|
|
// PublicationDate string `json:"publication_date" db:"publication_date"`
|
|
// CreatedAt string `json:"created_at" db:"created_at"`
|
|
// }
|
|
|
|
type fooHandler struct {
|
|
DB *sql.DB
|
|
}
|
|
|
|
func (foo fooHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintf(w, `<!doctype html>
|
|
<meta charset="utf-8">
|
|
<title>Observatorio de medios</title>
|
|
<ul>
|
|
`)
|
|
|
|
rows, err := foo.DB.Query(`
|
|
select medio, title, link, publication_date
|
|
from notas
|
|
where
|
|
title like "%colegio%" or
|
|
title like "%escuela%" or
|
|
title like "%Acosta%" or
|
|
title like "%tomas%" or
|
|
content like "%colegio%" or
|
|
content like "%escuela%" or
|
|
content like "%Acosta%" or
|
|
content like "%tomas%"
|
|
order by publication_date asc
|
|
limit 50
|
|
`)
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var medio string
|
|
var title string
|
|
var link string
|
|
var publication_date string
|
|
err = rows.Scan(&medio, &title, &link, &publication_date)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Fprintf(w, "<li>[%s] <a href=\"%s\">%s</a> %s\n",
|
|
html.EscapeString(medio),
|
|
html.EscapeString(link),
|
|
html.EscapeString(title),
|
|
html.EscapeString(publication_date),
|
|
)
|
|
}
|
|
err = rows.Err()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
fmt.Fprintf(w, `</ul>`)
|
|
}
|
|
|
|
func main() {
|
|
db, err := sql.Open("sqlite3", "./test.db?cache=shared")
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
defer db.Close()
|
|
|
|
db.SetMaxOpenConns(1)
|
|
|
|
http.Handle("/foo", fooHandler{db})
|
|
|
|
go cronologicalFetcher(db)
|
|
|
|
log.Println("Listening in :8080")
|
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
|
}
|