Skip to content

Commit

Permalink
cleanup cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danharasymiw committed Mar 29, 2024
1 parent 7791743 commit 3d0a4b2
Show file tree
Hide file tree
Showing 54 changed files with 332 additions and 364 deletions.
7 changes: 3 additions & 4 deletions .air.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ root = "."
tmp_dir = "bin"

[build]
bin = "./bin/main server"
args_bin = ["server"]
cmd = "go build -o ./bin/main main.go"
bin = "./bin/main"
cmd = "go build -o ./bin/main ./cmd/server/main.go"

delay = 1000
exclude_dir = ["bin", "cmd", "public"]
Expand All @@ -17,7 +16,7 @@ tmp_dir = "bin"
include_ext = ["go", "tpl", "tmpl", "templ", "html"]
kill_delay = "0s"
log = "build-errors.log"
send_interrupt = true
send_interrupt = false
stop_on_error = true

[color]
Expand Down
7 changes: 3 additions & 4 deletions pkg/scraper/main.go → cmd/scraper/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package scraper
package main

import (
"context"

scraper "github.com/salt-today/salttoday2/pkg/scraper/internal"
"github.com/salt-today/salttoday2/internal/scraper"
)

func Main() {
func main() {
scraper.ScrapeAndStoreArticles(context.Background())

scraper.ScrapeAndStoreComments(context.Background())

}
12 changes: 6 additions & 6 deletions pkg/server/main.go → cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server
package main

import (
"context"
Expand All @@ -9,15 +9,15 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"

"github.com/salt-today/salttoday2/pkg/server/internal/handlers"
"github.com/salt-today/salttoday2/pkg/store"
"github.com/salt-today/salttoday2/internal/server/handlers"
"github.com/salt-today/salttoday2/internal/store/rdb"
)

func Main() {
func main() {
r := chi.NewRouter()
r.Use(middleware.Logger)

storage, err := store.NewSQLStorage(context.Background())
storage, err := rdb.New(context.Background())
if err != nil {
panic(err)
}
Expand All @@ -36,7 +36,7 @@ func Main() {
r.Get("/user/{userID}", handler.HandleUser)
r.Get("/user/{userID}/comments", handler.HandleGetUserComments)

r.Handle("/public/*", http.StripPrefix("/public/", http.FileServer(http.Dir("public"))))
r.Handle("/public/*", http.StripPrefix("/public/", http.FileServer(http.Dir("web/public"))))

port := os.Getenv("PORT")
if port == `` {
Expand Down
12 changes: 7 additions & 5 deletions pkg/scraper/internal/scraper.go → internal/scraper/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import (
"github.com/PuerkitoBio/goquery"
"github.com/sirupsen/logrus"

"github.com/salt-today/salttoday2/pkg/sdk"
"github.com/salt-today/salttoday2/pkg/store"
"github.com/salt-today/salttoday2/internal"
"github.com/salt-today/salttoday2/internal/sdk"
"github.com/salt-today/salttoday2/internal/store"
"github.com/salt-today/salttoday2/internal/store/rdb"
)

// verify these are still correct and if there's any missing
Expand All @@ -34,13 +36,13 @@ var potentialPrefixes = []string{

func ScrapeAndStoreArticles(ctx context.Context) {
logEntry := sdk.Logger(ctx).WithField("job", "scraper/articles")
storage, err := store.NewSQLStorage(ctx)
storage, err := rdb.New(ctx)
if err != nil {
logEntry.WithError(err).Fatal("failed to create storage")
}

articles := make([]*store.Article, 0)
for _, site := range sdk.GetSites() {
for _, site := range internal.GetSites() {
foundArticles := ScrapeArticles(ctx, site)
articles = append(articles, foundArticles...)
}
Expand All @@ -52,7 +54,7 @@ func ScrapeAndStoreArticles(ctx context.Context) {

func ScrapeAndStoreComments(ctx context.Context) {
logEntry := sdk.Logger(ctx).WithField("job", "scraper/comments")
storage, err := store.NewSQLStorage(ctx)
storage, err := rdb.New(ctx)
if err != nil {
logEntry.WithError(err).Fatal("failed to create storage")
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package handlers
import (
"net/http"

"github.com/salt-today/salttoday2/pkg/server/internal/ui/views"
"github.com/salt-today/salttoday2/internal/server/ui/views"
)

func (h *Handler) HandleAbout(w http.ResponseWriter, r *http.Request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"strings"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/salt-today/salttoday2/pkg/sdk"
"github.com/salt-today/salttoday2/pkg/server/internal/ui/components"
"github.com/salt-today/salttoday2/pkg/server/internal/ui/views"
"github.com/salt-today/salttoday2/pkg/store"
"github.com/salt-today/salttoday2/internal/sdk"
"github.com/salt-today/salttoday2/internal/server/ui/components"
"github.com/salt-today/salttoday2/internal/server/ui/views"
"github.com/salt-today/salttoday2/internal/store"
"github.com/samber/lo"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/salt-today/salttoday2/pkg/store"
"github.com/salt-today/salttoday2/internal/store"
)

func processPageQueryParams(parameters map[string]string) (*store.PageQueryOptions, error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package handlers

import (
"github.com/salt-today/salttoday2/pkg/store"
"github.com/salt-today/salttoday2/internal/store"
)

type Handler struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/go-chi/chi/v5"

"github.com/salt-today/salttoday2/pkg/sdk"
"github.com/salt-today/salttoday2/pkg/server/internal/ui/components"
"github.com/salt-today/salttoday2/pkg/server/internal/ui/views"
"github.com/salt-today/salttoday2/pkg/store"
"github.com/salt-today/salttoday2/internal/sdk"
"github.com/salt-today/salttoday2/internal/server/ui/components"
"github.com/salt-today/salttoday2/internal/server/ui/views"
"github.com/salt-today/salttoday2/internal/store"
)

func (h *Handler) HandleUser(w http.ResponseWriter, r *http.Request) {
Expand Down
57 changes: 57 additions & 0 deletions internal/server/ui/components/comments.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package components

import "fmt"
import "github.com/salt-today/salttoday2/internal/store"

func getGradient(comment *store.Comment) string {
if comment.Likes+comment.Dislikes == 0 {
return "bg-gray-500"
}
if comment.Likes == 0 {
return "bg-red-600"
}
if comment.Dislikes == 0 {
return "bg-blue-600"
}
return fmt.Sprintf("bg-gradient-to-br from-blue-600 to-red-600 from-%d%% to-%d%%",
max(0,
int((float64(comment.Likes)/float64(comment.Likes+comment.Dislikes))*100)/5*5),
min(100,
int((float64(comment.Likes)/float64(comment.Likes+comment.Dislikes))*100-5)/5*5),
)

}

func max(x, y int) int {
if x > y {
return x
}
return y
}

func min(x, y int) int {
if x > y {
return y
}
return x
}

templ CommentComponent(comment *store.Comment) {
<div class="">
<div>
<a href={ templ.URL(comment.Article.Url) }>{ comment.Article.Title }</a>
</div>
<div class={ fmt.Sprintf(`flex flex-col p-2 rounded %s`, getGradient(comment)) }>
<div class=" flex flex-col p-3 bg-gray-200 text-black rounded">
<span>{ comment.Text }</span>
<span>👍 { fmt.Sprintf("%d", comment.Likes) } 👎 { fmt.Sprintf("%d", comment.Dislikes) }</span>
</div>
</div>
<div class="w-half flex flex-row max-width">
<div><a href={ templ.URL(fmt.Sprintf(" /user/%d", comment.User.ID)) }>- { comment.User.UserName }</a></div>
if comment.Deleted {
<div class="px-3 text-right font-bold text-red-600">DELETED</div>
}
</div>
</div>
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package components

import "github.com/salt-today/salttoday2/pkg/store"
import "github.com/salt-today/salttoday2/internal/store"

func selectedUintPtr(ptr *uint, lookingFor uint) bool {
if ptr == nil {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package components

import "github.com/salt-today/salttoday2/pkg/store"
import "github.com/salt-today/salttoday2/internal/store"

templ CommentsListComponent(comments []*store.Comment, nextUrl string) {
<div class="space-y-4">
Expand Down

0 comments on commit 3d0a4b2

Please sign in to comment.