-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from dewep-online/develop
add MySQL and SQLite Plugin
- Loading branch information
Showing
11 changed files
with
378 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
env: dev | ||
pid: "" | ||
level: 4 | ||
log: /dev/stdout | ||
|
||
debug: | ||
addr: 127.0.0.1:12000 | ||
read_timeout: 0s | ||
write_timeout: 0s | ||
idle_timeout: 0s | ||
shutdown_timeout: 0s | ||
|
||
http: | ||
main: | ||
addr: 127.0.0.1:8080 | ||
read_timeout: 0s | ||
write_timeout: 0s | ||
idle_timeout: 0s | ||
shutdown_timeout: 0s | ||
|
||
mysql: | ||
- name: main | ||
host: 127.0.0.1 | ||
port: 3306 | ||
schema: test_database | ||
user: test | ||
password: test | ||
maxidleconn: 5 | ||
maxopenconn: 5 | ||
maxconnttl: 50s | ||
interpolateparams: false | ||
timezone: UTC | ||
txisolevel: "" | ||
charset: utf8mb4,utf8 | ||
timeout: 5s | ||
readtimeout: 5s | ||
writetimeout: 5s | ||
|
||
sqlite: | ||
- name: main | ||
file: ./sqlite.db | ||
init_migration: | ||
- ./migration.sql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/dewep-online/goppy" | ||
"github.com/dewep-online/goppy/plugins" | ||
"github.com/dewep-online/goppy/plugins/database" | ||
"github.com/dewep-online/goppy/plugins/http" | ||
) | ||
|
||
func main() { | ||
|
||
app := goppy.New() | ||
app.WithConfig("./config.yaml") | ||
app.Plugins( | ||
http.WithHTTPDebug(), | ||
http.WithHTTP(), | ||
database.WithMySQL(), | ||
database.WithSQLite(), | ||
) | ||
app.Plugins( | ||
plugins.Plugin{ | ||
Inject: NewController, | ||
Resolve: func(routes *http.RouterPool, c *Controller) { | ||
router := routes.Main() | ||
router.Use(http.ThrottlingMiddleware(100)) | ||
router.Get("/users", c.Users) | ||
|
||
api := router.Collection("/api/v1", http.ThrottlingMiddleware(100)) | ||
api.Get("/user/{id}", c.User) | ||
}, | ||
}, | ||
) | ||
app.Run() | ||
|
||
} | ||
|
||
type Controller struct { | ||
db database.MySQL | ||
} | ||
|
||
func NewController(v database.MySQL) *Controller { | ||
return &Controller{ | ||
db: v, | ||
} | ||
} | ||
|
||
func (v *Controller) Users(ctx http.Ctx) { | ||
data := []int64{1, 2, 3, 4} | ||
ctx.SetBody().JSON(data) | ||
} | ||
|
||
func (v *Controller) User(ctx http.Ctx) { | ||
id, _ := ctx.Param("id").Int() | ||
|
||
err := v.db.Pool("main").Ping() | ||
if err != nil { | ||
ctx.Log().Errorf("db: %s", err.Error()) | ||
} | ||
|
||
ctx.SetBody().Error(http.ErrMessage{ | ||
HTTPCode: 400, | ||
InternalCode: "x1000", | ||
Message: "user not found", | ||
Ctx: map[string]interface{}{"id": id}, | ||
}) | ||
|
||
ctx.Log().Infof("user - %d", id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
create table `test_data` | ||
( | ||
`id` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, | ||
`data` TEXT, | ||
`updated_at` NUMERIC | ||
); |
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package database | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/dewep-online/goppy/plugins" | ||
"github.com/deweppro/go-logger" | ||
"github.com/deweppro/go-orm" | ||
"github.com/deweppro/go-orm/schema" | ||
"github.com/deweppro/go-orm/schema/mysql" | ||
) | ||
|
||
//ConfigMysql mysql config model | ||
type ConfigMysql struct { | ||
Pool []mysql.Item `yaml:"mysql"` | ||
} | ||
|
||
//List getting all configs | ||
func (v *ConfigMysql) List() (list []schema.ItemInterface) { | ||
for _, item := range v.Pool { | ||
list = append(list, item) | ||
} | ||
return | ||
} | ||
|
||
func (v *ConfigMysql) Default() { | ||
if len(v.Pool) == 0 { | ||
v.Pool = []mysql.Item{ | ||
{ | ||
Name: "main", | ||
Host: "127.0.0.1", | ||
Port: 3306, | ||
Schema: "test_database", | ||
User: "test", | ||
Password: "test", | ||
MaxIdleConn: 5, | ||
MaxOpenConn: 5, | ||
MaxConnTTL: time.Second * 50, | ||
InterpolateParams: false, | ||
Timezone: "UTC", | ||
TxIsolationLevel: "", | ||
Charset: "utf8mb4,utf8", | ||
Timeout: time.Second * 5, | ||
ReadTimeout: time.Second * 5, | ||
WriteTimeout: time.Second * 5, | ||
}, | ||
} | ||
|
||
} | ||
} | ||
|
||
//WithMySQL launch MySQL connection pool | ||
func WithMySQL() plugins.Plugin { | ||
return plugins.Plugin{ | ||
Config: &ConfigMysql{}, | ||
Inject: func(conf *ConfigMysql, log logger.Logger) (*mysqlProvider, MySQL) { | ||
conn := mysql.New(conf) | ||
o := orm.NewDB(conn, orm.Plugins{Logger: log}) | ||
return &mysqlProvider{conn: conn, conf: *conf, log: log}, o | ||
}, | ||
} | ||
} | ||
|
||
type ( | ||
mysqlProvider struct { | ||
conn schema.Connector | ||
conf ConfigMysql | ||
log logger.Logger | ||
} | ||
|
||
//MySQL connection MySQL interface | ||
MySQL interface { | ||
Pool(name string) orm.StmtInterface | ||
} | ||
) | ||
|
||
func (v *mysqlProvider) Up() error { | ||
if err := v.conn.Reconnect(); err != nil { | ||
return err | ||
} | ||
for _, item := range v.conf.Pool { | ||
p, err := v.conn.Pool(item.Name) | ||
if err != nil { | ||
return fmt.Errorf("pool `%s`: %w", item.Name, err) | ||
} | ||
if err = p.Ping(); err != nil { | ||
return fmt.Errorf("pool `%s`: %w", item.Name, err) | ||
} | ||
v.log.WithFields( | ||
logger.Fields{item.Name: fmt.Sprintf("%s:%d", item.Host, item.Port)}, | ||
).Infof("MySQL connect") | ||
} | ||
return nil | ||
} | ||
|
||
func (v *mysqlProvider) Down() error { | ||
return v.conn.Close() | ||
} |
Oops, something went wrong.