Initial Project Structure
[aaf/sms.git] / sms-service / src / sms / handler / handler.go
1 /*
2  * Copyright 2018 Intel Corporation, Inc
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package handler
18
19 import (
20         "encoding/json"
21         "net/http"
22
23         "sms/backend"
24
25         "github.com/gorilla/mux"
26 )
27
28 type secretDomainJSON struct {
29         name string
30 }
31
32 type secretKeyValue struct {
33         name  string
34         value string
35 }
36
37 type secretJSON struct {
38         name   string
39         values []secretKeyValue
40 }
41
42 type handler struct {
43         secretBackend backend.SecretBackend
44         loginBackend  backend.LoginBackend
45 }
46
47 // GetSecretDomainHandler returns list of secret domains
48 func (h handler) GetSecretDomainHandler(w http.ResponseWriter, r *http.Request) {
49
50 }
51
52 // CreateSecretDomainHandler creates a secret domain with a name provided
53 func (h handler) CreateSecretDomainHandler(w http.ResponseWriter, r *http.Request) {
54         var d secretDomainJSON
55
56         err := json.NewDecoder(r.Body).Decode(&d)
57         if err != nil {
58                 http.Error(w, err.Error(), 400)
59                 return
60         }
61 }
62
63 // DeleteSecretDomainHandler deletes a secret domain with the ID provided
64 func (h handler) DeleteSecretDomainHandler(w http.ResponseWriter, r *http.Request) {
65
66 }
67
68 // struct that tracks various status items for SMS and backend
69 type status struct {
70         Seal bool `json:"sealstatus"`
71 }
72
73 // StatusHandler returns information related to SMS and SMS backend services
74 func (h handler) StatusHandler(w http.ResponseWriter, r *http.Request) {
75         s := h.secretBackend.GetStatus()
76         status := status{Seal: s}
77         err := json.NewEncoder(w).Encode(status)
78         if err != nil {
79                 http.Error(w, err.Error(), 400)
80                 return
81         }
82 }
83
84 // LoginHandler handles login via password and username
85 func (h handler) LoginHandler(w http.ResponseWriter, r *http.Request) {
86
87 }
88
89 // CreateRouter returns an http.Handler for the registered URLs
90 func CreateRouter(b backend.SecretBackend) http.Handler {
91         h := handler{secretBackend: b}
92
93         // Create a new mux to handle URL endpoints
94         router := mux.NewRouter()
95
96         router.HandleFunc("/v1/sms/login", h.LoginHandler).Methods("POST")
97
98         router.HandleFunc("/v1/sms/status", h.StatusHandler).Methods("GET")
99
100         router.HandleFunc("/v1/sms/domain", h.GetSecretDomainHandler).Methods("GET")
101         router.HandleFunc("/v1/sms/domain", h.CreateSecretDomainHandler).Methods("POST")
102         router.HandleFunc("/v1/sms/domain/{domName}", h.DeleteSecretDomainHandler).Methods("DELETE")
103
104         return router
105 }