github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/tequilapi/endpoints/terms.go (about) 1 /* 2 * Copyright (C) 2020 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package endpoints 19 20 import ( 21 "encoding/json" 22 "net/http" 23 24 "github.com/gin-gonic/gin" 25 "github.com/mysteriumnetwork/go-rest/apierror" 26 27 "github.com/mysteriumnetwork/node/config" 28 "github.com/mysteriumnetwork/node/tequilapi/contract" 29 "github.com/rs/zerolog/log" 30 ) 31 32 type termsAPI struct { 33 config configProvider 34 } 35 36 func newTermsAPI(config configProvider) *termsAPI { 37 return &termsAPI{config: config} 38 } 39 40 // GetTerms returns current terms config 41 // 42 // swagger:operation GET /terms Terms getTerms 43 // 44 // --- 45 // summary: Get terms 46 // description: Return an object with the current terms config 47 // responses: 48 // 200: 49 // description: Terms object 50 // schema: 51 // "$ref": "#/definitions/TermsResponse" 52 func (api *termsAPI) GetTerms(c *gin.Context) { 53 c.JSON(http.StatusOK, contract.NewTermsResp()) 54 } 55 56 // UpdateTerms accepts new terms and updates user config 57 // 58 // swagger:operation POST /terms Terms updateTerms 59 // 60 // --- 61 // summary: Update terms agreement 62 // description: Takes the given data and tries to update terms agreement config. 63 // parameters: 64 // - in: body 65 // name: body 66 // description: Required data to update terms 67 // schema: 68 // $ref: "#/definitions/TermsRequest" 69 // responses: 70 // 200: 71 // description: Terms agreement updated 72 // 400: 73 // description: Failed to parse or request validation failed 74 // schema: 75 // "$ref": "#/definitions/APIError" 76 // 500: 77 // description: Internal server error 78 // schema: 79 // "$ref": "#/definitions/APIError" 80 func (api *termsAPI) UpdateTerms(c *gin.Context) { 81 var req contract.TermsRequest 82 err := json.NewDecoder(c.Request.Body).Decode(&req) 83 if err != nil { 84 c.Error(apierror.ParseFailed()) 85 return 86 } 87 88 for k, v := range req.ToMap() { 89 log.Debug().Msgf("Setting user config value: %q = %q", k, v) 90 api.config.SetUser(k, v) 91 } 92 93 err = api.config.SaveUserConfig() 94 if err != nil { 95 c.Error(apierror.Internal("Failed to save config", contract.ErrCodeConfigSave)) 96 return 97 } 98 c.Status(http.StatusOK) 99 } 100 101 // AddRoutesForTerms registers /terms endpoints in Tequilapi 102 func AddRoutesForTerms(e *gin.Engine) error { 103 api := newTermsAPI(config.Current) 104 105 g := e.Group("/terms") 106 g.GET("", api.GetTerms) 107 g.POST("", api.UpdateTerms) 108 return nil 109 }