github.com/circl-dev/go-swagger@v0.31.0/examples/tutorials/custom-server/cmd/greeter/main.go (about) 1 package main 2 3 import ( 4 "flag" 5 "fmt" 6 "log" 7 8 "github.com/circl-dev/loads" 9 "github.com/circl-dev/runtime/middleware" 10 "github.com/go-openapi/swag" 11 12 "github.com/circl-dev/go-swagger/examples/tutorials/custom-server/gen/restapi" 13 "github.com/circl-dev/go-swagger/examples/tutorials/custom-server/gen/restapi/operations" 14 ) 15 16 var portFlag = flag.Int("port", 3000, "Port to run this service on") 17 18 func main() { 19 // load embedded swagger file 20 swaggerSpec, err := loads.Analyzed(restapi.SwaggerJSON, "") 21 if err != nil { 22 log.Fatalln(err) 23 } 24 25 // create new service API 26 api := operations.NewGreeterAPI(swaggerSpec) 27 server := restapi.NewServer(api) 28 defer func() { 29 _ = server.Shutdown() 30 }() 31 32 // parse flags 33 flag.Parse() 34 // set the port this service will be run on 35 server.Port = *portFlag 36 37 // GetGreetingHandler greets the given name, 38 // in case the name is not given, it will default to World 39 api.GetGreetingHandler = operations.GetGreetingHandlerFunc( 40 func(params operations.GetGreetingParams) middleware.Responder { 41 name := swag.StringValue(params.Name) 42 if name == "" { 43 name = "World" 44 } 45 46 greeting := fmt.Sprintf("Hello, %s!", name) 47 return operations.NewGetGreetingOK().WithPayload(greeting) 48 }) 49 50 // serve API 51 if err := server.Serve(); err != nil { 52 log.Fatalln(err) 53 } 54 }