Добавил HTMLRouter

main 0.0.4
parent 2027d100b5
commit a74e309a82

@ -0,0 +1,6 @@
test of test {{.value}}
<div>
{{range .items}}
<h3>{{.}}</h3>
{{end}}
</div>

@ -3,14 +3,16 @@ package fiber
import (
"fmt"
_ "github.com/go-sql-driver/mysql" // для связи с mysql
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/jmoiron/sqlx"
"net/http"
"os"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/compress"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/favicon"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/template/html/v2"
)
type App struct {
@ -20,6 +22,7 @@ type App struct {
}
type StringHandler func(db *sqlx.DB, query string) string
type HTMLHandler func(db *sqlx.DB, query string) map[string]interface{}
func OpenDB(database string) *sqlx.DB {
// fmt.Printf("открываю БД nano-svelte\n")
@ -32,13 +35,26 @@ func OpenDB(database string) *sqlx.DB {
}
func NewApp(name string, db *sqlx.DB) *App {
// Create a new engine
//engine := html.New("./app/views", ".html")
engine := html.NewFileSystem(http.Dir("./app/views"), ".html")
engine.Reload(true) // Optional. Default: false
// Debug will print each template that is parsed, good for debugging
engine.Debug(true) // Optional. Default: false
// инициализация fiber
app := fiber.New()
app := fiber.New(fiber.Config{
Views: engine,
})
// midlleware
app.Use(logger.New())
app.Use(cors.New())
app.Use(favicon.New())
app.Use(compress.New())
// static
app.Static("/", "app/dist/index.html")
app.Static("/assets", "app/dist/assets", fiber.Static{
Compress: true,
@ -70,9 +86,17 @@ func (app *App) JSONRouter(name string, funcRouter StringHandler) {
})
})
}
func (app *App) StringRouter(name string, funcRouter StringHandler) {
app.fiber.Get(name, func(ctx *fiber.Ctx) error {
q := ctx.Query("q", "")
return ctx.SendString(funcRouter(app.db, q))
})
}
func (app *App) HTMLRouter(name string, funcRouter HTMLHandler) {
app.fiber.Get(name, func(ctx *fiber.Ctx) error {
q := ctx.Query("q", "")
return ctx.Render(name, funcRouter(app.db, q))
})
}

@ -10,6 +10,9 @@ require (
require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/gofiber/template v1.8.2 // indirect
github.com/gofiber/template/html/v2 v2.0.5 // indirect
github.com/gofiber/utils v1.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/klauspost/compress v1.16.3 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect

@ -5,6 +5,12 @@ github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrt
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/gofiber/fiber/v2 v2.48.0 h1:cRVMCb9aUJDsyHxGFLwz/sGzDggdailZZyptU9F9cU0=
github.com/gofiber/fiber/v2 v2.48.0/go.mod h1:xqJgfqrc23FJuqGOW6DVgi3HyZEm2Mn9pRqUb2kHSX8=
github.com/gofiber/template v1.8.2 h1:PIv9s/7Uq6m+Fm2MDNd20pAFFKt5wWs7ZBd8iV9pWwk=
github.com/gofiber/template v1.8.2/go.mod h1:bs/2n0pSNPOkRa5VJ8zTIvedcI/lEYxzV3+YPXdBvq8=
github.com/gofiber/template/html/v2 v2.0.5 h1:BKLJ6Qr940NjntbGmpO3zVa4nFNGDCi/IfUiDB9OC20=
github.com/gofiber/template/html/v2 v2.0.5/go.mod h1:RCF14eLeQDCSUPp0IGc2wbSSDv6yt+V54XB/+Unz+LM=
github.com/gofiber/utils v1.1.0 h1:vdEBpn7AzIUJRhe+CiTOJdUcTg4Q9RK+pEa0KPbLdrM=
github.com/gofiber/utils v1.1.0/go.mod h1:poZpsnhBykfnY1Mc0KeEa6mSHrS3dV0+oBWyeQmb2e0=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=

@ -20,10 +20,19 @@ func testRouter(db *sqlx.DB, query string) string {
return s
}
func testHTML(db *sqlx.DB, query string) map[string]interface{} {
result := make(map[string]interface{})
result["Item"] = "Проверка"
result["value"] = 456
result["items"] = []string{"Первый", "Второй", "Третий"}
return result
}
func main() {
db := fiber.OpenDB("sea")
app := fiber.NewApp("test-app", db)
app.JSONRouter("/test", testRouter)
app.HTMLRouter("test2", testHTML)
app.Start(4444)
fmt.Printf("init app fiber: %v\n", app)
}

Loading…
Cancel
Save