main.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package main
  2. import (
  3. "./api"
  4. "./settings"
  5. "fmt"
  6. "net/http"
  7. "os"
  8. "time"
  9. // "./schedule"
  10. "github.com/jinzhu/gorm"
  11. _ "github.com/jinzhu/gorm/dialects/mysql"
  12. )
  13. const (
  14. HTMLPATH = "html/"
  15. PATH_STATIC = "static/"
  16. NAVPATH = "html/nav.html"
  17. HEADPATH = "html/head.html"
  18. )
  19. func init() {
  20. var err error
  21. settings.DB, err = gorm.Open("mysql", settings.CONSTR)
  22. if err != nil {
  23. fmt.Println(err)
  24. return
  25. }
  26. settings.DB.SingularTable(true)
  27. }
  28. func GetTime() time.Time{
  29. return time.Now()
  30. }
  31. func main() {
  32. http.Handle("/static/", http.StripPrefix(
  33. "/static/",
  34. handleFileServer(http.Dir(PATH_STATIC))),
  35. )
  36. _, err := os.Create("log/Requests_API.log")
  37. if err != nil {
  38. fmt.Println("api log file not created")
  39. return
  40. }
  41. t := time.Now()
  42. fmt.Println("[", t.Format(settings.TimeLayout), "]", "API log file can be found at log/Requests_API.log")
  43. _, err = os.Create("log/Requests_URL.log")
  44. if err != nil {
  45. fmt.Println("url log file not created")
  46. return
  47. }
  48. fmt.Println("[", t.Format(settings.TimeLayout), "]", "URL log file can be found at log/Requests_URL.log")
  49. _, err = os.Create("log/error.log")
  50. if err != nil {
  51. fmt.Println("Error log file not created")
  52. return
  53. }
  54. fmt.Println("[", t.Format(settings.TimeLayout), "]", "Error log file can be found at log/error.log")
  55. // API functions handling start
  56. http.HandleFunc("/api/teacher/", api.TeacherRoute)
  57. http.HandleFunc("/api/classroom/", api.ClassroomRoute)
  58. http.HandleFunc("/api/classroom/computer", api.GetClassroomByComputer)
  59. http.HandleFunc("/api/classroom/lecture", api.GetClassroomByComputer)
  60. http.HandleFunc("/api/classroom/name/", api.ClassroomByNumber)
  61. http.HandleFunc("/api/group/", api.GroupRoute)
  62. http.HandleFunc("/api/groupsubject/", api.SubjectOfGroupRoute)
  63. http.HandleFunc("/api/schedule/generate", api.Generate)
  64. http.HandleFunc("/api/subject/module/", api.GetSubjectByModule)
  65. http.HandleFunc("/api/subject/", api.SubjectRoute)
  66. http.HandleFunc("/api/module/", api.ModuleApi)
  67. http.HandleFunc("/api/cycle/", api.CycleApi)
  68. http.HandleFunc("/api/specialty/", api.SpecialtyRoute)
  69. http.HandleFunc("/api/building/", api.GetBuilding)
  70. http.HandleFunc("/api/groupschedule/", api.ScheduleOfGroupRoute)
  71. http.HandleFunc("/api/bellschedule/", api.BellScheduleRoute)
  72. http.HandleFunc("/api/attestation/", api.AttestationRoute)
  73. http.HandleFunc("/api/semester/", api.SemesterRoute)
  74. http.HandleFunc("/api/studyplan/", api.StudyplanRoute)
  75. http.HandleFunc("/api/subjectofplan/", api.SubjectofplanRoute)
  76. http.HandleFunc("/api/subjecttype/", api.SubjecttypeRoute)
  77. http.HandleFunc("/api/logs", testPage)
  78. http.HandleFunc("/api/getteachers", api.GetDataTeachers)
  79. http.HandleFunc("/api/getgroups/", api.GetDataGroups)
  80. http.HandleFunc("/api/opop/", api.OpopRoute)
  81. http.HandleFunc("/api/subjectofexample/", api.SubjectofexampleRoute)
  82. http.HandleFunc("/api/importplan/", api.ImportPlanRoute)
  83. http.HandleFunc("/api/auth/", api.LoginRoute)
  84. // API functions handling end
  85. // HTTP pages handling start
  86. http.HandleFunc("/", indexPage)
  87. http.HandleFunc("/plan/", planPage)
  88. http.HandleFunc("/teacher/", teacherPage)
  89. http.HandleFunc("/classroom/", classroomPage)
  90. http.HandleFunc("/groupcard/", groupCardPage)
  91. http.HandleFunc("/schedule/", schedulePage)
  92. http.HandleFunc("/teachercard/", teacherCardPage)
  93. http.HandleFunc("/group/", groupPage)
  94. http.HandleFunc("/cycle/", cyclePage)
  95. http.HandleFunc("/subject/", subjectPage)
  96. http.HandleFunc("/specialty/", specialtyPage)
  97. http.HandleFunc("/bellschedule/", bellPage)
  98. http.HandleFunc("/api/", apiPage)
  99. http.HandleFunc("/auth/", loginPage)
  100. http.HandleFunc("/api/modelsDownload", downloadModels)
  101. http.HandleFunc("/wekan", wekanRedirect)
  102. http.HandleFunc("/opop/", opopPage)
  103. http.HandleFunc("/test/", testPage) // testing token
  104. // HTTP pages handling end
  105. api.PrintConsole("Server is listening")
  106. http.ListenAndServe(":80", nil)
  107. }
  108. func wekanRedirect(w http.ResponseWriter, r *http.Request){
  109. if r.URL.Path == "/wekan" || r.URL.Path == "/wekan/"{
  110. http.Redirect(w, r, "http://192.168.14.173:8000", 301)
  111. return
  112. }
  113. }
  114. func testPage(w http.ResponseWriter, r *http.Request) {
  115. if api.CheckToken(r) {
  116. fmt.Println("Access granted")
  117. }
  118. fmt.Println("Access denied")
  119. }
  120. func downloadModels(w http.ResponseWriter, r *http.Request) {
  121. w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", "models.txt"))
  122. http.ServeFile(w, r, "models.txt")
  123. }
  124. func RenameLogFiles() {
  125. now := time.Now()
  126. t := now.Format("2 Jan 2006 15:04:05")
  127. newpath := "log/Requests_API " + t + ".log"
  128. err := os.Rename("log/Requests_API.log", newpath)
  129. if err != nil {
  130. fmt.Println("Error during saving API log file")
  131. }
  132. newpath = "log/Requests_URL " + t + ".log"
  133. err = os.Rename("log/Requests_URL.log", newpath)
  134. if err != nil {
  135. fmt.Println("Error during saving URL log file")
  136. }
  137. }
  138. func shutdown(w http.ResponseWriter, r *http.Request) {
  139. showRequest(r)
  140. now := time.Now()
  141. t := now.Format(time.Stamp)
  142. fmt.Println("Shutting down...")
  143. newpath := "log/Requests_API " + t + ".log"
  144. err := os.Rename("log/Requests_API.log", newpath)
  145. if err != nil {
  146. fmt.Println("Error during saving API log file")
  147. }
  148. newpath = "log/Requests_URL " + t + ".log"
  149. err = os.Rename("log/Requests_URL.log", newpath)
  150. if err != nil {
  151. fmt.Println("Error during saving URL log file")
  152. }
  153. os.Exit(0)
  154. }
  155. func handleFileServer(fs http.FileSystem) http.Handler {
  156. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  157. if _, err := fs.Open(r.URL.Path); os.IsNotExist(err) {
  158. http.Redirect(w, r, "/404Err", 404)
  159. return
  160. }
  161. http.FileServer(fs).ServeHTTP(w, r)
  162. })
  163. }