github.com/pavlo67/common@v0.5.3/common/server_http/config.go (about) 1 package server_http 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "os" 7 "strings" 8 9 "github.com/pavlo67/common/common" 10 "github.com/pavlo67/common/common/logger" 11 ) 12 13 type ConfigStarter struct { 14 Port int `yaml:"port" json:"port"` 15 NoHTTPS bool `yaml:"no_https" json:"no_https"` 16 KeyPath string `yaml:"key_path" json:"key_path"` 17 TLSCertFile string `yaml:"tls_cert_file" json:"tls_cert_file"` 18 TLSKeyFile string `yaml:"tls_key_file" json:"tls_key_file"` 19 } 20 21 type Config struct { 22 ConfigCommon 23 EndpointsSettled 24 } 25 26 type ConfigCommon struct { 27 Title string 28 Version string 29 Host string 30 Port string 31 Prefix string 32 } 33 34 // Swagger ----------------------------------------------------------------------------- 35 36 type Swagger map[string]interface{} 37 38 func (c Config) SwaggerV2(isHTTPS bool) ([]byte, error) { 39 paths := map[string]common.Map{} // map[string]map[string]map[string]interface{}{} 40 41 for key, ep := range c.EndpointsSettled { 42 43 path := c.Prefix + ep.Endpoint.PathTemplateBraced(ep.Path) 44 method := strings.ToLower(ep.Endpoint.Method) 45 46 epDescr := common.Map{ 47 "operationId": key, 48 "tags": ep.Tags, 49 } 50 51 if len(ep.Produces) >= 1 { 52 epDescr["produces"] = ep.Produces 53 } else { 54 epDescr["produces"] = []string{"application/json"} 55 } 56 57 var parameters []interface{} // []map[string]interface{} 58 59 for _, pp := range ep.Endpoint.PathParams { 60 if len(pp) > 0 && pp[0] == '*' { 61 pp = pp[1:] 62 } 63 64 parameters = append( 65 parameters, 66 common.Map{ 67 "in": "path", 68 "required": true, 69 "name": pp, 70 "type": "string", 71 "description": "", // TODO!!! 72 }, 73 ) 74 } 75 for _, qp := range ep.Endpoint.QueryParams { 76 parameters = append( 77 parameters, 78 common.Map{ 79 "in": "query", 80 "required": false, // TODO!!! 81 "name": qp, 82 "type": "string", 83 "description": "", // TODO!!! 84 }, 85 ) 86 } 87 88 if method == "post" { 89 if len(ep.Endpoint.BodyParams) > 0 { 90 parameters = append(parameters, ep.Endpoint.BodyParams) 91 } else { 92 parameters = append(parameters, common.Map{ 93 "in": "body", 94 "required": true, 95 "name": "body_item", 96 "type": "string", 97 }) 98 } 99 } 100 101 if len(parameters) > 0 { 102 epDescr["parameters"] = parameters 103 } 104 105 if epDescrPrev, ok := paths[path][method]; ok { 106 return nil, fmt.Errorf("duplicate endpoint description (%s %s): \n%#v\nvs.\n%#v", method, path, epDescrPrev, epDescr) 107 } 108 if _, ok := paths[path]; ok { // pathPrev 109 paths[path][method] = epDescr 110 } else { 111 paths[path] = common.Map{method: epDescr} // map[string]map[string]interface{} 112 } 113 } 114 115 var schemes []string 116 if isHTTPS { 117 schemes = []string{"https", "http"} 118 } else { 119 schemes = []string{"http"} 120 } 121 122 swagger := Swagger{ 123 "swagger": "2.0", 124 "info": map[string]string{ 125 "title": c.Title, 126 "version": c.Version, 127 }, 128 // "basePath": c.Prefix, 129 "schemes": schemes, 130 "port": c.Port, 131 "paths": paths, 132 } 133 134 return json.MarshalIndent(swagger, "", " ") 135 } 136 137 func (c Config) InitSwagger(isHTTPS bool, swaggerStaticFilePath string, l logger.Operator) error { 138 //if c == nil { 139 // return nil 140 //} 141 142 swaggerJSON, err := c.SwaggerV2(isHTTPS) 143 if err != nil { 144 return err 145 } 146 if err = os.WriteFile(swaggerStaticFilePath, swaggerJSON, 0644); err != nil { 147 return fmt.Errorf("on ioutil.WriteFile(%s, %s, 0644): %s", swaggerStaticFilePath, swaggerJSON, err) 148 } 149 l.Infof("%d bytes are written into %s", len(swaggerJSON), swaggerStaticFilePath) 150 151 return nil 152 } 153 154 //func (c *Config) SwaggerV2(isHTTPS bool) ([]byte, error) { 155 // 156 // if c == nil { 157 // return nil, nil 158 // } 159 // 160 // paths := map[string]common.Map{} // map[string]map[string]map[string]interface{}{} 161 // 162 // for key, ep := range c.EndpointsSettled { 163 // 164 // path := c.Prefix + ep.PathTemplateBraced(ep.Path) 165 // method := strings.ToLower(ep.Method) 166 // 167 // epDescr := common.Map{ 168 // "operationId": key, 169 // "tags": ep.Tags, 170 // } 171 // 172 // if len(ep.Produces) >= 1 { 173 // epDescr["produces"] = ep.Produces 174 // } else { 175 // epDescr["produces"] = []string{"application/json"} 176 // } 177 // 178 // var parameters []interface{} // []map[string]interface{} 179 // 180 // for _, pp := range ep.PathParams { 181 // if len(pp) > 0 && pp[0] == '*' { 182 // pp = pp[1:] 183 // } 184 // 185 // parameters = append( 186 // parameters, 187 // common.Map{ 188 // "in": "path", 189 // "required": true, 190 // "name": pp, 191 // "type": "string", 192 // "description": "", // TODO!!! 193 // }, 194 // ) 195 // } 196 // for _, qp := range ep.QueryParams { 197 // parameters = append( 198 // parameters, 199 // common.Map{ 200 // "in": "query", 201 // "required": false, // TODO!!! 202 // "name": qp, 203 // "type": "string", 204 // "description": "", // TODO!!! 205 // }, 206 // ) 207 // } 208 // 209 // if method == "post" { 210 // if len(ep.BodyParams) > 0 { 211 // parameters = append(parameters, ep.BodyParams) 212 // } else { 213 // parameters = append(parameters, common.Map{ 214 // "in": "body", 215 // "required": true, 216 // "name": "body_item", 217 // "type": "string", 218 // }) 219 // } 220 // } 221 // 222 // if len(parameters) > 0 { 223 // epDescr["parameters"] = parameters 224 // } 225 // 226 // if epDescrPrev, ok := paths[path][method]; ok { 227 // return nil, fmt.Errorf("duplicate endpoint description (%s %s): \n%#v\nvs.\n%#v", method, path, epDescrPrev, epDescr) 228 // } 229 // if _, ok := paths[path]; ok { // pathPrev 230 // paths[path][method] = epDescr 231 // } else { 232 // paths[path] = common.Map{method: epDescr} // map[string]map[string]interface{} 233 // } 234 // } 235 // 236 // var schemes []string 237 // if isHTTPS { 238 // schemes = []string{"https", "http"} 239 // } else { 240 // schemes = []string{"http"} 241 // } 242 // 243 // swagger := Swagger{ 244 // "swagger": "2.0", 245 // "info": map[string]string{ 246 // "title": c.Title, 247 // "version": c.Version, 248 // }, 249 // // "basePath": c.Prefix, 250 // "schemes": schemes, 251 // "port": c.Port, 252 // "paths": paths, 253 // } 254 // 255 // return json.MarshalIndent(swagger, "", " ") 256 //} 257 //