반업주부의 일상 배움사
[Golang] Gin + MySQL 본문
반응형
1. .env
MYSQL_HOST = ""
MYSQL_USER = ""
MYSQL_PASSWORD = ""
MYSQL_DBNAME = ""
MYSQL_PROTOCOL = "tcp"
MYSQL_PORT = 3306
2. go.mod
module myproject/main
go 1.16
require (
github.com/gin-contrib/cors v1.3.1 // indirect
github.com/gin-gonic/gin v1.7.7 // indirect
github.com/go-playground/validator/v10 v10.11.0 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/joho/godotenv v1.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/ugorji/go v1.2.7 // indirect
golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9 // indirect
golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gorm.io/driver/mysql v1.3.3 // indirect
gorm.io/gorm v1.23.5 // indirect
)
3. main.go
package main
import (
"myproject/main/routes"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
user := r.Group("user")
routes.UserRouter(user)
r.Run(":3000")
}
4. routes/UserRouter.go
package routes
import (
"database/sql"
"log"
_ "github.com/go-sql-driver/mysql"
"fmt"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
type Config struct {
pk int
googleVersion string
iosVersion string
}
func UserRouter(user *gin.RouterGroup) {
var appConfig map[string]string
appConfig, err := godotenv.Read()
if err != nil {
log.Fatal("Error reading .env file")
}
//-----------------------------------------------//
// /user/echo
//-----------------------------------------------//
echo := user.Group("echo")
echo.GET("/:msg", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": c.Param("msg"),
})
})
//-----------------------------------------------//
//-----------------------------------------------//
// /user/getConfig
//-----------------------------------------------//
getConfig := user.Group("getConfig")
getConfig.GET("/", func(c *gin.Context) {
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@%s(%s:%s)/%s",
appConfig["MYSQL_USER"],
appConfig["MYSQL_PASSWORD"],
appConfig["MYSQL_PROTOCOL"],
appConfig["MYSQL_HOST"],
appConfig["MYSQL_PORT"],
appConfig["MYSQL_DBNAME"]))
if err != nil {
panic(err)
}
defer db.Close()
var config Config
err = db.QueryRow("SELECT pk, googleVersion, iosVersion FROM config WHERE pk = 1").Scan(&config.pk, &config.googleVersion, &config.iosVersion)
if err != nil {
log.Fatal(err)
}
c.JSON(200, gin.H{
"pk": &config.pk,
"googleVersion": &config.googleVersion,
"iosVersion": &config.iosVersion,
})
})
//-----------------------------------------------//
}
5. 실행
$ go mod download
$ go run main.go
영어, 중국어 공부중이신가요?
홈스쿨 교재. 한 권으로 가족 모두 할 수 있어요!
반응형
LIST
'IT 인터넷 > Golang' 카테고리의 다른 글
[Golang] AWS CloudFront > 무효화 (CreateInvalidation) (0) | 2022.05.18 |
---|---|
[Golang] 파일 업로드 > S3 (0) | 2022.05.18 |
[Golang] Hello Gin (0) | 2022.05.16 |
[Golang] echo 샘플2 (0) | 2022.05.15 |
[Golang] echo 샘플 (0) | 2022.05.15 |
Comments