module_index.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package modules
  2. import (
  3. "database/sql"
  4. _ "github.com/go-sql-driver/mysql"
  5. "fmt"
  6. "net/http"
  7. "os"
  8. "strconv"
  9. "golang-fave/consts"
  10. "golang-fave/engine/wrapper"
  11. "golang-fave/utils"
  12. )
  13. func (this *Modules) RegisterModule_Index() *Module {
  14. return this.newModule(MInfo{
  15. WantDB: true,
  16. Mount: "index",
  17. Name: "Pages",
  18. }, func(wrap *wrapper.Wrapper) {
  19. // Front-end
  20. wrap.RenderFrontEnd("index", consts.TmplDataModIndex{
  21. MetaTitle: "Meta Title",
  22. MetaKeywords: "Meta Keywords",
  23. MetaDescription: "Meta Description",
  24. MainMenuItems: []consts.TmplDataMainMenuItem{
  25. {Name: "Home", Link: "/", Active: true},
  26. {Name: "Item 1", Link: "/#1", Active: false},
  27. {Name: "Item 2", Link: "/#2", Active: false},
  28. {Name: "Item 3", Link: "/#3", Active: false},
  29. },
  30. })
  31. }, func(wrap *wrapper.Wrapper) {
  32. // Back-end
  33. wrap.W.WriteHeader(http.StatusOK)
  34. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  35. wrap.W.Header().Set("Content-Type", "text/html; charset=utf-8")
  36. wrap.W.Write([]byte(`INDEX BackEnd func call (` + wrap.CurrModule + `)`))
  37. })
  38. }
  39. func (this *Modules) RegisterAction_MysqlSetup() *Action {
  40. return this.newAction(AInfo{
  41. WantDB: false,
  42. Mount: "mysql",
  43. }, func(wrap *wrapper.Wrapper) {
  44. pf_host := wrap.R.FormValue("host")
  45. pf_port := wrap.R.FormValue("port")
  46. pf_name := wrap.R.FormValue("name")
  47. pf_user := wrap.R.FormValue("user")
  48. pf_password := wrap.R.FormValue("password")
  49. if pf_host == "" {
  50. wrap.MsgError(`Please specify host for MySQL connection`)
  51. return
  52. }
  53. if pf_port == "" {
  54. wrap.MsgError(`Please specify host port for MySQL connection`)
  55. return
  56. }
  57. if _, err := strconv.Atoi(pf_port); err != nil {
  58. wrap.MsgError(`MySQL host port must be integer number`)
  59. return
  60. }
  61. if pf_name == "" {
  62. wrap.MsgError(`Please specify MySQL database name`)
  63. return
  64. }
  65. if pf_user == "" {
  66. wrap.MsgError(`Please specify MySQL user`)
  67. return
  68. }
  69. // Try connect to mysql
  70. db, err := sql.Open("mysql", pf_user+":"+pf_password+"@tcp("+pf_host+":"+pf_port+")/"+pf_name)
  71. if err != nil {
  72. wrap.MsgError(err.Error())
  73. return
  74. }
  75. defer db.Close()
  76. err = db.Ping()
  77. if err != nil {
  78. wrap.MsgError(err.Error())
  79. return
  80. }
  81. // Try to install all tables
  82. _, err = db.Query(fmt.Sprintf(
  83. "CREATE TABLE `%s`.`users` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI', `first_name` VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'User first name', `last_name` VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'User last name', `email` VARCHAR(64) NOT NULL COMMENT 'User email', `password` VARCHAR(32) NOT NULL COMMENT 'User password (MD5)', PRIMARY KEY (`id`)) ENGINE = InnoDB;",
  84. pf_name))
  85. if err != nil {
  86. wrap.MsgError(err.Error())
  87. return
  88. }
  89. _, err = db.Query(fmt.Sprintf(
  90. "CREATE TABLE `%s`.`pages` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI', `user` int(11) NOT NULL COMMENT 'User id', `name` varchar(255) NOT NULL COMMENT 'Page name', `slug` varchar(255) NOT NULL COMMENT 'Page url part', `content` text NOT NULL COMMENT 'Page content', `meta_title` varchar(255) NOT NULL DEFAULT '' COMMENT 'Page meta title', `meta_keywords` varchar(255) NOT NULL DEFAULT '' COMMENT 'Page meta keywords', `meta_description` varchar(510) NOT NULL DEFAULT '' COMMENT 'Page meta description', `datetime` datetime NOT NULL COMMENT 'Creation date/time', `status` enum('draft','public','trash') NOT NULL COMMENT 'Page status', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;",
  91. pf_name))
  92. if err != nil {
  93. wrap.MsgError(err.Error())
  94. return
  95. }
  96. // Save mysql config file
  97. err = utils.MySqlConfigWrite(wrap.DConfig+string(os.PathSeparator)+"mysql.json", pf_host, pf_port, pf_name, pf_user, pf_password)
  98. if err != nil {
  99. wrap.MsgError(err.Error())
  100. return
  101. }
  102. // Reload current page
  103. wrap.Write(`window.location.reload(false);`)
  104. })
  105. }
  106. func (this *Modules) RegisterAction_CpFirstUser() *Action {
  107. return this.newAction(AInfo{
  108. WantDB: true,
  109. Mount: "first-user",
  110. }, func(wrap *wrapper.Wrapper) {
  111. pf_first_name := wrap.R.FormValue("first_name")
  112. pf_last_name := wrap.R.FormValue("last_name")
  113. pf_email := wrap.R.FormValue("email")
  114. pf_password := wrap.R.FormValue("password")
  115. if pf_email == "" {
  116. wrap.MsgError(`Please specify user email`)
  117. return
  118. }
  119. if !utils.IsValidEmail(pf_email) {
  120. wrap.MsgError(`Please specify correct user email`)
  121. return
  122. }
  123. if pf_password == "" {
  124. wrap.MsgError(`Please specify user password`)
  125. return
  126. }
  127. _, err := wrap.DB.Query(
  128. "INSERT INTO `users` SET `first_name` = ?, `last_name` = ?, `email` = ?, `password` = MD5(?);",
  129. pf_first_name, pf_last_name, pf_email, pf_password)
  130. if err != nil {
  131. wrap.MsgError(err.Error())
  132. return
  133. }
  134. // Reload current page
  135. wrap.Write(`window.location.reload(false);`)
  136. })
  137. }
  138. func (this *Modules) RegisterAction_CpUserLogin() *Action {
  139. return this.newAction(AInfo{
  140. WantDB: true,
  141. Mount: "signin",
  142. }, func(wrap *wrapper.Wrapper) {
  143. pf_email := wrap.R.FormValue("email")
  144. pf_password := wrap.R.FormValue("password")
  145. if pf_email == "" {
  146. wrap.MsgError(`Please specify user email`)
  147. return
  148. }
  149. if !utils.IsValidEmail(pf_email) {
  150. wrap.MsgError(`Please specify correct user email`)
  151. return
  152. }
  153. if pf_password == "" {
  154. wrap.MsgError(`Please specify user password`)
  155. return
  156. }
  157. if wrap.S.GetInt("UserId", 0) > 0 {
  158. wrap.MsgError(`You already logined`)
  159. return
  160. }
  161. var user_id int
  162. err := wrap.DB.QueryRow(
  163. "SELECT `id` FROM `users` WHERE `email` = ? and `password` = MD5(?) LIMIT 1;",
  164. pf_email, pf_password).Scan(&user_id)
  165. if err != nil && err != sql.ErrNoRows {
  166. wrap.MsgError(err.Error())
  167. return
  168. }
  169. if err == sql.ErrNoRows {
  170. wrap.MsgError(`Incorrect email or password`)
  171. return
  172. }
  173. // Save to current session
  174. wrap.S.SetInt("UserId", user_id)
  175. // Reload current page
  176. wrap.Write(`window.location.reload(false);`)
  177. })
  178. }
  179. func (this *Modules) RegisterAction_CpUserLogout() *Action {
  180. return this.newAction(AInfo{
  181. WantDB: true,
  182. Mount: "singout",
  183. WantUser: true,
  184. }, func(wrap *wrapper.Wrapper) {
  185. // Reset session var
  186. wrap.S.SetInt("UserId", 0)
  187. // Reload current page
  188. wrap.Write(`window.location.reload(false);`)
  189. })
  190. }
  191. func (this *Modules) RegisterAction_CpUserSettings() *Action {
  192. return this.newAction(AInfo{
  193. WantDB: true,
  194. Mount: "user-settings",
  195. WantUser: true,
  196. }, func(wrap *wrapper.Wrapper) {
  197. pf_first_name := wrap.R.FormValue("first_name")
  198. pf_last_name := wrap.R.FormValue("last_name")
  199. pf_email := wrap.R.FormValue("email")
  200. pf_password := wrap.R.FormValue("password")
  201. if pf_email == "" {
  202. wrap.MsgError(`Please specify user email`)
  203. return
  204. }
  205. if !utils.IsValidEmail(pf_email) {
  206. wrap.MsgError(`Please specify correct user email`)
  207. return
  208. }
  209. if pf_password != "" {
  210. // Update with password if set
  211. _, err := wrap.DB.Query(
  212. "UPDATE `users` SET `first_name` = ?, `last_name` = ?, `email` = ?, `password` = MD5(?) WHERE `id` = ?;",
  213. pf_first_name, pf_last_name, pf_email, pf_password, wrap.User.A_id)
  214. if err != nil {
  215. wrap.MsgError(err.Error())
  216. return
  217. }
  218. } else {
  219. // Update without password if not set
  220. _, err := wrap.DB.Query(
  221. "UPDATE `users` SET `first_name` = ?, `last_name` = ?, `email` = ? WHERE `id` = ?;",
  222. pf_first_name, pf_last_name, pf_email, wrap.User.A_id)
  223. if err != nil {
  224. wrap.MsgError(err.Error())
  225. return
  226. }
  227. }
  228. })
  229. }