-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support swagger * Add swagger fmt * Fix fmt * Setup swagger host according to server port
- Loading branch information
Showing
9 changed files
with
236 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package handler | ||
|
||
import ( | ||
swaggoGen "github.com/OJ-lab/oj-lab-services/application/server/swaggo-gen" | ||
"github.com/gin-gonic/gin" | ||
"github.com/spf13/viper" | ||
swaggerFiles "github.com/swaggo/files" | ||
ginSwagger "github.com/swaggo/gin-swagger" | ||
) | ||
|
||
const ( | ||
servicePortProp = "service.port" | ||
) | ||
|
||
var ( | ||
swaggerHost string | ||
) | ||
|
||
func SetupSwaggoRouter(r *gin.RouterGroup) { | ||
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) | ||
} | ||
|
||
func init() { | ||
sevicePort := viper.GetString(servicePortProp) | ||
swaggerHost = "localhost" + sevicePort | ||
println("Swagger host is set to: " + swaggerHost) | ||
// programmatically set swagger info | ||
swaggoGen.SwaggerInfo.Title = "OJ Lab Services API" | ||
swaggoGen.SwaggerInfo.Version = "1.0" | ||
swaggoGen.SwaggerInfo.Host = swaggerHost | ||
swaggoGen.SwaggerInfo.BasePath = "/api/v1" | ||
swaggoGen.SwaggerInfo.Schemes = []string{"http"} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Package swaggo_gen Code generated by swaggo/swag. DO NOT EDIT | ||
package swaggo_gen | ||
|
||
import "github.com/swaggo/swag" | ||
|
||
const docTemplate = `{ | ||
"schemes": {{ marshal .Schemes }}, | ||
"swagger": "2.0", | ||
"info": { | ||
"description": "{{escape .Description}}", | ||
"title": "{{.Title}}", | ||
"contact": {}, | ||
"version": "{{.Version}}" | ||
}, | ||
"host": "{{.Host}}", | ||
"basePath": "{{.BasePath}}", | ||
"paths": { | ||
"/user/login": { | ||
"post": { | ||
"description": "A Cookie will be set if login successfully", | ||
"consumes": [ | ||
"application/json" | ||
], | ||
"tags": [ | ||
"user" | ||
], | ||
"summary": "Login by account and password", | ||
"parameters": [ | ||
{ | ||
"description": "body", | ||
"name": "loginBody", | ||
"in": "body", | ||
"required": true, | ||
"schema": { | ||
"$ref": "#/definitions/handler.loginBody" | ||
} | ||
} | ||
], | ||
"responses": { | ||
"200": { | ||
"description": "OK" | ||
} | ||
} | ||
} | ||
}, | ||
"/user/me": { | ||
"get": { | ||
"description": "If correctly logined with cookie, return current user", | ||
"tags": [ | ||
"user" | ||
], | ||
"summary": "Get current user", | ||
"responses": { | ||
"200": { | ||
"description": "OK" | ||
}, | ||
"401": { | ||
"description": "Unauthorized" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"definitions": { | ||
"handler.loginBody": { | ||
"type": "object", | ||
"properties": { | ||
"account": { | ||
"type": "string", | ||
"example": "admin" | ||
}, | ||
"password": { | ||
"type": "string", | ||
"example": "admin" | ||
} | ||
} | ||
} | ||
} | ||
}` | ||
|
||
// SwaggerInfo holds exported Swagger Info so clients can modify it | ||
var SwaggerInfo = &swag.Spec{ | ||
Version: "", | ||
Host: "", | ||
BasePath: "", | ||
Schemes: []string{}, | ||
Title: "", | ||
Description: "", | ||
InfoInstanceName: "swagger", | ||
SwaggerTemplate: docTemplate, | ||
LeftDelim: "{{", | ||
RightDelim: "}}", | ||
} | ||
|
||
func init() { | ||
swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.