Advertisement
Guest User

Untitled

a guest
Nov 14th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. # go-lang, Revel, MySQL, ORM(gorm)
  2.  
  3. ### Installation
  4.  
  5. ```bash
  6. $ go get github.com/jinzhu/gorm
  7. $ go get github.com/go-sql-driver/mysql
  8. ```
  9.  
  10. ### Add configuration
  11.  
  12. ```bash
  13. $ vim conf/app.conf
  14. [dev]
  15. db.user = root
  16. db.password = ***
  17. db.host = localhost
  18. db.port = 3306
  19. db.name = ***_development
  20. db.protocol = tcp
  21. db.args
  22. ```
  23.  
  24. ### Create based controller
  25.  
  26. ```go
  27. $ vim app/controllers/database.go
  28. package controllers
  29.  
  30. import (
  31. "fmt"
  32. _ "github.com/go-sql-driver/mysql"
  33. "github.com/jinzhu/gorm"
  34. "github.com/revel/revel"
  35. "log"
  36. "strings"
  37. )
  38.  
  39. var Db *gorm.DB
  40.  
  41. func InitDB() {
  42. constr := genConStr()
  43. db, err := gorm.Open("mysql", constr)
  44. if err != nil {
  45. log.Panicf("Failed to database connect: %v\n", err)
  46. }
  47. db.DB()
  48. db.AutoMigrate(&models.User{})
  49. Db = db
  50. }
  51.  
  52. func genConStr() string {
  53. host := getConfigString("db.host", "")
  54. port := getConfigString("db.port", "")
  55. user := getConfigString("db.user", "")
  56. pass := getConfigString("db.pass", "")
  57. name := getConfigString("db.name", "")
  58. prot := getConfigString("db.prot", "")
  59. args := getConfigString("db.args", "")
  60.  
  61. if strings.Trim(args, " ") != "" {
  62. args = "?" + args
  63. } else {
  64. args = ""
  65. }
  66. return fmt.Sprintf("%s:%s@%s([%s]:%s)/%s%s",
  67. user, pass, prot, host, port, name, args)
  68. }
  69.  
  70. func getConfigString(param string, defval string) string {
  71. value, found := revel.Config.String(param)
  72. if !found {
  73. if defval == "" {
  74. log.Panicf("Not found config parameter: %s\n", param)
  75. }
  76. return defval
  77. }
  78. return value
  79. }
  80. ```
  81.  
  82. ### Initializer
  83.  
  84. ```go
  85. % vim app/init.go
  86. import (
  87. "{APP_PATH}/app/controllers"
  88. )
  89. func init() {
  90. revel.OnAppStart(controllers.InitDB)
  91. }
  92. ```
  93.  
  94. ### Create models
  95.  
  96. ```go
  97. % vim app/models/user.go
  98. package models
  99. type User struct {
  100. Id int64
  101. Name string
  102. Age int32
  103. }
  104. ```
  105.  
  106. ### Usage from controller
  107.  
  108. ```go
  109. % vim app/controllers/my_controller.go
  110. import (
  111. "{APP_PATH}/app/models"
  112. "github.com/revel/revel"
  113. )
  114.  
  115. type MyController struct {
  116. *revel.Controller
  117. }
  118.  
  119. func (c MyController) Index() revel.Result {
  120. user := &models.User{Name: "yukpiz", Age: 68}
  121. Db.Create(user)
  122.  
  123. result := Db.First(&user)
  124. return c.RenderJson(result)
  125. }
  126. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement