This repository has been archived by the owner on Jan 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tima.go
117 lines (94 loc) · 5.57 KB
/
tima.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"flag"
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/nesmyslny/tima/server"
)
type cliFlags struct {
configPath string
dbUp int
dbDown int
dbGenerateData bool
}
func main() {
flags := parseFlags()
config := server.NewConfig(flags.configPath)
db := server.NewDB(config.GetDataSourceName())
defer db.Close()
executed, err := db.ExecCliActions(flags.dbUp, flags.dbDown, flags.dbGenerateData)
if executed {
printCliActionResult(err)
return
}
auth := server.NewAuth(config.JwtPrivateKey, config.JwtPublicKey, config.TokenExpiry)
departmentAPI := server.NewDepartmentAPI(db)
userAPI := server.NewUserAPI(db, auth)
projectAPI := server.NewProjectAPI(db)
projectCategoryAPI := server.NewProjectCategoryAPI(db)
activityTypeAPI := server.NewActivityTypeAPI(db)
activityAPI := server.NewActivityAPI(db)
migrationAPI := server.NewMigrationAPI(db, userAPI)
reporting := server.NewReporting(db)
router := mux.NewRouter()
// todo: secure upgrade route (-> implement installation/upgrading
createAnonRoute(router, "/upgrade", "POST", migrationAPI.UpgradeHandler)
createAnonRoute(router, "/signIn", "POST", userAPI.SigninHandler)
createAnonRoute(router, "/isSignedIn", "GET", userAPI.IsSignedInHandler)
createAuthRoute(router, auth, server.AuthorizeUser, "/activities/{day}", "GET", activityAPI.GetByDayHandler)
createAuthRoute(router, auth, server.AuthorizeUser, "/activities", "POST", activityAPI.SaveHandler)
createAuthRoute(router, auth, server.AuthorizeUser, "/activities/{id}", "DELETE", activityAPI.DeleteHandler)
createAuthRoute(router, auth, server.AuthorizeDeptManager, "/projects/admin", "GET", projectAPI.GetListHandler)
createAuthRoute(router, auth, server.AuthorizeUser, "/projects/user", "GET", projectAPI.GetListUserHandler)
createAuthRoute(router, auth, server.AuthorizeUser, "/projects/selectList", "GET", projectAPI.GetListSelectHandler)
createAuthRoute(router, auth, projectAPI.AuthorizeGet, "/projects/{id}", "GET", projectAPI.GetHandler)
createAuthRoute(router, auth, projectAPI.AuthorizeSave, "/projects", "POST", projectAPI.SaveHandler)
createAuthRoute(router, auth, projectAPI.AuthorizeDelete, "/projects/{id}", "DELETE", projectAPI.DeleteHandler)
createAuthRoute(router, auth, server.AuthorizeManager, "/projectCategories/tree", "GET", projectCategoryAPI.GetTreeHandler)
createAuthRoute(router, auth, server.AuthorizeUser, "/projectCategories/list", "GET", projectCategoryAPI.GetListHandler)
createAuthRoute(router, auth, server.AuthorizeManager, "/projectCategories", "POST", projectCategoryAPI.SaveHandler)
createAuthRoute(router, auth, server.AuthorizeManager, "/projectCategories/{id}", "DELETE", projectCategoryAPI.DeleteHandler)
createAuthRoute(router, auth, server.AuthorizeUser, "/activityTypes", "GET", activityTypeAPI.GetListHandler)
createAuthRoute(router, auth, server.AuthorizeManager, "/activityTypes/{id}", "GET", activityTypeAPI.GetHandler)
createAuthRoute(router, auth, server.AuthorizeManager, "/activityTypes", "POST", activityTypeAPI.SaveHandler)
createAuthRoute(router, auth, server.AuthorizeManager, "/activityTypes/{id}", "DELETE", activityTypeAPI.DeleteHandler)
createAuthRoute(router, auth, server.AuthorizeUser, "/users/all", "GET", userAPI.GetListHandler)
createAuthRoute(router, auth, server.AuthorizeUser, "/users/department", "GET", userAPI.GetListDeptHandler)
createAuthRoute(router, auth, userAPI.AuthorizeGet, "/users/{id}", "GET", userAPI.GetHandler)
createAuthRoute(router, auth, userAPI.AuthorizeSave, "/users", "POST", userAPI.SaveHandler)
createAuthRoute(router, auth, server.AuthorizeAdmin, "/departments/tree", "GET", departmentAPI.GetTreeHandler)
createAuthRoute(router, auth, server.AuthorizeUser, "/departments/list", "GET", departmentAPI.GetListHandler)
createAuthRoute(router, auth, server.AuthorizeAdmin, "/departments", "POST", departmentAPI.SaveHandler)
createAuthRoute(router, auth, server.AuthorizeAdmin, "/departments/{id}", "DELETE", departmentAPI.DeleteHandler)
createAuthRoute(router, auth, server.AuthorizeManager, "/report/overview", "POST", reporting.CreateReportOverview)
createAuthRoute(router, auth, server.AuthorizeManager, "/report/projects", "POST", reporting.CreateReportProjects)
router.PathPrefix("/").Handler(http.FileServer(http.Dir("public/")))
http.Handle("/", router)
http.ListenAndServe(config.HttpAddr, nil)
}
func createAnonRoute(router *mux.Router, path string, method string,
handlerFunc func(context *server.HandlerContext) (interface{}, *server.HandlerError)) {
router.Handle(path, server.NewAnonHandler(handlerFunc)).Methods(method)
}
func createAuthRoute(router *mux.Router, auth *server.Auth,
authorizeFunc func(context *server.HandlerContext) (bool, error),
path string, method string,
handlerFunc func(context *server.HandlerContext) (interface{}, *server.HandlerError)) {
router.Handle(path, server.NewAuthHandler(handlerFunc, auth.AuthenticateUser, authorizeFunc)).Methods(method)
}
func parseFlags() cliFlags {
configPath := flag.String("configPath", ".", "Path of the config file (default: current working directory)")
dbUp := flag.Int("dbUp", -1, "Apply the given number of database migrations (if 0 is specified, all pending migrations will be applied)")
dbDown := flag.Int("dbDown", -1, "Undo the given number of database migration (if 0 is specified, all migration will be undone)")
dbGenerateData := flag.Bool("dbGenerateData", false, "Generate test data")
flag.Parse()
return cliFlags{*configPath, *dbUp, *dbDown, *dbGenerateData}
}
func printCliActionResult(err error) {
if err == nil {
fmt.Println("done!")
} else {
fmt.Println(err.Error())
}
}