|
|
|
@ -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))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|