Add an icon-refresh button

This commit is contained in:
Aaron Bieber 2023-07-25 07:20:09 -06:00
parent 5374830887
commit 402749adb4
No known key found for this signature in database
5 changed files with 92 additions and 41 deletions

View File

@ -92,6 +92,10 @@
ul li span {
cursor: pointer;
}
footer {
text-align: center;
}
</style>
</head>
<body>

2
assets/main.min.js vendored

File diff suppressed because one or more lines are too long

52
icons.go Normal file
View File

@ -0,0 +1,52 @@
package main
import (
"fmt"
"io"
"log"
"net/http"
"suah.dev/gostart/data"
)
func updateIcons() {
links, err := app.queries.GetAllLinks(app.ctx)
if err != nil {
log.Println("can't get links: ", err)
}
for _, link := range links {
fmt.Println(link.LogoUrl)
if link.LogoUrl == "" {
continue
}
resp, err := http.Get(link.LogoUrl)
if err != nil {
log.Println(err)
continue
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println(err)
continue
}
err = resp.Body.Close()
if err != nil {
log.Println(err)
continue
}
contentType := resp.Header.Get("Content-Type")
err = app.queries.AddIcon(app.ctx, data.AddIconParams{
OwnerID: link.OwnerID,
LinkID: link.ID,
ContentType: contentType,
Data: body,
})
if err != nil {
log.Println("can't add icon: ", err)
}
}
}

49
main.go
View File

@ -7,7 +7,6 @@ import (
"embed"
"flag"
"fmt"
"io"
"io/fs"
"log"
"net/http"
@ -162,6 +161,14 @@ func main() {
r.Use(IconCacher)
r.Get("/{linkID:[0-9]+}", iconGET)
})
r.Route("/update-icons", func(r chi.Router) {
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
updateIcons()
w.Header().Add("Content-type", "application/json")
w.WriteHeader(200)
w.Write([]byte(`{"update": true}`))
})
})
go func() {
for {
@ -182,45 +189,7 @@ func main() {
go func() {
if dbExists {
for {
links, err := app.queries.GetAllLinks(app.ctx)
if err != nil {
log.Println("can't get links: ", err)
}
for _, link := range links {
fmt.Println(link.LogoUrl)
if link.LogoUrl == "" {
continue
}
resp, err := http.Get(link.LogoUrl)
if err != nil {
log.Println(err)
continue
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println(err)
continue
}
err = resp.Body.Close()
if err != nil {
log.Println(err)
continue
}
contentType := resp.Header.Get("Content-Type")
err = app.queries.AddIcon(app.ctx, data.AddIconParams{
OwnerID: link.OwnerID,
LinkID: link.ID,
ContentType: contentType,
Data: body,
})
if err != nil {
log.Println("can't add icon: ", err)
}
}
updateIcons()
time.Sleep(24 * time.Hour)
}
} else {

View File

@ -89,6 +89,8 @@ type Msg
| ReloadWatches
| SubmitLink
| SubmitWatch
| FetchIcons
| LoadIcons (Result Http.Error ())
type Status
@ -199,6 +201,14 @@ addWatch model =
}
fetchIcons : Cmd Msg
fetchIcons =
Http.get
{ url = "/update-icons"
, expect = Http.expectWhatever LoadIcons
}
deleteLink : Int -> Cmd Msg
deleteLink linkId =
Http.request
@ -228,6 +238,15 @@ deleteWatch watchId =
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
LoadIcons (Ok _) ->
( model, getLinks )
LoadIcons (Err _) ->
( { model | status = Errored "Server error reloading icons!" }, Cmd.none )
FetchIcons ->
( model, fetchIcons )
DeleteLink linkId ->
( model, deleteLink linkId )
@ -359,6 +378,13 @@ view model =
text ""
]
]
, footer []
[ details []
[ summary []
[ b [] [ text "Maintenence" ] ]
, button [ onClick FetchIcons ] [ text "Update Icons" ]
]
]
]