diff --git a/README.md b/README.md index fc03357..ad9f7e9 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,9 @@ ## История - 23-07-2023 Первый коммит
Перенес на github. На Gitea, почему-то не захотела работать. + + +## Комментарии + +Что-бы использовать пакет нужно сделать релиз. + diff --git a/app/dist/index.html b/app/dist/index.html new file mode 100644 index 0000000..ca1226a --- /dev/null +++ b/app/dist/index.html @@ -0,0 +1,5 @@ + + +

Hello!

+ + \ No newline at end of file diff --git a/fiber/README.md b/fiber/README.md new file mode 100644 index 0000000..89cb3e7 --- /dev/null +++ b/fiber/README.md @@ -0,0 +1,3 @@ +# Fiber - работа с fiber + +просто инициализация diff --git a/fiber/init.go b/fiber/init.go index 0f0eefa..8121d49 100644 --- a/fiber/init.go +++ b/fiber/init.go @@ -2,6 +2,10 @@ package fiber import ( "fmt" + _ "github.com/go-sql-driver/mysql" // для связи с mysql + "github.com/jmoiron/sqlx" + "os" + "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/compress" "github.com/gofiber/fiber/v2/middleware/cors" @@ -9,13 +13,66 @@ import ( "github.com/gofiber/fiber/v2/middleware/logger" ) -func NewFiber() *fiber.App { +type App struct { + name string + fiber *fiber.App + db *sqlx.DB +} + +type StringHandler func(db *sqlx.DB, query string) string + +func OpenDB(database string) *sqlx.DB { + // fmt.Printf("открываю БД nano-svelte\n") + db, err := sqlx.Open("mysql", "itman:X753951x@(xigmanas:3306)/"+database) + if err != nil { + fmt.Errorf("невозможно открыть базу данных %s", database) + os.Exit(0) + } + return db +} + +func NewApp(name string, db *sqlx.DB) *App { // инициализация fiber app := fiber.New() app.Use(logger.New()) app.Use(cors.New()) app.Use(favicon.New()) app.Use(compress.New()) - fmt.Printf("fiber init\n") - return app + + app.Static("/", "app/dist/index.html") + app.Static("/assets", "app/dist/assets", fiber.Static{ + Compress: true, + }) + app.Static("/fonts", "app/dist/fonts", fiber.Static{ + Compress: true, + }) + + //fmt.Printf("fiber init\n") + result := App{ + name: name, + fiber: app, + db: db, + } + return &result +} + +func (app *App) Start(port int) error { + err := app.fiber.Listen(fmt.Sprintf(":%d", port)) + return err +} + +func (app *App) JSONRouter(name string, funcRouter StringHandler) { + app.fiber.Get(name, func(ctx *fiber.Ctx) error { + q := ctx.Query("q", "") + return ctx.JSON(fiber.Map{ + "status": "ok", + "result": funcRouter(app.db, q), + }) + }) +} +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)) + }) } diff --git a/fiber/init_test.go b/fiber/init_test.go new file mode 100644 index 0000000..4bd1835 --- /dev/null +++ b/fiber/init_test.go @@ -0,0 +1,12 @@ +package fiber + +import ( + "testing" +) + +func TestNewFiber(t *testing.T) { + if got := NewFiber(); got == nil { + t.Errorf("fiber не инициализирован, проверять дальше нечего") + t.Fail() + } +} diff --git a/go.mod b/go.mod index 5646135..bc02ca3 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,11 @@ module github.com/tad17/tad go 1.19 -require github.com/gofiber/fiber/v2 v2.48.0 +require ( + github.com/go-sql-driver/mysql v1.7.1 + github.com/gofiber/fiber/v2 v2.48.0 + github.com/jmoiron/sqlx v1.3.5 +) require ( github.com/andybalholm/brotli v1.0.5 // indirect diff --git a/go.sum b/go.sum index 6841e01..9a8282d 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,18 @@ github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= +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/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= +github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ= github.com/klauspost/compress v1.16.3 h1:XuJt9zzcnaz6a16/OU53ZjWp/v7/42WcR5t2a0PcNQY= github.com/klauspost/compress v1.16.3/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0= +github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= @@ -13,6 +20,8 @@ github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APP github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg= +github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= diff --git a/main.go b/main.go index f5a2cdc..7af3bff 100644 --- a/main.go +++ b/main.go @@ -2,10 +2,28 @@ package main import ( "fmt" + "github.com/jmoiron/sqlx" "github.com/tad17/tad/fiber" + "os" + "strings" ) +func testRouter(db *sqlx.DB, query string) string { + //fmt.Printf("query: %s\n", query) + var urls []string + cmd := "select newurl from filemeta limit 5" + if err := db.Select(&urls, cmd); err != nil { + fmt.Errorf("ошибка выполнения: %s", cmd) + os.Exit(0) + } + s := strings.Join(urls, " ") + return s +} + func main() { - app := fiber.NewFiber() - fmt.Printf("init fiber: %v\n", app) -} \ No newline at end of file + db := fiber.OpenDB("sea") + app := fiber.NewApp("test-app", db) + app.JSONRouter("/test", testRouter) + app.Start(4444) + fmt.Printf("init app fiber: %v\n", app) +}