main.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 starting...")
  98. err = nil
  99. err = http.ListenAndServe(":" + os.Args[1], nil)
  100. if err!=nil{
  101. t := time.Now()
  102. s := "[" + t.Format(settings.TimeLayout) + "] " + "Error:" + err.Error()
  103. fmt.Println(s)
  104. }
  105. }
  106. func wekanRedirect(w http.ResponseWriter, r *http.Request){
  107. if r.URL.Path == "/wekan" || r.URL.Path == "/wekan/"{
  108. http.Redirect(w, r, "http://192.168.14.173:8000", 301)
  109. return
  110. }
  111. }
  112. func testPage(w http.ResponseWriter, r *http.Request) {
  113. if api.CheckToken(r) {
  114. fmt.Println("Access granted")
  115. }
  116. fmt.Println("Access denied")
  117. }
  118. func downloadModels(w http.ResponseWriter, r *http.Request) {
  119. w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", "models.txt"))
  120. http.ServeFile(w, r, "models.txt")
  121. }
  122. func RenameLogFiles() {
  123. now := time.Now()
  124. t := now.Format("2 Jan 2006 15:04:05")
  125. newpath := "log/Requests_API " + t + ".log"
  126. err := os.Rename("log/Requests_API.log", newpath)
  127. if err != nil {
  128. fmt.Println("Error during saving API log file")
  129. }
  130. newpath = "log/Requests_URL " + t + ".log"
  131. err = os.Rename("log/Requests_URL.log", newpath)
  132. if err != nil {
  133. fmt.Println("Error during saving URL log file")
  134. }
  135. }
  136. func shutdown(w http.ResponseWriter, r *http.Request) {
  137. showRequest(r)
  138. now := time.Now()
  139. t := now.Format(time.Stamp)
  140. fmt.Println("Shutting down...")
  141. newpath := "log/Requests_API " + t + ".log"
  142. err := os.Rename("log/Requests_API.log", newpath)
  143. if err != nil {
  144. fmt.Println("Error during saving API log file")
  145. }
  146. newpath = "log/Requests_URL " + t + ".log"
  147. err = os.Rename("log/Requests_URL.log", newpath)
  148. if err != nil {
  149. fmt.Println("Error during saving URL log file")
  150. }
  151. os.Exit(0)
  152. }
  153. func handleFileServer(fs http.FileSystem) http.Handler {
  154. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  155. if _, err := fs.Open(r.URL.Path); os.IsNotExist(err) {
  156. http.Redirect(w, r, "/404Err", 404)
  157. return
  158. }
  159. http.FileServer(fs).ServeHTTP(w, r)
  160. })
  161. }