You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.3 KiB
89 lines
2.3 KiB
package webapp
|
|
|
|
import (
|
|
"log"
|
|
"zedshaw.games/webapp/tools"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
|
"github.com/gofiber/template/html/v2"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
"github.com/jmoiron/sqlx"
|
|
sq "github.com/Masterminds/squirrel"
|
|
)
|
|
|
|
type Link struct {
|
|
Id int `db:"id" json:"id"`
|
|
StreamId int `db:"stream_id" json:"stream_id" form:"stream_id" validate:"required,numeric"`
|
|
Url string `db:"url" json:"url" form:"url" validate:"required,url"`
|
|
Description string `db:"description" json:"description" form:"description" validate:"required"`
|
|
}
|
|
|
|
type Stream struct {
|
|
Id int `db:"id" json:"id"`
|
|
Title string `db:"title" json:"title"`
|
|
Description string `db:"description" json:"description"`
|
|
}
|
|
|
|
|
|
func main() {
|
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
|
|
|
db, err := sqlx.Connect("sqlite3", "db.sqlite3")
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
engine := html.New("./views", ".html")
|
|
app := fiber.New(fiber.Config{
|
|
Views: engine,
|
|
})
|
|
app.Use(logger.New())
|
|
|
|
// handler that returns one json from a sql database
|
|
app.Get("/api/stream/", func (c *fiber.Ctx) error {
|
|
sql, args, err := sq.Select("*").From("stream").ToSql()
|
|
return tools.SelectJson[Stream](db, c, err, sql, args...)
|
|
})
|
|
|
|
app.Get("/api/stream/:id", func (c *fiber.Ctx) error {
|
|
sql, args, err := sq.Select("*").From("stream").Where("id", c.Params("id")).ToSql()
|
|
return tools.GetJson[Stream](db, c, err, sql, args...)
|
|
})
|
|
|
|
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()
|
|
|
|
return tools.SelectJson[Link](db, c, err, sql, args...)
|
|
})
|
|
|
|
app.Post("/api/link", func (c *fiber.Ctx) error {
|
|
link, err := tools.GetThing[Link](c)
|
|
|
|
if(err != nil) {
|
|
log.Println(err)
|
|
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.Get("/test/", func (c *fiber.Ctx) error {
|
|
return c.Render("index", fiber.Map{
|
|
"Title": "Hello, World!",
|
|
})
|
|
})
|
|
|
|
log.Fatal(app.Listen(":5001"))
|
|
}
|
|
|