bug fixes, accessiblity improvements, and more

This commit is contained in:
Zoe
2024-09-12 00:43:17 -05:00
parent 1fb4c71b2b
commit cbeafb3995
20 changed files with 241 additions and 271 deletions

15
main.go
View File

@@ -10,7 +10,6 @@ import (
"filething/routes"
"filething/ui"
"fmt"
"log"
"net/http"
"os"
"strings"
@@ -25,7 +24,6 @@ import (
var initUi func(e *echo.Echo)
func main() {
dbHost := os.Getenv("DB_HOST")
dbName := os.Getenv("DB_NAME")
dbUser := os.Getenv("DB_USER")
@@ -35,6 +33,7 @@ func main() {
panic("Missing database environment variabled!")
}
// TODO: retry connection or only connect at the first moment that we need the db
dbUrl := fmt.Sprintf("postgres://%s:%s@%s/%s?dial_timeout=10s&sslmode=disable", dbUser, dbPasswd, dbHost, dbName)
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dbUrl)))
@@ -52,6 +51,7 @@ func main() {
e := echo.New()
// insert the db into the echo context so it is easily accessible in routes and middleware
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Set("db", db)
@@ -68,6 +68,7 @@ func main() {
CookieHTTPOnly: true,
CookieSameSite: http.SameSiteStrictMode,
}))
e.Use(echoMiddleware.Secure())
api := e.Group("/api")
{
@@ -76,6 +77,7 @@ func main() {
// everything past this needs auth
api.Use(middleware.SessionMiddleware(db))
api.POST("/logout", routes.LogoutHandler)
api.GET("/user", routes.GetUser)
api.POST("/files/upload*", routes.UploadFile)
@@ -88,6 +90,9 @@ func main() {
// this isnt explicitly required, but it provides a better experience than doing this same thing clientside
e.Use(middleware.AuthCheckMiddleware)
// calls out to a function set by either server.go server_dev.go based on the value of the dev tag, and hosts
// either the static files that get embedded into the binary in ui/embed.go or proxies the dev server that gets
// run in the provided function
initUi(e)
e.HTTPErrorHandler = customHTTPErrorHandler
@@ -97,6 +102,8 @@ func main() {
}
}
// Custom Error handling since Nuxt relies on the 404 page for dynamic pages we still want api routes to use the default
// error handling built into echo
func customHTTPErrorHandler(err error, c echo.Context) {
if he, ok := err.(*echo.HTTPError); ok && he.Code == http.StatusNotFound {
path := c.Request().URL.Path
@@ -132,6 +139,7 @@ func customHTTPErrorHandler(err error, c echo.Context) {
c.Echo().DefaultHTTPErrorHandler(err, c)
}
// creates tables in the db if they dont already exist
func createSchema(db *bun.DB) error {
models := []interface{}{
(*models.User)(nil),
@@ -149,6 +157,7 @@ func createSchema(db *bun.DB) error {
return nil
}
// seeds the storage plans into the database
func seedPlans(db *bun.DB) error {
ctx := context.Background()
count, err := db.NewSelect().Model((*models.Plan)(nil)).Count(ctx)
@@ -161,6 +170,7 @@ func seedPlans(db *bun.DB) error {
return nil
}
// TODO: read this from a config
plans := []models.Plan{
{MaxStorage: 10 * 1024 * 1024 * 1024}, // 10GB
{MaxStorage: 50 * 1024 * 1024 * 1024}, // 50GB
@@ -172,6 +182,5 @@ func seedPlans(db *bun.DB) error {
return fmt.Errorf("failed to seed plans: %w", err)
}
log.Println("Successfully seeded the plans table")
return nil
}