Compare commits
3 Commits
a2a0978f77
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 505a8c1407 | |||
| 7049c22988 | |||
| 6c169b7fd7 |
66
main.go
66
main.go
@@ -8,6 +8,7 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
@@ -20,7 +21,7 @@ type Item struct {
|
||||
URL string `json:"url" db:"url"`
|
||||
Title string `json:"title" db:"title"`
|
||||
Description string `json:"description" db:"description"`
|
||||
ImageURL string `json:"imageUrl" db:"image_url"`
|
||||
Image *string `json:"image" db:"image"`
|
||||
CreatedAt time.Time `json:"createdAt" db:"created_at"`
|
||||
UpdatedAt time.Time `json:"updatedAt" db:"updated_at"`
|
||||
}
|
||||
@@ -28,7 +29,7 @@ type Item struct {
|
||||
type ArticleMetadata struct {
|
||||
Title string
|
||||
Description string
|
||||
ImageURL string
|
||||
Image *string
|
||||
}
|
||||
|
||||
func getArticleMetadata(url string) (*ArticleMetadata, error) {
|
||||
@@ -38,6 +39,12 @@ func getArticleMetadata(url string) (*ArticleMetadata, error) {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 400 {
|
||||
return &ArticleMetadata{
|
||||
Title: url,
|
||||
}, nil
|
||||
}
|
||||
|
||||
doc, err := goquery.NewDocumentFromReader(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse page html: %v", err)
|
||||
@@ -50,12 +57,12 @@ func getArticleMetadata(url string) (*ArticleMetadata, error) {
|
||||
|
||||
description := doc.Find(`head>meta[name="og:description"]`).AttrOr("content", "")
|
||||
|
||||
imageUrl := doc.Find(`head>meta[name="og:image"]`).AttrOr("content", "")
|
||||
if imageUrl == "" {
|
||||
image := doc.Find(`head>meta[name="og:image"]`).AttrOr("content", "")
|
||||
if image == "" {
|
||||
doc.Find("img").Each(func(i int, s *goquery.Selection) {
|
||||
src := s.AttrOr("src", "")
|
||||
if src != "" {
|
||||
imageUrl = src
|
||||
image = src
|
||||
return
|
||||
}
|
||||
})
|
||||
@@ -64,7 +71,7 @@ func getArticleMetadata(url string) (*ArticleMetadata, error) {
|
||||
return &ArticleMetadata{
|
||||
Title: title,
|
||||
Description: description,
|
||||
ImageURL: imageUrl,
|
||||
Image: &image,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -91,7 +98,7 @@ CREATE TABLE IF NOT EXISTS items (
|
||||
url varchar not null unique,
|
||||
title varchar not null,
|
||||
description varchar,
|
||||
image_url varchar not null,
|
||||
image varchar default null,
|
||||
created_at datetime default current_timestamp,
|
||||
updated_at datetime default current_timestamp
|
||||
);
|
||||
@@ -127,11 +134,11 @@ END;
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := db.NamedExec("INSERT INTO items (url, title, description, image_url) VALUES (:url, :title, :description, :image_url)", map[string]any{
|
||||
if _, err := db.NamedExec("INSERT INTO items (url, title, description, image) VALUES (:url, :title, :description, :image)", map[string]any{
|
||||
"url": pageUrl,
|
||||
"title": meta.Title,
|
||||
"description": meta.Description,
|
||||
"image_url": meta.ImageURL,
|
||||
"image": meta.Image,
|
||||
}); err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to add item to db: %v", err), 500)
|
||||
return
|
||||
@@ -162,6 +169,47 @@ END;
|
||||
}
|
||||
})
|
||||
|
||||
mux.HandleFunc("GET /items/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.Atoi(r.PathValue("id"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
row := db.QueryRowx("SELECT * FROM items WHERE id = ?", id)
|
||||
if row.Err() != nil {
|
||||
http.Error(w, fmt.Sprintf("item not found: %v", row.Err()), 404)
|
||||
return
|
||||
}
|
||||
|
||||
item := &Item{}
|
||||
if err := row.StructScan(item); err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to scan item to struct: %v", err), 500)
|
||||
return
|
||||
}
|
||||
|
||||
tmpl.ExecuteTemplate(w, "item.html", struct {
|
||||
Item *Item
|
||||
}{
|
||||
Item: item,
|
||||
})
|
||||
})
|
||||
|
||||
mux.HandleFunc("POST /items/{id}/delete", func(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.Atoi(r.PathValue("id"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := db.Exec("DELETE FROM items WHERE id = ?", id); err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to delete item: %v", err), 500)
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/", http.StatusFound)
|
||||
})
|
||||
|
||||
log.Println("starting http server")
|
||||
if err := http.ListenAndServe(":5000", mux); err != nil {
|
||||
log.Fatalf("failed to start http server: %v\n", err)
|
||||
|
||||
25
views/item.html
Normal file
25
views/item.html
Normal file
@@ -0,0 +1,25 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>{{.Item.Title}}</title>
|
||||
|
||||
<style>
|
||||
.container {
|
||||
max-width: 1200px;
|
||||
width: 100%;
|
||||
margin: 0 auto;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<img src="{{.Item.Image}}" alt="" width="auto" height="100px" />
|
||||
<h2>{{.Item.Title}}</h2>
|
||||
<form method="POST" action="/items/{{.Item.ID}}/delete">
|
||||
<button type="submit">Delete item</button>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user