main.go 5.2 KB

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