blob: c0bb33aa217af2b1ea1b0f00dbee5e3102949bf2 [file] [log] [blame]
iomodo5e0dc112021-03-25 20:49:55 +04001package rest
2
3import (
4 "net/http"
5
iomodoa19d4792021-03-26 00:27:25 +04006 "github.com/giolekva/pcloud/core/kg/common"
iomodo5e0dc112021-03-25 20:49:55 +04007 "github.com/gorilla/mux"
8)
9
10const APIURLSuffix = "/api/v1"
11
iomodoa19d4792021-03-26 00:27:25 +040012type Router struct {
13 App common.AppIface
14 Logger common.LoggerIface
15
iomodo5e0dc112021-03-25 20:49:55 +040016 Root *mux.Router // ''
17 APIRoot *mux.Router // 'api/v1'
18 Users *mux.Router // 'api/v1/users'
19 User *mux.Router // 'api/v1/users/{user_id:[A-Za-z0-9]+}'
20}
21
iomodoa19d4792021-03-26 00:27:25 +040022func NewRouter(root *mux.Router, app common.AppIface, logger common.LoggerIface) *Router {
iomodo5e0dc112021-03-25 20:49:55 +040023 apiRoot := root.PathPrefix(APIURLSuffix).Subrouter()
24 users := apiRoot.PathPrefix("/users").Subrouter()
25 user := apiRoot.PathPrefix("/users/{user_id:[A-Za-z0-9]+}").Subrouter()
26
iomodoa19d4792021-03-26 00:27:25 +040027 routers := &Router{
28 App: app,
29 Logger: logger,
30
iomodo5e0dc112021-03-25 20:49:55 +040031 Root: root,
32 APIRoot: apiRoot,
33 Users: users,
34 User: user,
35 }
iomodoa19d4792021-03-26 00:27:25 +040036
iomodo5e0dc112021-03-25 20:49:55 +040037 root.Handle("/api/v1/{anything:.*}", http.HandlerFunc(http.NotFound))
38 routers.initUsers()
39
40 return routers
41}
42
iomodoa19d4792021-03-26 00:27:25 +040043func (router *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
44 router.Root.ServeHTTP(w, req)
iomodo5e0dc112021-03-25 20:49:55 +040045}