-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
100 lines (82 loc) · 3.41 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"accis/controllers"
"fmt"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
"html/template"
"log"
"net/http"
"os"
"os/exec"
"time"
)
func main() {
r := mux.NewRouter()
http.Handle("/api/", r)
http.Handle("/static/", http.StripPrefix("/static", http.FileServer(http.Dir("./static/"))))
http.HandleFunc("/", mainPage)
// departments
r.HandleFunc("/api/departments/", controllers.GetDepartments).Methods("GET")
r.HandleFunc("/api/departments/", controllers.AddDepartment).Methods("POST")
r.HandleFunc("/api/department/{id}/", controllers.GetDepartment).Methods("GET")
r.HandleFunc("/api/department/{id}/", controllers.DeleteDepartment).Methods("DELETE")
r.HandleFunc("/api/department/{id}/", controllers.UpdateDepartment).Methods("PUT")
// positions
r.HandleFunc("/api/positions/", controllers.GetPositions).Methods("GET")
r.HandleFunc("/api/positions/", controllers.AddPosition).Methods("POST")
r.HandleFunc("/api/position/{id}/", controllers.GetPosition).Methods("GET")
r.HandleFunc("/api/position/{id}/", controllers.DeletePosition).Methods("DELETE")
r.HandleFunc("/api/position/{id}/", controllers.UpdatePosition).Methods("PUT")
// employees
r.HandleFunc("/api/employees/", controllers.GetEmployees).Methods("GET")
r.HandleFunc("/api/employees/", controllers.AddEmployee).Methods("POST")
r.HandleFunc("/api/employee/{id}/", controllers.GetEmployee).Methods("GET")
r.HandleFunc("/api/employee/{id}/", controllers.DeleteEmployee).Methods("DELETE")
r.HandleFunc("/api/employee/{id}/", controllers.UpdateEmployee).Methods("PUT")
r.HandleFunc("/api/auth/login/", controllers.Login).Methods("POST")
r.HandleFunc("/api/auth/logout/", controllers.Logout).Methods("GET")
// notifications
r.HandleFunc("/api/notifications/", controllers.GetNotifications).Methods("GET")
r.HandleFunc("/api/notifications/", controllers.AddNotification).Methods("POST")
r.HandleFunc("/api/notification/{id}/", controllers.GetNotification).Methods("GET")
r.HandleFunc("/api/notification/{id}/", controllers.DeleteNotification).Methods("DELETE")
r.HandleFunc("/api/notification/{id}/", controllers.UpdateNotification).Methods("PUT")
// reports
r.HandleFunc("/api/reports/", controllers.GetReports).Methods("GET")
r.HandleFunc("/api/reports/", controllers.AddReport).Methods("POST")
r.HandleFunc("/api/report/{id}/", controllers.GetReport).Methods("GET")
r.HandleFunc("/api/report/{id}/", controllers.DeleteReport).Methods("DELETE")
r.HandleFunc("/api/report/{id}/", controllers.UpdateReport).Methods("PUT")
r.HandleFunc("/api/generate-report/", controllers.GenerateReport).Methods("POST")
r.HandleFunc("/api/import/", controllers.ImportReport).Methods("POST")
e := godotenv.Load() //Загрузить файл .env
if e != nil {
fmt.Print(e)
}
port := os.Getenv("PORT")
println("Server is started on port:", port)
openBrowser(fmt.Sprintf("http://localhost%s/", port))
err := http.ListenAndServe(port, nil)
if err != nil {
log.Fatal("ListenAndServe", err)
}
}
func mainPage(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFiles("static/frontend/templates/frontend/index.html")
if err != nil {
http.Error(w, err.Error(), 400)
return
}
if err := tmpl.Execute(w, nil); err != nil {
http.Error(w, err.Error(), 400)
return
}
}
func openBrowser(url string) {
<-time.After(100 * time.Millisecond)
err := exec.Command("cmd", "/c", "start", url).Start()
if err != nil {
log.Println(err)
}
}