1
0

webPage.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package main
  2. import (
  3. "./color"
  4. "./settings"
  5. "fmt"
  6. "html/template"
  7. "net/http"
  8. "os"
  9. "time"
  10. )
  11. func indexPage(w http.ResponseWriter, r *http.Request) {
  12. http.Redirect(w, r, "/plan/", 302)
  13. }
  14. func planPage(w http.ResponseWriter, r *http.Request) {
  15. if r.URL.Path != "/" && r.URL.Path != "/plan/" {
  16. http.Redirect(w, r, "/404Err", 404)
  17. return
  18. }
  19. t, err := template.ParseFiles(
  20. HTMLPATH+"index.html",
  21. NAVPATH,
  22. HEADPATH,
  23. )
  24. if err != nil {
  25. panic("can't load hmtl files")
  26. }
  27. showRequest(r)
  28. t.Execute(w, nil)
  29. }
  30. func loginPage(w http.ResponseWriter, r *http.Request) {
  31. if r.URL.Path != "/" && r.URL.Path != "/auth/" {
  32. http.Redirect(w, r, "/404Err", 404)
  33. return
  34. }
  35. t, err := template.ParseFiles(
  36. HTMLPATH+"login.html",
  37. HEADPATH,
  38. )
  39. if err != nil {
  40. panic("can't load hmtl files")
  41. }
  42. showRequest(r)
  43. t.Execute(w, nil)
  44. }
  45. func specialtyPage(w http.ResponseWriter, r *http.Request) {
  46. if r.URL.Path != "/" && r.URL.Path != "/specialty/" {
  47. http.Redirect(w, r, "/404Err", 404)
  48. return
  49. }
  50. t, err := template.ParseFiles(
  51. HTMLPATH+"specialty.html",
  52. NAVPATH,
  53. HEADPATH,
  54. )
  55. if err != nil {
  56. panic("can't load hmtl files")
  57. }
  58. showRequest(r)
  59. t.Execute(w, nil)
  60. }
  61. func bellPage(w http.ResponseWriter, r *http.Request) {
  62. if r.URL.Path != "/" && r.URL.Path != "/bellschedule/" {
  63. http.Redirect(w, r, "/404Err", 404)
  64. return
  65. }
  66. t, err := template.ParseFiles(
  67. HTMLPATH+"bellschedule.html",
  68. NAVPATH,
  69. HEADPATH,
  70. )
  71. if err != nil {
  72. panic("can't load hmtl files")
  73. }
  74. showRequest(r)
  75. t.Execute(w, nil)
  76. }
  77. func schedulePage(w http.ResponseWriter, r *http.Request) {
  78. if r.URL.Path != "/" && r.URL.Path != "/schedule/" {
  79. http.Redirect(w, r, "/404Err", 404)
  80. return
  81. }
  82. t, err := template.ParseFiles(
  83. HTMLPATH+"schedule.html",
  84. NAVPATH,
  85. HEADPATH,
  86. )
  87. if err != nil {
  88. panic("can't load hmtl files")
  89. }
  90. showRequest(r)
  91. t.Execute(w, nil)
  92. }
  93. func apiPage(w http.ResponseWriter, r *http.Request) {
  94. if r.URL.Path != "/" && r.URL.Path != "/api/" {
  95. http.Redirect(w, r, "/404Err", 404)
  96. return
  97. }
  98. t, err := template.ParseFiles(
  99. HTMLPATH + "api.html",
  100. )
  101. if err != nil {
  102. panic("can't load hmtl files")
  103. }
  104. showRequest(r)
  105. t.Execute(w, nil)
  106. }
  107. func subjectPage(w http.ResponseWriter, r *http.Request) {
  108. if r.URL.Path != "/" && r.URL.Path != "/subject/" {
  109. http.Redirect(w, r, "/404Err", 404)
  110. return
  111. }
  112. t, err := template.ParseFiles(
  113. HTMLPATH+"subject.html",
  114. NAVPATH,
  115. HEADPATH,
  116. )
  117. if err != nil {
  118. panic("can't load hmtl files")
  119. }
  120. showRequest(r)
  121. t.Execute(w, nil)
  122. }
  123. func groupPage(w http.ResponseWriter, r *http.Request) {
  124. if r.URL.Path != "/group/" {
  125. http.Redirect(w, r, "/404Err", 404)
  126. return
  127. }
  128. t, err := template.ParseFiles(
  129. HTMLPATH+"group.html",
  130. NAVPATH,
  131. HEADPATH,
  132. )
  133. if err != nil {
  134. panic("can't load hmtl files")
  135. }
  136. showRequest(r)
  137. t.Execute(w, nil)
  138. }
  139. func groupCardPage(w http.ResponseWriter, r *http.Request) {
  140. if r.URL.Path != "/groupcard/" {
  141. http.Redirect(w, r, "/404Err", 404)
  142. return
  143. }
  144. t, err := template.ParseFiles(
  145. HTMLPATH+"groupcard.html",
  146. NAVPATH,
  147. HEADPATH,
  148. )
  149. if err != nil {
  150. panic("can't load hmtl files")
  151. }
  152. showRequest(r)
  153. t.Execute(w, nil)
  154. }
  155. func teacherCardPage(w http.ResponseWriter, r *http.Request) {
  156. if r.URL.Path != "/teachercard/" {
  157. http.Redirect(w, r, "/404Err", 404)
  158. return
  159. }
  160. t, err := template.ParseFiles(
  161. HTMLPATH+"teacherscard.html",
  162. NAVPATH,
  163. HEADPATH,
  164. )
  165. if err != nil {
  166. panic("can't load hmtl files")
  167. }
  168. showRequest(r)
  169. t.Execute(w, nil)
  170. }
  171. func teacherPage(w http.ResponseWriter, r *http.Request) {
  172. if r.URL.Path != "/teacher/" {
  173. return
  174. }
  175. t, err := template.ParseFiles(
  176. HTMLPATH+"teacher.html",
  177. NAVPATH,
  178. HEADPATH,
  179. )
  180. if err != nil {
  181. panic("can't load hmtl files")
  182. }
  183. showRequest(r)
  184. t.Execute(w, nil)
  185. }
  186. func classroomPage(w http.ResponseWriter, r *http.Request) {
  187. if r.URL.Path != "/classroom/" {
  188. return
  189. }
  190. t, err := template.ParseFiles(
  191. HTMLPATH+"classroom.html",
  192. NAVPATH,
  193. HEADPATH,
  194. )
  195. if err != nil {
  196. panic("can't load hmtl files")
  197. }
  198. showRequest(r)
  199. t.Execute(w, nil)
  200. }
  201. func cyclePage(w http.ResponseWriter, r *http.Request) {
  202. if r.URL.Path != "/cycle/" {
  203. http.Redirect(w, r, "/404Err", 404)
  204. return
  205. }
  206. t, err := template.ParseFiles(
  207. HTMLPATH+"cycle.html",
  208. NAVPATH,
  209. HEADPATH,
  210. )
  211. if err != nil {
  212. panic("can't load hmtl files")
  213. }
  214. showRequest(r)
  215. t.Execute(w, nil)
  216. }
  217. func showRequest(r *http.Request) {
  218. t := time.Now()
  219. fmt.Println("[", t.Format(settings.TimeLayout), "]", color.Blue, "Request", color.Reset, "from", r.RemoteAddr, " to ", r.URL.Path)
  220. urllog, err := os.OpenFile("log/Requests_URL.log", os.O_APPEND|os.O_WRONLY, 0644)
  221. if err != nil {
  222. fmt.Println("[", t.Format(settings.TimeLayout), "]", "Error writing to URL log file")
  223. }
  224. s := "[" + t.Format(settings.TimeLayout) + "] " + "Request from " + r.RemoteAddr + " to " + r.URL.Path + "\n"
  225. _, err = urllog.WriteString(s)
  226. if err != nil {
  227. fmt.Println("[", t.Format(settings.TimeLayout), "]", "Error writing to log file")
  228. }
  229. }