This commit is contained in:
병준 박 2019-11-19 22:54:01 +09:00
parent b135da6a00
commit e30c7b7100
3 changed files with 36 additions and 11 deletions

1
go.sum
View File

@ -51,6 +51,7 @@ github.com/jackc/pgx/v4 v4.1.2 h1:xZwqiD9cP6zF7oJ1NO2j9txtjpA7I+MdfP3h/TAT1Q8=
github.com/jackc/pgx/v4 v4.1.2/go.mod h1:0cQ5ee0A6fEsg29vZekucSFk5OcWy8sT4qkhuPXHuIE=
github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.0.0 h1:rbjAshlgKscNa7j0jAM0uNQflis5o2XUogPMVAwtcsM=
github.com/jackc/puddle v1.0.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/klauspost/compress v1.8.2 h1:Bx0qjetmNjdFXASH02NSAREKpiaDwkO1DRZ3dV2KCcs=
github.com/klauspost/compress v1.8.2/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"net/url"
"reflect"
"git.loafle.net/loafer/di-go"
@ -12,7 +13,7 @@ import (
"git.loafle.net/totopia/server/pkg/loafer/web/router"
"github.com/valyala/fasthttp"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
type MethodMapping struct {
@ -21,15 +22,30 @@ type MethodMapping struct {
}
func Run(t reflect.Type) error {
conn, err := pgx.Connect(context.Background(), fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
"localhost", 54320, "totopia", "totopia", "totopia"))
pgURL := &url.URL{
Scheme: "postgres",
User: url.UserPassword("totopia", "totopia"),
Path: "totopia",
}
q := pgURL.Query()
q.Add("sslmode", "disable")
pgURL.RawQuery = q.Encode()
config, err := pgxpool.ParseConfig(pgURL.String())
if err != nil {
return fmt.Errorf("[%v]", err)
}
defer conn.Close(context.Background())
config.ConnConfig.Host = "localhost"
config.ConnConfig.Port = 54320
di.RegisterResource("dbConn", conn)
pool, err := pgxpool.ConnectConfig(context.Background(), config)
if err != nil {
return fmt.Errorf("[%v]", err)
}
defer pool.Close()
di.RegisterResource("dbConnPool", pool)
taServer := di.GetTypeAnnotation(t, appAnnotation.ServerAnnotationType)
if nil == taServer {

View File

@ -8,7 +8,7 @@ import (
"git.loafle.net/loafer/annotation-go"
"git.loafle.net/loafer/di-go"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
var UserDaoType = reflect.TypeOf((*UserDao)(nil))
@ -20,20 +20,28 @@ func init() {
type UserDao struct {
annotation.TypeAnnotation `annotation:"@web:Dao(\"name\": \"/userDao\")"`
Conn *pgx.Conn `annotation:"@Resource(\"name\": \"dbConn\")"`
Pool *pgxpool.Pool `annotation:"@Resource(\"name\": \"dbConnPool\")"`
}
func (ud *UserDao) FindAll() {
log.Printf("UserDao FindAll")
row, err := ud.Conn.Query(context.Background(), "SELECT NOW()")
conn, err := ud.Pool.Acquire(context.Background())
if nil != err {
log.Printf("%v", err)
return
}
defer conn.Release()
row, err := conn.Query(context.Background(), "select count(*) from users")
if nil != err {
log.Printf("%v", err)
return
}
if row.Next() {
var date string
var date int64
row.Scan(&date)
log.Printf("%s", date)
log.Printf("%d", date)
}
}