comparison pkg/controllers/routes.go @ 414:c1047fd04a3a

Moved project specific Go packages to new pkg folder.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 15 Aug 2018 17:30:50 +0200
parents controllers/routes.go@ac23905e64b1
children 6627c48363a0
comparison
equal deleted inserted replaced
413:a9440a4826aa 414:c1047fd04a3a
1 package controllers
2
3 import (
4 "net/http"
5 "net/http/httputil"
6
7 "github.com/gorilla/mux"
8
9 "gemma.intevation.de/gemma/pkg/auth"
10 )
11
12 func BindRoutes(m *mux.Router) {
13
14 api := m.PathPrefix("/api").Subrouter()
15
16 var (
17 sysAdmin = auth.EnsureRole("sys_admin")
18 all = auth.EnsureRole("sys_admin", "waterway_admin", "waterway_user")
19 )
20
21 // User management.
22 api.Handle("/users", all(&JSONHandler{
23 Handle: listUsers,
24 })).Methods(http.MethodGet)
25
26 api.Handle("/users", sysAdmin(&JSONHandler{
27 Input: func() interface{} { return new(User) },
28 Handle: createUser,
29 })).Methods(http.MethodPost)
30
31 api.Handle("/users/{user}", all(&JSONHandler{
32 Handle: listUser,
33 })).Methods(http.MethodGet)
34
35 api.Handle("/users/{user}", all(&JSONHandler{
36 Input: func() interface{} { return new(User) },
37 Handle: updateUser,
38 })).Methods(http.MethodPut)
39
40 api.Handle("/users/{user}", sysAdmin(&JSONHandler{
41 Handle: deleteUser,
42 })).Methods(http.MethodDelete)
43
44 // Password resets.
45 api.Handle("/users/passwordreset", &JSONHandler{
46 Input: func() interface{} { return new(PWResetUser) },
47 Handle: passwordResetRequest,
48 }).Methods(http.MethodPost)
49
50 api.Handle("/users/passwordreset/{hash}", &JSONHandler{
51 Handle: passwordReset,
52 }).Methods(http.MethodGet)
53
54 // Proxy for external WFSs.
55 proxy := &httputil.ReverseProxy{
56 Director: proxyDirector,
57 ModifyResponse: proxyModifyResponse,
58 }
59
60 api.Handle(`/proxy/{hash}/{url}`, proxy).
61 Methods(
62 http.MethodGet, http.MethodPost,
63 http.MethodPut, http.MethodDelete)
64
65 api.Handle("/proxy/{entry}", proxy).
66 Methods(
67 http.MethodGet, http.MethodPost,
68 http.MethodPut, http.MethodDelete)
69
70 // Token handling: Login/Logout.
71 api.HandleFunc("/login", login).
72 Methods(http.MethodGet, http.MethodPost)
73 api.Handle("/logout", auth.SessionMiddleware(http.HandlerFunc(logout))).
74 Methods(http.MethodGet, http.MethodPost)
75 api.Handle("/renew", auth.SessionMiddleware(http.HandlerFunc(renew))).
76 Methods(http.MethodGet, http.MethodPost)
77 }