Came up with the pages/ directory that's templates which are rendered into public for the static pages.
This commit is contained in:
parent
facc691343
commit
f476605ecf
19 changed files with 795 additions and 61 deletions
133
api/handlers.go
Normal file
133
api/handlers.go
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
sq "github.com/Masterminds/squirrel"
|
||||
"github.com/gofiber/fiber/v2/middleware/session"
|
||||
|
||||
"zedshaw.games/webapp/data"
|
||||
)
|
||||
|
||||
var STORE *session.Store
|
||||
|
||||
|
||||
func GetApiLogout(c *fiber.Ctx) error {
|
||||
err := LogoutUser(c)
|
||||
if err != nil { return IfErrNil(err, c) }
|
||||
|
||||
return c.Redirect("/")
|
||||
}
|
||||
|
||||
func GetApiStream(c *fiber.Ctx) error {
|
||||
authed, _, err := CheckAuthed(c)
|
||||
if NotAuthed(err, authed) { return IfErrNil(err, c) }
|
||||
|
||||
sql, args, err := sq.Select("*").From("stream").ToSql()
|
||||
err = data.SelectJson[data.Stream](c, err, sql, args...)
|
||||
|
||||
return IfErrNil(err, c)
|
||||
}
|
||||
|
||||
func GetApiStreamId(c *fiber.Ctx) error {
|
||||
sql, args, err := sq.Select("*").
|
||||
From("stream").Where("id=?", c.Params("id")).ToSql()
|
||||
|
||||
err = data.GetJson[data.Stream](c, err, sql, args...)
|
||||
return IfErrNil(err, c)
|
||||
}
|
||||
|
||||
func GetApiStreamIdLinks(c *fiber.Ctx) error {
|
||||
sql, args, err := sq.Select("*").
|
||||
From("stream_link").
|
||||
Where("stream_id=?", c.Params("id")).ToSql()
|
||||
|
||||
err = data.SelectJson[data.Link](c, err, sql, args...)
|
||||
|
||||
return IfErrNil(err, c)
|
||||
}
|
||||
|
||||
func PostApiRegister(c *fiber.Ctx) error {
|
||||
user, err := ReceivePost[data.User](c)
|
||||
if err != nil { return IfErrNil(err, c) }
|
||||
|
||||
err = SetUserPassword(user)
|
||||
if err != nil { return IfErrNil(err, c) }
|
||||
|
||||
sql, args, err := sq.Insert("user").
|
||||
Columns("username", "email", "password").
|
||||
Values(user.Username, user.Email, user.Password).ToSql()
|
||||
|
||||
err = data.Exec(err, sql, args...)
|
||||
if err != nil { return IfErrNil(err, c) }
|
||||
|
||||
return c.Redirect("/login/")
|
||||
}
|
||||
|
||||
func PostApiLogin(c *fiber.Ctx) error {
|
||||
var user data.User
|
||||
|
||||
login, err := ReceivePost[data.Login](c)
|
||||
if(err != nil) { return IfErrNil(err, c) }
|
||||
|
||||
pass_good, err := LoginUser(&user, login)
|
||||
if err != nil { return IfErrNil(err, c) }
|
||||
|
||||
if pass_good {
|
||||
sess, err := STORE.Get(c)
|
||||
if err != nil { return IfErrNil(err, c) }
|
||||
|
||||
sess.Set("authenticated", true)
|
||||
err = sess.Save()
|
||||
if err != nil { return IfErrNil(err, c) }
|
||||
|
||||
return c.Redirect("/")
|
||||
} else {
|
||||
return c.Redirect("/login/")
|
||||
}
|
||||
}
|
||||
|
||||
func PostApiLink(c *fiber.Ctx) error {
|
||||
var sql string
|
||||
var args []interface{}
|
||||
|
||||
link, err := ReceivePost[data.Link](c)
|
||||
if err != nil { goto fail }
|
||||
|
||||
sql, args, err = sq.Insert("stream_blah").
|
||||
Columns("stream_id", "url", "description").
|
||||
Values(link.StreamId, link.Url, link.Description).ToSql()
|
||||
|
||||
err = data.Exec(err, sql, args...)
|
||||
if(err != nil) { goto fail }
|
||||
|
||||
return c.Redirect("/live/")
|
||||
|
||||
fail:
|
||||
return IfErrNil(err, c)
|
||||
}
|
||||
|
||||
|
||||
func Setup(app *fiber.App) {
|
||||
STORE = session.New()
|
||||
|
||||
app.Static("/", "./public", fiber.Static{
|
||||
Compress: false,
|
||||
CacheDuration: 1 * time.Nanosecond,
|
||||
})
|
||||
|
||||
app.Get("/api/stream", GetApiStream)
|
||||
app.Get("/api/logout", GetApiLogout)
|
||||
app.Get("/api/stream/:id", GetApiStreamId)
|
||||
app.Get("/api/stream/:id/links", GetApiStreamIdLinks)
|
||||
app.Post("/api/login", PostApiLogin)
|
||||
app.Post("/api/link", PostApiLink)
|
||||
app.Post("/api/register", PostApiRegister)
|
||||
}
|
||||
|
||||
func Shutdown() {
|
||||
log.Println("Shutting down controllers...")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue