"github.com/marmotedu/gopractise-demo/swagger/api" // This line is necessary for go-swagger to find your docs! _ "github.com/marmotedu/gopractise-demo/swagger/docs" )
var users []*api.User
funcmain() { r := gin.Default() r.POST("/users", Create) r.GET("/users/:name", Get)
log.Fatal(r.Run(":5555")) }
// Create create a user in memory. funcCreate(c *gin.Context) { var user api.User if err := c.ShouldBindJSON(&user); err != nil { c.JSON(http.StatusBadRequest, gin.H{"message": err.Error(), "code": 10001}) return }
for _, u := range users { if u.Name == user.Name { c.JSON(http.StatusBadRequest, gin.H{"message": fmt.Sprintf("user %s already exist", user.Name), "code": 10001}) return } }
// Get return the detail information for a user. funcGet(c *gin.Context) { username := c.Param("name") for _, u := range users { if u.Name == username { c.JSON(http.StatusOK, u) return } }
c.JSON(http.StatusBadRequest, gin.H{"message": fmt.Sprintf("user %s not exist", username), "code": 10002}) }
Required: true
api/user.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Package api defines the user model. package api
// User represents body of User request and response. type User struct { // User's name. // Required: true Name string`json:"name"`
// swagger:route POST /users user createUserRequest // Create a user in memory. // responses: // 200: createUserResponse // default: errResponse
// swagger:route GET /users/{name} user getUserRequest // Get a user from memory. // responses: // 200: getUserResponse // default: errResponse
// swagger:parameters createUserRequest type userParamsWrapper struct { // This text will appear as description of your request body. // in:body Body api.User }
// This text will appear as description of your request url path. // swagger:parameters getUserRequest type getUserParamsWrapper struct { // in:path Name string`json:"name"` }
// This text will appear as description of your response body. // swagger:response createUserResponse type createUserResponseWrapper struct { // in:body Body api.User }
// This text will appear as description of your response body. // swagger:response getUserResponse type getUserResponseWrapper struct { // in:body Body api.User }
// This text will appear as description of your error response body. // swagger:response errResponse type errResponseWrapper struct { // Error code. Code int`json:"code"`
$ go run cmd/go-user-server/main.go -h Usage: main [OPTIONS]
Documentation of our awesome API.
Application Options: --scheme= the listeners to enable, this can be repeated and defaults to the schemes in the swagger spec --cleanup-timeout= grace period for which to wait before killing idle connections (default: 10s) --graceful-timeout= grace period for which to wait before shutting down the server (default: 15s) --max-header-size= controls the maximum number of bytes the server will read parsing the request header's keys and values, including the request line. It does not limit the size of the request body. (default: 1MiB) --socket-path= the unix socket to listen on (default: /var/run/go-user.sock) --host= the IP to listen on (default: localhost) [$HOST] --port= the port to listen on for insecure connections, defaults to a random value [$PORT] --listen-limit= limit the number of outstanding requests --keep-alive= sets the TCP keep-alive timeouts on accepted connections. It prunes dead TCP connections ( e.g. closing laptop mid-download) (default: 3m) --read-timeout= maximum duration before timing out read of the request (default: 30s) --write-timeout= maximum duration before timing out write of the response (default: 30s) --tls-host= the IP to listen on for tls, when not specified it's the same as --host [$TLS_HOST] --tls-port= the port to listen on for secure connections, defaults to a random value [$TLS_PORT] --tls-certificate= the certificate to use for secure connections [$TLS_CERTIFICATE] --tls-key= the private key to use for secure connections [$TLS_PRIVATE_KEY] --tls-ca= the certificate authority file to be used with mutual tls auth [$TLS_CA_CERTIFICATE] --tls-listen-limit= limit the number of outstanding requests --tls-keep-alive= sets the TCP keep-alive timeouts on accepted connections. It prunes dead TCP connections ( e.g. closing laptop mid-download) --tls-read-timeout= maximum duration before timing out read of the request --tls-write-timeout= maximum duration before timing out write of the response