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