router.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. package back
  2. import (
  3. "fmt"
  4. "strconv"
  5. "github.com/gin-gonic/contrib/sessions"
  6. "github.com/gin-gonic/contrib/static"
  7. "github.com/gin-gonic/gin"
  8. )
  9. var router *gin.Engine
  10. var month map[string]string
  11. func init() {
  12. initSetting()
  13. initLog()
  14. initPostgreSQL()
  15. m := manager{}
  16. fmt.Println("ADMIN EXISTS:", m.SelectTest())
  17. month = make(map[string]string)
  18. month["01"] = " января "
  19. month["02"] = " февраля "
  20. month["03"] = " марта "
  21. month["04"] = " апреля "
  22. month["05"] = " мая "
  23. month["06"] = " июня "
  24. month["07"] = " июля "
  25. month["08"] = " августа "
  26. month["09"] = " сентября "
  27. month["10"] = " октября "
  28. month["11"] = " ноября "
  29. month["12"] = " декабря "
  30. prepareMenu()
  31. prepareNews()
  32. prepareNewsPagination()
  33. prepareNotice()
  34. loadSession()
  35. router = gin.Default()
  36. word := sessions.NewCookieStore([]byte("RepairToSecretKey"))
  37. router.Use(sessions.Sessions("session", word))
  38. router.Static("/css/"+Setting.ServerVersion, Setting.Assets+"css/")
  39. router.Static("/js/"+Setting.ServerVersion, Setting.Assets+"js/")
  40. router.Static("/fonts", Setting.Assets+"font/")
  41. router.Static("/img", Setting.Assets+"img/")
  42. router.Use(static.Serve("/", static.LocalFile(Setting.Data, false)))
  43. router.LoadHTMLFiles(
  44. Setting.HTML+"index.html",
  45. Setting.HTML+"404.html",
  46. Setting.HTML+"position.html",
  47. Setting.HTML+"BigBoss.html",
  48. )
  49. router.GET("/", handlerIndex)
  50. // "/данные/материал/hello world"
  51. // "/данные/новость/f9a8c99c874"
  52. // "/данные/объявление/89c304af9f7e"
  53. router.GET("/данные/:category/:link", handlerMaterial)
  54. router.GET("/страница/:page", handlerIndexPage)
  55. router.GET("/page/:page", handlerPages)
  56. router.POST("/login", handlerLogin)
  57. router.POST("/logout", handlerLogout)
  58. router.NoRoute(handlerNoRoute)
  59. prepareRouterMenu()
  60. prepareRouterMaterial()
  61. prepareRouterDocument()
  62. }
  63. // Run запустить сервер
  64. func Run() {
  65. var e error
  66. if Setting.ServerSSL {
  67. e = router.RunTLS(Setting.ServerHost+":"+Setting.ServerPort, Setting.ServerSSLCert, Setting.ServerSSLKey)
  68. } else {
  69. e = router.Run(Setting.ServerHost + ":" + Setting.ServerPort)
  70. }
  71. logger.Println(e)
  72. }
  73. func handlerIndex(c *gin.Context) {
  74. isMain := true
  75. isAdmin := false
  76. isLogin := false
  77. role := getRoleFromContext(c)
  78. if len(role) != 0 {
  79. if role == "__admin" {
  80. isAdmin = true
  81. }
  82. isLogin = true
  83. }
  84. h := gin.H{}
  85. h["IsMain"] = isMain
  86. h["IsLogin"] = isLogin
  87. h["IsAdmin"] = isAdmin
  88. if isAdmin {
  89. h["Menu"] = templateMenuAll
  90. h["MenuTop"] = templateMenuAllTop
  91. } else {
  92. h["Menu"] = templateMenu
  93. h["MenuTop"] = templateMenuTop
  94. }
  95. h["News"] = templateNews
  96. h["Pagination"] = templateNewsPagination
  97. h["Notice"] = templateNotice
  98. h["ServerVersion"] = Setting.ServerVersion
  99. c.HTML(200, "index.html", h)
  100. }
  101. func handlerPages(c *gin.Context) {
  102. isMain := false
  103. isAdmin := false
  104. isLogin := false
  105. h := gin.H{}
  106. h["IsMain"] = isMain
  107. h["IsLogin"] = isLogin
  108. h["IsAdmin"] = isAdmin
  109. h["News"] = templateNews
  110. h["Pagination"] = templateNewsPagination
  111. h["Notice"] = templateNotice
  112. h["ServerVersion"] = Setting.ServerVersion
  113. h["AvailableMaterial"] = false
  114. h["Page"] = c.Param("page")
  115. c.HTML(200, "index.html", h)
  116. }
  117. func handlerIndexPage(c *gin.Context) {
  118. isMain := true
  119. isAdmin := false
  120. isLogin := false
  121. role := getRoleFromContext(c)
  122. if len(role) != 0 {
  123. if role == "__admin" {
  124. isAdmin = true
  125. }
  126. isLogin = true
  127. }
  128. page, e := strconv.ParseInt(c.Param("page"), 10, 64)
  129. if e != nil {
  130. logger.Println(e)
  131. page = 0
  132. }
  133. h := gin.H{}
  134. h["IsMain"] = isMain
  135. h["IsLogin"] = isLogin
  136. h["IsAdmin"] = isAdmin
  137. if isAdmin {
  138. h["Menu"] = templateMenuAll
  139. h["MenuTop"] = templateMenuAllTop
  140. } else {
  141. h["Menu"] = templateMenu
  142. h["MenuTop"] = templateMenuTop
  143. }
  144. h["News"] = getNewsByPage(int(page))
  145. h["Pagination"] = getNewsPaginationByPage(int(page))
  146. h["Notice"] = templateNotice
  147. h["ServerVersion"] = Setting.ServerVersion
  148. c.HTML(200, "index.html", h)
  149. }
  150. func handlerLogin(c *gin.Context) {
  151. var m manager
  152. e := c.BindJSON(&m)
  153. if e != nil {
  154. fmt.Println(e.Error())
  155. c.JSON(200, gin.H{
  156. "Error": "Неверный логин/пароль",
  157. })
  158. return
  159. }
  160. e = m.Select()
  161. if e != nil {
  162. fmt.Println(e.Error())
  163. c.JSON(200, gin.H{
  164. "Error": "Неверный логин/пароль",
  165. })
  166. return
  167. }
  168. s := sessions.Default(c)
  169. hash, e := GenerateHash(m.Login)
  170. if e != nil {
  171. logger.Println(e)
  172. c.JSON(200, gin.H{
  173. "Error": "Ошибка сервера",
  174. })
  175. return
  176. }
  177. newSession := session{}
  178. newSession.Hash = hash
  179. newSession.Manager = m.ID
  180. e = newSession.Insert()
  181. if e != nil {
  182. logger.Println(e)
  183. c.JSON(200, gin.H{
  184. "Error": "Ошибка сервера",
  185. })
  186. return
  187. }
  188. s.Set(sessionKey, hash)
  189. e = s.Save()
  190. if e != nil {
  191. fmt.Println(e.Error())
  192. c.JSON(200, gin.H{
  193. "Error": "Ошибка сохранения сессии в куки",
  194. })
  195. return
  196. }
  197. c.JSON(200, gin.H{
  198. "Error": nil,
  199. "Name": m.FirstName,
  200. })
  201. }
  202. func handlerLogout(c *gin.Context) {
  203. clearSessionFromContext(c)
  204. c.JSON(200, gin.H{
  205. "Error": nil,
  206. })
  207. }
  208. func handlerNoRoute(c *gin.Context) {
  209. h := gin.H{}
  210. h["Page"] = "400"
  211. isAdmin := false
  212. isLogin := false
  213. role := getRoleFromContext(c)
  214. if len(role) != 0 {
  215. if role == "__admin" {
  216. isAdmin = true
  217. }
  218. isLogin = true
  219. }
  220. h["IsMain"] = false
  221. h["IsLogin"] = isLogin
  222. h["IsAdmin"] = isAdmin
  223. if isAdmin {
  224. h["Menu"] = templateMenuAll
  225. h["MenuTop"] = templateMenuAllTop
  226. } else {
  227. h["Menu"] = templateMenu
  228. h["MenuTop"] = templateMenuTop
  229. }
  230. h["AvailableMaterial"] = false
  231. h["ServerVersion"] = Setting.ServerVersion
  232. c.HTML(200, "index.html", h)
  233. }
  234. func handlerCatalog(c *gin.Context) {
  235. isMain := false
  236. isAdmin := false
  237. isLogin := false
  238. role := getRoleFromContext(c)
  239. if len(role) != 0 {
  240. if role == "__admin" {
  241. isAdmin = true
  242. }
  243. isLogin = true
  244. }
  245. h := gin.H{}
  246. h["IsMain"] = isMain
  247. h["IsLogin"] = isLogin
  248. h["IsAdmin"] = isAdmin
  249. h["Page"] = "catalog"
  250. h["AvailableMaterial"] = false
  251. h["ServerVersion"] = Setting.ServerVersion
  252. c.HTML(200, "index.html", h)
  253. }