A website for my game dev stuff that supports chat, etc.
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.
 
 
 
 
 
zedshaw-games/main.go

96 lines
2.6 KiB

package main
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"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/fiber/v2/middleware/session"
)
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)
}
store := session.New()
engine := html.New("./views", ".html")
app := fiber.New(fiber.Config{
Views: engine,
})
app.Use(logger.New())
app.Use(recover.New())
// handler that returns one json from a sql database
app.Get("/api/stream/", func (c *fiber.Ctx) error {
sess, err := store.Get(c)
sess.Set("fuck", "off")
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.ReceivePost[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()
err = tools.Insert(db, err, sql, args...)
if err != nil {
log.Println(err)
return c.Redirect("/live/")
} else {
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"))
}