From a74e309a8239abb49c57cd0900df4dfb9f5289ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=A2?= =?UTF-8?q?=D1=83=D1=85=D1=82=D0=B0=D1=80=D0=BE=D0=B2?= Date: Mon, 24 Jul 2023 16:53:41 +0500 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20HT?= =?UTF-8?q?MLRouter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/test2.html | 6 ++++++ fiber/init.go | 28 ++++++++++++++++++++++++++-- go.mod | 3 +++ go.sum | 6 ++++++ main.go | 9 +++++++++ 5 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 app/views/test2.html diff --git a/app/views/test2.html b/app/views/test2.html new file mode 100644 index 0000000..9089b9e --- /dev/null +++ b/app/views/test2.html @@ -0,0 +1,6 @@ +test of test {{.value}} +
+ {{range .items}} +

{{.}}

+ {{end}} +
\ No newline at end of file diff --git a/fiber/init.go b/fiber/init.go index 8121d49..f64cae1 100644 --- a/fiber/init.go +++ b/fiber/init.go @@ -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)) + }) +} diff --git a/go.mod b/go.mod index bc02ca3..3dcf76e 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 9a8282d..c340124 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index 7af3bff..ff08c37 100644 --- a/main.go +++ b/main.go @@ -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) }