v0.0.3 (минимально рабочая)

main
parent a8ef441f51
commit 067b2b84d7

@ -4,3 +4,9 @@
## История ## История
- 23-07-2023 Первый коммит<br> - 23-07-2023 Первый коммит<br>
Перенес на github. На Gitea, почему-то не захотела работать. Перенес на github. На Gitea, почему-то не захотела работать.
## Комментарии
Что-бы использовать пакет нужно сделать релиз.

@ -0,0 +1,5 @@
<html>
<body>
<h1>Hello!</h1>
</body>
</html>

@ -0,0 +1,3 @@
# Fiber - работа с fiber
просто инициализация

@ -2,6 +2,10 @@ package fiber
import ( import (
"fmt" "fmt"
_ "github.com/go-sql-driver/mysql" // для связи с mysql
"github.com/jmoiron/sqlx"
"os"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/compress" "github.com/gofiber/fiber/v2/middleware/compress"
"github.com/gofiber/fiber/v2/middleware/cors" "github.com/gofiber/fiber/v2/middleware/cors"
@ -9,13 +13,66 @@ import (
"github.com/gofiber/fiber/v2/middleware/logger" "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 // инициализация fiber
app := fiber.New() app := fiber.New()
app.Use(logger.New()) app.Use(logger.New())
app.Use(cors.New()) app.Use(cors.New())
app.Use(favicon.New()) app.Use(favicon.New())
app.Use(compress.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))
})
} }

@ -0,0 +1,12 @@
package fiber
import (
"testing"
)
func TestNewFiber(t *testing.T) {
if got := NewFiber(); got == nil {
t.Errorf("fiber не инициализирован, проверять дальше нечего")
t.Fail()
}
}

@ -2,7 +2,11 @@ module github.com/tad17/tad
go 1.19 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 ( require (
github.com/andybalholm/brotli v1.0.5 // indirect github.com/andybalholm/brotli v1.0.5 // indirect

@ -1,11 +1,18 @@
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= 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 h1:cRVMCb9aUJDsyHxGFLwz/sGzDggdailZZyptU9F9cU0=
github.com/gofiber/fiber/v2 v2.48.0/go.mod h1:xqJgfqrc23FJuqGOW6DVgi3HyZEm2Mn9pRqUb2kHSX8= 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 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 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 h1:XuJt9zzcnaz6a16/OU53ZjWp/v7/42WcR5t2a0PcNQY=
github.com/klauspost/compress v1.16.3/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= 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 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= 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= 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-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 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= 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 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=

@ -2,10 +2,28 @@ package main
import ( import (
"fmt" "fmt"
"github.com/jmoiron/sqlx"
"github.com/tad17/tad/fiber" "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() { func main() {
app := fiber.NewFiber() db := fiber.OpenDB("sea")
fmt.Printf("init fiber: %v\n", app) app := fiber.NewApp("test-app", db)
} app.JSONRouter("/test", testRouter)
app.Start(4444)
fmt.Printf("init app fiber: %v\n", app)
}

Loading…
Cancel
Save