Added seed code for caas-helm.
[ta/caas-helm.git] / src / chart-repo-handler / pkg / api / routers.go
1 // Copyright 2019 Nokia
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package api
16
17 import (
18         "net/http"
19         "pkg/config"
20
21         "github.com/gorilla/mux"
22         "github.com/ncw/swift"
23 )
24
25 type Route struct {
26         Name        string
27         Method      string
28         Pattern     string
29         HandlerFunc func(*swift.Connection, config.EnvConfig) http.HandlerFunc
30 }
31
32 type Routes []Route
33
34 var routes = Routes{
35         Route{
36                 "Index",
37                 "GET",
38                 "/",
39                 Index,
40         },
41
42         Route{
43                 "ObjectGet",
44                 "GET",
45                 // tonyaw: make it can match slash, such as "aaa/bbb"
46                 "/{id:.*}",
47                 ObjectGet,
48         },
49         Route{
50                 "ObjectPut",
51                 "POST",
52                 // tonyaw: make it can match slash, such as "aaa/bbb"
53                 "/{id:.*}",
54                 ObjectPut,
55         },
56         Route{
57                 "ObjectPut",
58                 "PUT",
59                 // tonyaw: make it can match slash, such as "aaa/bbb"
60                 "/{id:.*}",
61                 ObjectPut,
62         },
63         Route{
64                 "ObjectDelete",
65                 "DELETE",
66                 "/{id:.*}",
67                 ObjectDelete,
68         },
69 }
70
71 func NewRouter(c *swift.Connection, config config.EnvConfig) *mux.Router {
72         router := mux.NewRouter().StrictSlash(true)
73         for _, route := range routes {
74                 var handler http.Handler
75                 handler = route.HandlerFunc(c, config)
76                 handler = Logger(handler, route.Name)
77
78                 router.
79                         Methods(route.Method).
80                         Path(route.Pattern).
81                         Name(route.Name).
82                         Handler(handler)
83         }
84
85         return router
86 }