main.go 5.4 KB

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