MCPcopy
hub / github.com/spacecloud-io/space-cloud / handleApplyService

Method handleApplyService

runner/server/handle.go:91–136  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

89}
90
91func (s *Server) handleApplyService() http.HandlerFunc {
92 return func(w http.ResponseWriter, r *http.Request) {
93
94 // Close the body of the request
95 defer utils.CloseTheCloser(r.Body)
96
97 ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
98 defer cancel()
99
100 // Verify token
101 _, err := s.auth.VerifyToken(utils.GetToken(r))
102 if err != nil {
103 _ = helpers.Logger.LogError(helpers.GetRequestID(ctx), "Failed to apply service", err, nil)
104 _ = helpers.Response.SendErrorResponse(ctx, w, http.StatusUnauthorized, err)
105 return
106 }
107
108 vars := mux.Vars(r)
109 projectID := vars["project"]
110 serviceID := vars["serviceId"]
111 version := vars["version"]
112
113 // Parse request body
114 service := new(model.Service)
115 if err := json.NewDecoder(r.Body).Decode(service); err != nil {
116 _ = helpers.Logger.LogError(helpers.GetRequestID(ctx), "Failed to apply service", err, nil)
117 _ = helpers.Response.SendErrorResponse(ctx, w, http.StatusBadRequest, err)
118 return
119 }
120
121 service.ProjectID = projectID
122 service.ID = serviceID
123 service.Version = version
124
125 // TODO: Override the project id present in the service object with the one present in the token if user not admin
126
127 // Apply the service config
128 if err := s.driver.ApplyService(ctx, service); err != nil {
129 _ = helpers.Logger.LogError(helpers.GetRequestID(ctx), "Failed to apply service", err, nil)
130 _ = helpers.Response.SendErrorResponse(ctx, w, http.StatusInternalServerError, err)
131 return
132 }
133
134 _ = helpers.Response.SendOkayResponse(ctx, http.StatusOK, w)
135 }
136}
137
138func (s *Server) handleGetLogs() http.HandlerFunc {
139 return func(w http.ResponseWriter, r *http.Request) {

Callers 1

routesMethod · 0.95

Calls 2

VerifyTokenMethod · 0.80
ApplyServiceMethod · 0.65

Tested by

no test coverage detected