diff --git a/cmd/web/web.go b/cmd/web/web.go new file mode 100644 index 0000000..9ecf60c --- /dev/null +++ b/cmd/web/web.go @@ -0,0 +1,68 @@ +package main + +import ( + "fmt" + "gitlab.com/kbr4/svevijesti/internal/database" + "html/template" + "io/ioutil" + "net/http" + "path/filepath" + "strings" + "time" +) + +var tPath = "./web/tpl/" +var dPath = "./web/data/" + +var templateDirs = []string{"./web/tpl", "./web/data"} +var templates *template.Template + +func getTemplates() (templates *template.Template, err error) { + var allFiles []string + for _, dir := range templateDirs { + files2, _ := ioutil.ReadDir(dir) + for _, file := range files2 { + filename := file.Name() + if strings.HasSuffix(filename, ".html") { + filePath := filepath.Join(dir, filename) + fmt.Println("Template found: ", filePath) + allFiles = append(allFiles, filePath) + } + } + } + + templates, err = template.New("").ParseFiles(allFiles...) + return +} + +func init() { + templates, _ = getTemplates() +} + +func rootHandler(wr http.ResponseWriter, req *http.Request) { + title := "Pocetna" + store, err := database.Connect() + if err != nil { + http.Error(wr, err.Error(), http.StatusInternalServerError) + } + + articles, err := database.ArticlesForDay(store, time.Now()) + if err != nil { + http.Error(wr, err.Error(), http.StatusInternalServerError) + } + + data := map[string]interface{}{ + "title": title, + "articles": articles, + } + + err = templates.ExecuteTemplate(wr, "homeHTML", data) + if err != nil { + http.Error(wr, err.Error(), http.StatusInternalServerError) + } +} + +func main() { + http.HandleFunc("/", rootHandler) + http.ListenAndServe(":8080", nil) +} diff --git a/internal/database/articles.go b/internal/database/articles.go new file mode 100644 index 0000000..65c6df2 --- /dev/null +++ b/internal/database/articles.go @@ -0,0 +1,88 @@ +package database + +import ( + "fmt" + _ "github.com/lib/pq" + "gitlab.com/kbr4/svevijesti/internal/model" + "math" + "time" +) + +func InsertArticle(store *Store, article model.ScrapedArticle) (err error) { + query := ` + INSERT INTO articles + (title, content, slug, original_url, source_id) + VALUES + ($1,$2,$3,$4,$5);` + + _, err = store.Exec(query, article.Title, article.Content, article.Slug, article.OriginalUrl, article.SourceId) + if err != nil { + return err + } + + return nil +} + +func IsSaved(store *Store, url string) bool { + + exists := false + query, err := store.Prepare(` + select exists(select 1 from articles where original_url = $1); + `) + if err != nil { + panic(err) + } + + row := query.QueryRow(url) + err = row.Scan(&exists) + if err != nil { + panic(err) + } + return exists +} + +func ArticlesForDay(store *Store, day time.Time) (articles []model.DisplayArticle, err error) { + + result := []model.DisplayArticle{} + query, err := store.Prepare(` + select id,title, content, slug, original_url, source_id, created_at from articles where created_at > $1 and created_at < $2 and LENGTH(content) > 10 order by id desc; + `) + if err != nil { + return result, err + } + + tomorrow := day.AddDate(0, 0, 1) + todayDate := day.Format("2006-01-02") + tomorrowDate := tomorrow.Format("2006-01-02") + + rows, err := query.Query(todayDate, tomorrowDate) + if err != nil { + return result, err + } + defer rows.Close() + + for rows.Next() { + r := model.DisplayArticle{} + err = rows.Scan(&r.ID, &r.Title, &r.Content, &r.Slug, &r.OriginalUrl, &r.SourceId, &r.CreatedAt) + if err != nil { + return result, err + } + + ago := time.Now().Sub(r.CreatedAt) + hours := ago.Hours() + + if hours < 1 { + r.FormatedCreatedAt = fmt.Sprintf("Prije %d minuta.", int(math.Floor(ago.Minutes()))) + + } else if hours > 24 { + r.FormatedCreatedAt = r.CreatedAt.Format("01.02.2006. 15:04:05") + } else { + r.FormatedCreatedAt = fmt.Sprintf("Prije %d sati.", int(math.Floor(hours))) + } + r.SourceName = model.SourceName(r.SourceId) + + result = append(result, r) + } + + return result, nil +} diff --git a/internal/database/database.go b/internal/database/database.go index a88aae7..f280b0a 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -4,7 +4,6 @@ import ( "database/sql" "fmt" _ "github.com/lib/pq" - "gitlab.com/kbr4/svevijesti/internal/model" ) const ( @@ -24,36 +23,3 @@ func Connect() (*Store, error) { db, err := sql.Open("postgres", psqlInfo) return db, err } - -func InsertArticle(store *Store, article model.ScrapedArticle) (err error) { - query := ` - INSERT INTO articles - (title, content, slug, original_url, source_id) - VALUES - ($1,$2,$3,$4,$5);` - - _, err = store.Exec(query, article.Title, article.Content, article.Slug, article.OriginalUrl, article.SourceId) - if err != nil { - return err - } - - return nil -} - -func IsSaved(store *Store, url string) bool { - - exists := false - query, err := store.Prepare(` - select exists(select 1 from articles where original_url = $1); - `) - if err != nil { - panic(err) - } - - row := query.QueryRow(url) - err = row.Scan(&exists) - if err != nil { - panic(err) - } - return exists -} diff --git a/internal/model/model.go b/internal/model/model.go index 9dce488..29aa6c7 100644 --- a/internal/model/model.go +++ b/internal/model/model.go @@ -1,5 +1,7 @@ package model +import "time" + type ScrapedArticle struct { Title string Content string @@ -8,6 +10,26 @@ type ScrapedArticle struct { SourceId int } +type DisplayArticle struct { + ID int + Title string + Content string + Slug string + OriginalUrl string + SourceId int + CreatedAt time.Time + FormatedCreatedAt string + SourceName string +} + const ( KlixSource = 1 ) + +func SourceName(sourceId int) string { + switch sourceId { + case KlixSource: + return "klix" + } + return "starenovine" +} diff --git a/web/data/articles.html b/web/data/articles.html new file mode 100644 index 0000000..66c0d3c --- /dev/null +++ b/web/data/articles.html @@ -0,0 +1,16 @@ +{{define "articlesHTML"}} +
    +{{range .articles}} +
  1. +
    + + {{.Title}}
    +
    {{.SourceName}} - {{ .FormatedCreatedAt }}
    +
  2. +

    +{{else}} +Nema članaka za izabrani datum. +{{end}} + +
+{{end}} diff --git a/web/data/footer.html b/web/data/footer.html new file mode 100644 index 0000000..80b8f37 --- /dev/null +++ b/web/data/footer.html @@ -0,0 +1,6 @@ +{{define "footerHTML"}} + + +{{end}} diff --git a/web/data/head.html b/web/data/head.html new file mode 100644 index 0000000..8c66df4 --- /dev/null +++ b/web/data/head.html @@ -0,0 +1,44 @@ +{{define "headHTML"}} + + + + + + + + + + + + {{.title}} - stare novine + + + +{{end}} diff --git a/web/data/header.html b/web/data/header.html new file mode 100644 index 0000000..88b0d46 --- /dev/null +++ b/web/data/header.html @@ -0,0 +1,18 @@ +{{define "headerHTML"}} +
+ +
+ +
+{{end}} diff --git a/web/tpl/home.html b/web/tpl/home.html new file mode 100644 index 0000000..849ed2f --- /dev/null +++ b/web/tpl/home.html @@ -0,0 +1,12 @@ +{{define "homeHTML"}} + +{{template "headHTML" .}} + +{{template "headerHTML" .}} + +{{template "articlesHTML" .}} + +{{template "footerHTML" .}} + + +{{end}}