Update things to allow for shared links.

- fix non-looping loop to pull in new icons
- ghetto fix for timing issue on db creation
This commit is contained in:
Aaron Bieber 2022-12-21 19:23:44 -07:00
parent 728f9e1675
commit dc3dc81238
6 changed files with 87 additions and 53 deletions

View File

@ -2,6 +2,7 @@
<project version="4">
<component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/queries.sql" dialect="GenericSQL" />
<file url="file://$PROJECT_DIR$/schema.sql" dialect="SQLite" />
<file url="PROJECT" dialect="SQLite" />
</component>
</project>

View File

@ -78,17 +78,19 @@ function deleteItem(path, id) {
}
function sendLinkData() {
return __awaiter(this, void 0, void 0, function () {
var ln, lu, ll, data;
var ln, lu, ll, ls, data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
ln = document.getElementById("linkname");
lu = document.getElementById('linkurl');
ll = document.getElementById('logourl');
ls = document.getElementById('linkshared');
data = {};
data.Url = lu.value;
data.Name = ln.value;
data.Logo_Url = ll.value;
data.Shared = ls.checked;
return [4 /*yield*/, postData('/links', data)];
case 1:
_a.sent();

View File

@ -24,6 +24,7 @@ interface Link {
Url: string
Name: string
Clicked: number|null
Shared: boolean
Logo_Url: string
}
@ -67,10 +68,12 @@ async function sendLinkData() {
const ln = document.getElementById("linkname") as HTMLInputElement;
const lu = document.getElementById('linkurl') as HTMLInputElement;
const ll = document.getElementById('logourl') as HTMLInputElement;
const ls = document.getElementById('linkshared') as HTMLInputElement;
let data = {} as Link;
data.Url = lu.value;
data.Name = ln.value;
data.Logo_Url = ll.value;
data.Shared = ls.checked;
await postData('/links', data);
}
async function sendWatchData() {

108
main.go
View File

@ -59,8 +59,12 @@ func main() {
log.Fatal("can't get ts local client: ", err)
}
dbExists := false
if *dbFile == ":memory:" {
tmpDBPopulate(db)
err := tmpDBPopulate(db)
if err != nil {
log.Fatal(err)
}
} else {
if _, err := os.Stat(*dbFile); os.IsNotExist(err) {
log.Println("Creating database..")
@ -69,6 +73,10 @@ func main() {
}
}
}
go func() {
time.Sleep(6 * time.Second)
dbExists = true
}()
if *key != "" {
keyData, err := os.ReadFile(*key)
@ -139,57 +147,67 @@ func main() {
go func() {
for {
var err error
app.watches, err = UpdateWatches(ghToken)
if err != nil {
log.Fatal("can't update watches: ", err)
if dbExists {
var err error
app.watches, err = UpdateWatches(ghToken)
if err != nil {
log.Fatal("can't update watches: ", err)
}
time.Sleep(time.Duration(*watchInterval) * time.Minute)
} else {
time.Sleep(3 * time.Second)
}
time.Sleep(time.Duration(*watchInterval) * time.Minute)
}
}()
go func() {
links, err := app.queries.GetAllLinks(app.ctx)
if err != nil {
log.Fatal("can't get links: ", err)
if dbExists {
for {
links, err := app.queries.GetAllLinks(app.ctx)
if err != nil {
log.Fatal("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.Fatal("can't add icon: ", err)
}
}
time.Sleep(24 * time.Hour)
}
} else {
time.Sleep(3 * time.Second)
}
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.Fatal("can't add icon: ", err)
}
}
time.Sleep(24 * time.Hour)
}()
hs := &http.Server{

View File

@ -131,6 +131,10 @@
URL
<input id="linkurl" type="text" name="url" placeholder="https://..." required>
</label>
<label for="shared">
Shared
<input id="linkshared" type="checkbox" required>
</label>
<label for="logourl">
Logo URL
<input id="logourl" type="text" name="logourl" placeholder="https://..." required>

20
tmp.go
View File

@ -7,10 +7,10 @@ import (
"suah.dev/gostart/data"
)
func tmpDBPopulate(db *sql.DB) {
func tmpDBPopulate(db *sql.DB) error {
log.Println("CREATING TEMP DATABASE!")
if _, err := db.ExecContext(app.ctx, schema); err != nil {
log.Fatal("can't create schema in temp db: ", err)
return err
}
ownerID := int64(57395170551826799)
@ -20,15 +20,18 @@ func tmpDBPopulate(db *sql.DB) {
Name: "europa.humpback-trout.ts.net.",
})
if err != nil {
log.Fatal("can't add temp owner: ", err)
return err
}
b, err := app.queries.AddLink(app.ctx, data.AddLinkParams{
_, err = app.queries.AddLink(app.ctx, data.AddLinkParams{
OwnerID: ownerID,
Url: "https://tapenet.org",
Name: "Tape::Net",
Shared: true,
LogoUrl: "https://git.tapenet.org/assets/img/logo.svg",
})
log.Println(b, err)
if err != nil {
return err
}
_, err = app.queries.AddPullRequest(app.ctx, data.AddPullRequestParams{
OwnerID: ownerID,
@ -36,7 +39,7 @@ func tmpDBPopulate(db *sql.DB) {
Repo: "NixOS/nixpkgs",
})
if err != nil {
log.Fatal("can't add temp PR: ", err)
return err
}
_, err = app.queries.AddWatchItem(app.ctx, data.AddWatchItemParams{
Name: "tailscale",
@ -44,6 +47,9 @@ func tmpDBPopulate(db *sql.DB) {
OwnerID: ownerID,
})
if err != nil {
log.Fatal("can't add temp watch item: ", err)
return err
}
log.Println("Done setting up tmp DB")
return nil
}