Skip to content

Commit

Permalink
Merge pull request #49 from dongxuny/master
Browse files Browse the repository at this point in the history
Add database/mysql as supported entry
  • Loading branch information
dongxuny authored Jan 6, 2022
2 parents 2d15f70 + 3142ba8 commit 633ca97
Show file tree
Hide file tree
Showing 11 changed files with 2,213 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Bootstrapper for rkentry.Entry.
With rk-boot, users can start gRPC, gin, echo, GoFrame, prometheus client or custom entry service with yaml formatted config file.
Easy to compile, run and debug your grpc service, grpc gateway, swagger UI and rk-tv web UI.

Besides, ORM database instances can also be bootstrapped by rk-boot.

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*
Expand All @@ -34,6 +36,7 @@ Easy to compile, run and debug your grpc service, grpc gateway, swagger UI and r
- [Concept](#concept)
- [Why do I want it?](#why-do-i-want-it)
- [Supported web frameworks](#supported-web-frameworks)
- [Supported database](#supported-database)
- [Quick Start](#quick-start)
- [Start grpc server from YAML](#start-grpc-server-from-yaml)
- [Start gin server from YAML](#start-gin-server-from-yaml)
Expand Down Expand Up @@ -78,6 +81,13 @@ rk-boot is a library which support bootstrapping server at runtime. It is a litt
| [go-zero](https://github.com/zeromicro/go-zero) | Testing | v0.0.2 | go get github.com/rookie-ninja/rk-boot/zero | [rk-zero](https://github.com/rookie-ninja/rk-zero) |
| [GorillaMux](https://github.com/gorilla/mux) | Testing | v0.0.2 | go get github.com/rookie-ninja/rk-boot/mux | [rk-mux](https://github.com/rookie-ninja/rk-mux) |

## Supported database
Databases still in Testing stage. Please see [examples](example/database) for detail.

| Database | Status | ORM | Import | Dependency | Docs |
| --- | --- | --- | --- | --- | --- |
| MySQL | Testing | [gorm](https://gorm.io/) | go get github.com/rookie-ninja/rk-db/mysql | [rk-db/mysql](https://github.com/rookie-ninja/rk-db/mysql) | See [docs](https://github.com/rookie-ninja/rk-db/mysql) |

## Quick Start
There are two ways users can run service.

Expand Down
52 changes: 52 additions & 0 deletions database/mysql/boot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.

package rkbootmysql

import (
_ "github.com/rookie-ninja/rk-boot"
"github.com/rookie-ninja/rk-db/mysql"
"gorm.io/gorm"
)

// GetMySqlEntry return rkmysql.MySqlEntry with name.
//
// The entryName was specified in boot.yaml file as bellow.
//
// user is the name of entry as function parameter.
//
// mySql:
// - name: user # Required
// enabled: true # Required
// locale: "*::*::*::*" # Required
// addr: "localhost:3306" # Optional, default: localhost:3306
func GetMySqlEntry(entryName string) *rkmysql.MySqlEntry {
return rkmysql.GetMySqlEntry(entryName)
}

// GetGormDb return gorm.DB instance with entryName and database name.
//
// rkmysql will init gorm.DB by reading boot.yaml file and validate the connectivity for us.
//
// The entryName and dataBaseName was specified in boot.yaml file as bellow.
//
// entryName: user
// dataBaseName: user-meta
//
// mySql:
// - name: user # Required
// enabled: true # Required
// locale: "*::*::*::*" # Required
// addr: "localhost:3306" # Optional, default: localhost:3306
// database:
// - name: user-meta # Required
// autoCreate: true # Optional, default: false
func GetGormDb(entryName, dataBaseName string) *gorm.DB {
if entry := GetMySqlEntry(entryName); entry != nil {
return entry.GetDB(dataBaseName)
}

return nil
}
9 changes: 9 additions & 0 deletions database/mysql/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/rookie-ninja/rk-boot/database/mysql

go 1.16

require (
github.com/rookie-ninja/rk-boot v1.4.1
github.com/rookie-ninja/rk-db/mysql v0.0.3
gorm.io/gorm v1.22.4
)
766 changes: 766 additions & 0 deletions database/mysql/go.sum

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions example/database/mysql/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.PHONY: all
all: gomod doctoc fmt

.PHONY: fmt
fmt:
@echo "[fmt] Formatting go project..."
@gofmt -s -w . 2>&1
@echo "------------------------------------[Done]"

.PHONY: doctoc
doctoc:
@echo "[doctoc] Running doctoc..."
@doctoc . 2>&1
@echo "------------------------------------[Done]"

.PHONY: gomod
gomod:
@echo "[gomod] Running go mod tidy..."
@go mod tidy
@echo "------------------------------------[Done]"
Loading

0 comments on commit 633ca97

Please sign in to comment.