github.com/josephspurrier/go-swagger@v0.2.1-0.20221129144919-1f672a142a00/cmd/swagger/commands/generate/server.go (about) 1 // Copyright 2015 go-swagger maintainers 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package generate 16 17 import ( 18 "log" 19 "strings" 20 21 "github.com/go-swagger/go-swagger/generator" 22 ) 23 24 type serverOptions struct { 25 ServerPackage string `long:"server-package" short:"s" description:"the package to save the server specific code" default:"restapi"` 26 MainTarget string `long:"main-package" short:"" description:"the location of the generated main. Defaults to cmd/{name}-server" default:""` 27 ImplementationPackage string `long:"implementation-package" short:"" description:"the location of the backend implementation of the server, which will be autowired with api" default:""` 28 } 29 30 func (cs serverOptions) apply(opts *generator.GenOpts) { 31 opts.ServerPackage = cs.ServerPackage 32 } 33 34 // Server the command to generate an entire server application 35 type Server struct { 36 WithShared 37 WithModels 38 WithOperations 39 40 serverOptions 41 schemeOptions 42 mediaOptions 43 44 SkipModels bool `long:"skip-models" description:"no models will be generated when this flag is specified"` 45 SkipOperations bool `long:"skip-operations" description:"no operations will be generated when this flag is specified"` 46 SkipSupport bool `long:"skip-support" description:"no supporting files will be generated when this flag is specified"` 47 ExcludeMain bool `long:"exclude-main" description:"exclude main function, so just generate the library"` 48 ExcludeSpec bool `long:"exclude-spec" description:"don't embed the swagger specification"` 49 FlagStrategy string `long:"flag-strategy" description:"the strategy to provide flags for the server" default:"go-flags" choice:"go-flags" choice:"pflag" choice:"flag"` // nolint: staticcheck 50 CompatibilityMode string `long:"compatibility-mode" description:"the compatibility mode for the tls server" default:"modern" choice:"modern" choice:"intermediate"` // nolint: staticcheck 51 RegenerateConfigureAPI bool `long:"regenerate-configureapi" description:"Force regeneration of configureapi.go"` 52 53 Name string `long:"name" short:"A" description:"the name of the application, defaults to a mangled value of info.title"` 54 // TODO(fredbi): CmdName string `long:"cmd-name" short:"A" description:"the name of the server command, when main is generated (defaults to {name}-server)"` 55 56 // deprecated flags 57 WithContext bool `long:"with-context" description:"handlers get a context as first arg (deprecated)"` 58 } 59 60 func (s Server) apply(opts *generator.GenOpts) { 61 if s.WithContext { 62 log.Printf("warning: deprecated option --with-context is ignored") 63 } 64 65 s.Shared.apply(opts) 66 s.Models.apply(opts) 67 s.Operations.apply(opts) 68 s.serverOptions.apply(opts) 69 s.schemeOptions.apply(opts) 70 s.mediaOptions.apply(opts) 71 72 opts.IncludeModel = !s.SkipModels 73 opts.IncludeValidator = !s.SkipModels 74 opts.IncludeHandler = !s.SkipOperations 75 opts.IncludeParameters = !s.SkipOperations 76 opts.IncludeResponses = !s.SkipOperations 77 opts.IncludeURLBuilder = !s.SkipOperations 78 opts.IncludeSupport = !s.SkipSupport 79 opts.IncludeMain = !s.ExcludeMain 80 opts.ExcludeSpec = s.ExcludeSpec 81 opts.FlagStrategy = s.FlagStrategy 82 opts.CompatibilityMode = s.CompatibilityMode 83 opts.RegenerateConfigureAPI = s.RegenerateConfigureAPI 84 85 opts.Name = s.Name 86 opts.MainPackage = s.MainTarget 87 88 opts.ImplementationPackage = s.ImplementationPackage 89 } 90 91 func (s *Server) generate(opts *generator.GenOpts) error { 92 return generator.GenerateServer(s.Name, s.Models.Models, s.Operations.Operations, opts) 93 } 94 95 func (s Server) log(rp string) { 96 var flagsPackage string 97 switch { 98 case strings.HasPrefix(s.FlagStrategy, "pflag"): 99 flagsPackage = "github.com/spf13/pflag" 100 case strings.HasPrefix(s.FlagStrategy, "flag"): 101 flagsPackage = "flag" 102 default: 103 flagsPackage = "github.com/jessevdk/go-flags" 104 } 105 106 log.Println(`Generation completed! 107 108 For this generation to compile you need to have some packages in your go.mod: 109 110 * github.com/go-openapi/runtime 111 * ` + flagsPackage + ` 112 113 You can get these now with: go mod tidy`) 114 } 115 116 // Execute runs this command 117 func (s *Server) Execute(args []string) error { 118 return createSwagger(s) 119 }