I can now submit a form and store it in the database.

main
Zed A. Shaw 4 days ago
parent eaaab2ac0b
commit 4045799ab9
  1. 52
      .air.toml
  2. 7
      Makefile
  3. 27
      main.go
  4. 11
      public/live/index.html

@ -0,0 +1,52 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"
[build]
args_bin = []
bin = "fibertest.exe"
cmd = "make build"
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html", "css", "js"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = []
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false
[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"
[log]
main_only = false
silent = false
time = false
[misc]
clean_on_exit = false
[proxy]
app_port = 5001
enabled = true
proxy_port = 5002
[screen]
clear_on_rebuild = false
keep_scroll = true

@ -2,7 +2,10 @@ build:
go build . go build .
docs: docs:
go tool godoc -http=localhost:6060 -index go tool godoc -http=localhost:6060 -index -index_files godoc.idx
build_docs:
go tool godoc -v -http=localhost:6060 -index -index_files godoc.idx -write_index
dev: dev:
air go tool air

@ -12,9 +12,9 @@ import (
type Link struct { type Link struct {
Id int `db:"id" json:"id"` Id int `db:"id" json:"id"`
StreamId int `db:"stream_id" json:"stream_id"` StreamId int `db:"stream_id" json:"stream_id" form:"stream_id"`
Url string `db:"url" json:"url"` Url string `db:"url" json:"url" form:"url"`
Description string `db:"description" json:"description"` Description string `db:"description" json:"description" form:"description"`
} }
type Stream struct { type Stream struct {
@ -74,13 +74,32 @@ func main() {
return GetJson[Stream](db, c, err, sql, args...) return GetJson[Stream](db, c, err, sql, args...)
}) })
app.Get("/api/stream/:id/links", func (c *fiber.Ctx) error { app.Get("/api/stream/:id/links", func (c *fiber.Ctx) error {
sql, args, err := sq.Select("*").From("stream_link").Where("stream_id", c.Params("id")).ToSql() sql, args, err := sq.Select("*").From("stream_link").Where("stream_id", c.Params("id")).ToSql()
return SelectJson[Link](db, c, err, sql, args...) return SelectJson[Link](db, c, err, sql, args...)
}) })
app.Post("/api/link", func (c *fiber.Ctx) error {
link := new(Link)
if err := c.BodyParser(link); err != nil {
log.Println(err);
return c.Redirect("/live/")
}
sql, args, err := sq.Insert("stream_link").Columns("stream_id", "url", "description").Values(link.StreamId, link.Url, link.Description).ToSql()
if err != nil {
log.Println(err)
return c.Redirect("/live/")
}
db.MustExec(sql, args...)
return c.Redirect("/live/")
})
app.Static("/", "./public") app.Static("/", "./public")
log.Fatal(app.Listen(":5001")) log.Fatal(app.Listen(":5001"))

@ -9,7 +9,8 @@
<script defer src="/js/alpine.js"></script> <script defer src="/js/alpine.js"></script>
<script src="/js/code.js"></script> <script src="/js/code.js"></script>
<script> <script>
let req = new GetJson("/api/live/index.json"); let req = new GetJson("/api/stream/1");
let link_req = new GetJson("/api/stream/1/links");
</script> </script>
</head> </head>
@ -26,8 +27,8 @@
<block> <block>
<h2>Links Found in Chat</h2> <h2>Links Found in Chat</h2>
<ul> <ul x-init="links = await link_req.theData()" x-data="{links: {}}">
<template x-for="item in Stream.links"> <template x-for="item in links">
<li><a x-text="item.description" x-bind:href="item.url"></a></li> <li><a x-text="item.description" x-bind:href="item.url"></a></li>
</template> </template>
</ul> </ul>
@ -37,7 +38,9 @@
<card> <card>
<top>Submit a Link</top> <top>Submit a Link</top>
<middle> <middle>
<input type="text" placeholder="Link Url"> <input name="stream_id" type="hidden" value="1">
<input name="url" type="text" placeholder="Link Url">
<input name="description" type="text" placeholder="Description">
</middle> </middle>
</card> </card>
<buttons> <buttons>

Loading…
Cancel
Save