2 * Copyright 2020 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 "github.com/gorilla/mux"
23 "github.com/sirupsen/logrus"
28 CreateAppInstance = "/ealtedge/mepm/app_lcm/v1/app_instances"
29 InstantiateAppInstance = "/ealtedge/mepm/app_lcm/v1/app_instances/{appInstanceId}/instantiate"
30 QueryAppInstanceInfo = "/ealtedge/mepm/app_lcm/v1/app_instances/{appInstanceId}"
31 QueryAppLcmOperationStatus = "/ealtedge/mepm/app_lcm/v1/app_lcm_op_occs"
32 TerminateAppIns = "/ealtedge/mepm/app_lcm/v1/app_instances/{appInstanceId}/terminate"
33 DeleteAppInstanceIdentifier = "/ealtedge/mepm/app_lcm/v1/app_instances/{appInstanceId}"
34 OnboardPackage = "/ealtedge/mepm/app_pkgm/v1/app_packages"
35 QueryOnboardPackage = "/ealtedge/mepm/app_pkgm/v1/app_packages/{appPkgId}"
36 // Https flag value true
37 HTTPSFlagValue = "true"
41 PackageFolderPath = os.Getenv("PACKAGE_PATH")
42 PackageArtifactPath = os.Getenv("PACKAGE_ARTIFACT_PATH")
45 // Handler of REST APIs
46 type Handlers struct {
52 // Initialize initializes the handler
53 func (hdlr *Handlers) Initialize(logger *logrus.Logger) {
54 hdlr.router = mux.NewRouter()
57 hdlr.impl = newHandlerImpl(hdlr.logger)
61 func (hdlr *Handlers) Run(host string) {
62 hdlr.logger.Infof("Server is running on port %s", host)
64 var httpflag = os.Getenv("HTTPS_FLAG")
65 if httpflag == HTTPSFlagValue {
66 err = http.ListenAndServeTLS(host, os.Getenv("CERTIFICATE_PATH"), os.Getenv("KEY_PATH"), hdlr.router)
68 err = http.ListenAndServe(host, hdlr.router)
71 hdlr.logger.Fatalf("Server couldn't run on port %s", host)
75 // SetRouters sets the all required routers
76 func (hdlr *Handlers) setRouters() {
77 // Routing for handling the requests
78 hdlr.Post(OnboardPackage, hdlr.handleRequest(hdlr.impl.UploadPackage))
79 hdlr.Get(QueryOnboardPackage, hdlr.handleRequest(hdlr.impl.QueryAppPackageInfo))
80 hdlr.Post(CreateAppInstance, hdlr.handleRequest(hdlr.impl.CreateAppInstance))
81 hdlr.Delete(QueryOnboardPackage, hdlr.handleRequest(hdlr.impl.DeleteAppPackage))
82 hdlr.Post(InstantiateAppInstance, hdlr.handleRequest(hdlr.impl.InstantiateAppInstance))
83 hdlr.Get(QueryAppInstanceInfo, hdlr.handleRequest(hdlr.impl.QueryAppInstanceInfo))
84 hdlr.Get(QueryAppLcmOperationStatus, hdlr.handleRequest(hdlr.impl.QueryAppLcmOperationStatus))
85 hdlr.Post(TerminateAppIns, hdlr.handleRequest(hdlr.impl.TerminateAppInstance))
86 hdlr.Delete(DeleteAppInstanceIdentifier, hdlr.handleRequest(hdlr.impl.DeleteAppInstanceIdentifier))
89 // Get wraps the router for GET method
90 func (hdlr *Handlers) Get(path string, f func(w http.ResponseWriter, r *http.Request)) {
91 hdlr.router.HandleFunc(path, f).Methods("GET")
94 // Post wraps the router for POST method
95 func (hdlr *Handlers) Post(path string, f func(w http.ResponseWriter, r *http.Request)) {
96 hdlr.router.HandleFunc(path, f).Methods("POST")
99 // Put wraps the router for PUT method
100 func (hdlr *Handlers) Put(path string, f func(w http.ResponseWriter, r *http.Request)) {
101 hdlr.router.HandleFunc(path, f).Methods("PUT")
104 // Delete wraps the router for DELETE method
105 func (hdlr *Handlers) Delete(path string, f func(w http.ResponseWriter, r *http.Request)) {
106 hdlr.router.HandleFunc(path, f).Methods("DELETE")
109 type RequestHandlerFunction func(w http.ResponseWriter, r *http.Request)
111 func (hdlr *Handlers) handleRequest(handler RequestHandlerFunction) http.HandlerFunc {
112 return func(w http.ResponseWriter, r *http.Request) {