diff --git a/docs/docs.go b/docs/docs.go index 788fa4b..86c2b29 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -55,7 +55,7 @@ const docTemplate = `{ "in": "body", "required": true, "schema": { - "$ref": "#/definitions/swagger_search.Search" + "$ref": "#/definitions/repository.Search" } } ], @@ -63,19 +63,54 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/swagger_search.Search" + "$ref": "#/definitions/repository.Search" } }, "400": { - "description": "We need ID!!", + "description": "Bad Request", "schema": { - "$ref": "#/definitions/swagger_search.APIError" + "type": "object" } }, "404": { - "description": "Can not find ID", + "description": "Not Found", "schema": { - "$ref": "#/definitions/swagger_search.APIError" + "type": "object" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "type": "object" + } + } + } + } + }, + "/health": { + "get": { + "description": "search engine api", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Search" + ], + "summary": "search engine api", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "object" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "type": "object" } } } @@ -135,18 +170,7 @@ const docTemplate = `{ } } }, - "swagger_search.APIError": { - "type": "object", - "properties": { - "errorCode": { - "type": "integer" - }, - "errorMessage": { - "type": "string" - } - } - }, - "swagger_search.Search": { + "repository.Search": { "type": "object", "properties": { "age": { diff --git a/docs/swagger.json b/docs/swagger.json index f6fced8..14fdc7a 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -44,7 +44,7 @@ "in": "body", "required": true, "schema": { - "$ref": "#/definitions/swagger_search.Search" + "$ref": "#/definitions/repository.Search" } } ], @@ -52,19 +52,54 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/swagger_search.Search" + "$ref": "#/definitions/repository.Search" } }, "400": { - "description": "We need ID!!", + "description": "Bad Request", "schema": { - "$ref": "#/definitions/swagger_search.APIError" + "type": "object" } }, "404": { - "description": "Can not find ID", + "description": "Not Found", "schema": { - "$ref": "#/definitions/swagger_search.APIError" + "type": "object" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "type": "object" + } + } + } + } + }, + "/health": { + "get": { + "description": "search engine api", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Search" + ], + "summary": "search engine api", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "object" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "type": "object" } } } @@ -124,18 +159,7 @@ } } }, - "swagger_search.APIError": { - "type": "object", - "properties": { - "errorCode": { - "type": "integer" - }, - "errorMessage": { - "type": "string" - } - } - }, - "swagger_search.Search": { + "repository.Search": { "type": "object", "properties": { "age": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 8593242..5b4371c 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -14,14 +14,7 @@ definitions: example: John type: string type: object - swagger_search.APIError: - properties: - errorCode: - type: integer - errorMessage: - type: string - type: object - swagger_search.Search: + repository.Search: properties: age: description: Age @@ -63,22 +56,45 @@ paths: name: search required: true schema: - $ref: '#/definitions/swagger_search.Search' + $ref: '#/definitions/repository.Search' produces: - application/json responses: "200": description: OK schema: - $ref: '#/definitions/swagger_search.Search' + $ref: '#/definitions/repository.Search' "400": - description: We need ID!! + description: Bad Request schema: - $ref: '#/definitions/swagger_search.APIError' + type: object "404": - description: Can not find ID + description: Not Found + schema: + type: object + "500": + description: Internal Server Error + schema: + type: object + summary: search engine api + tags: + - Search + /health: + get: + consumes: + - application/json + description: search engine api + produces: + - application/json + responses: + "200": + description: OK + schema: + type: object + "500": + description: Internal Server Error schema: - $ref: '#/definitions/swagger_search.APIError' + type: object summary: search engine api tags: - Search diff --git a/lib/util/util.go b/lib/util/util.go index 768b754..1597ede 100644 --- a/lib/util/util.go +++ b/lib/util/util.go @@ -10,12 +10,12 @@ import ( func Set_Env(initial_str string, replace_str string) (string) { - transform_str := "" + transform_str := initial_str if initial_str == "" { transform_str = replace_str } log.Println("Set_Env : ", transform_str) - return replace_str + return transform_str } func ReplaceStr(str string) (string) { diff --git a/repository/model.go b/repository/model.go new file mode 100644 index 0000000..484c161 --- /dev/null +++ b/repository/model.go @@ -0,0 +1,22 @@ +package repository + +import "errors" + + +var ( + IndexNameEmptyStringError = errors.New("index name cannot be empty string") + IndexAlreadyExistsError = errors.New("elasticsearch index already exists") + ESInstanceError = errors.New("elasticsearch goes down") +) + +type Search struct { + Id int `json:"id" example:"1"` // UserId + Name string `json:"name" example:"John"` // Name + Age int `json:"age" example:"10"` // Age +} + + +type APIError struct { + ErrorCode int + ErrorMessage string +} \ No newline at end of file diff --git a/swagger.go b/swagger.go index f543ccf..4460b48 100644 --- a/swagger.go +++ b/swagger.go @@ -6,7 +6,7 @@ import ( "net/http" "os" - swagger_search "go-search_engine/swagger_controller" + controller "go-search_engine/swagger_controller" "github.com/gin-gonic/gin" swaggerFiles "github.com/swaggo/files" @@ -46,7 +46,8 @@ func main() { v1Search := r.Group("/") { - v1Search.POST("/es/search", swagger_search.SearchHandler) + v1Search.GET("/health", controller.HealthHandler) + v1Search.POST("/es/search", controller.SearchHandler) } httpPort := os.Getenv("PORT") diff --git a/swagger_controller/search_controller.go b/swagger_controller/search_controller.go index c78cd9b..0b4a782 100644 --- a/swagger_controller/search_controller.go +++ b/swagger_controller/search_controller.go @@ -1,23 +1,19 @@ -package swagger_search +package controller import ( + "go-search_engine/repository" + "log" "net/http" + "os" + "reflect" + "time" + + my_elasticsearch "go-search_engine/lib/elasticsearch" + "go-search_engine/lib/util" "github.com/gin-gonic/gin" ) -type Search struct { - Id int `json:"id" example:"1"` // UserId - Name string `json:"name" example:"John"` // Name - Age int `json:"age" example:"10"` // Age -} - - -type APIError struct { - ErrorCode int - ErrorMessage string -} - /* // SearchHandler godoc // @Summary search engine api @@ -34,6 +30,32 @@ func SearchHandler(c *gin.Context) { } */ +// HealthHandler godoc +// @Summary search engine api +// @tags Search +// @Description search engine api +// @Accept json +// @Produce json +// @Router /health [get] +// @content application/json +// @Success 200 {object} object +// @Failure 500 {object} object +func HealthHandler(c *gin.Context) { + + es_host := util.Set_Env(os.Getenv("ES_HOST"), "http://localhost:9209") + es_client := my_elasticsearch.Get_es_instance(es_host) + + res, err := es_client.Info() + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"message": repository.ESInstanceError.Error()}) + log.Printf("Error getting response: %s", err) + return + } + + log.Println(res) + c.JSON(http.StatusOK, gin.H{"message": res}) +} + // SearchHandler godoc // @Summary search engine api // @tags Search @@ -41,17 +63,33 @@ func SearchHandler(c *gin.Context) { // @Accept json // @Produce json // @Router /es/search [post] -// @Param search body Search true "Search Info Body" +// @Param search body repository.Search true "Search Info Body" // @content application/json -// @Success 200 {object} Search -// @Failure 400 {object} APIError "We need ID!!" -// @Failure 404 {object} APIError "Can not find ID" +// @Success 200 {object} repository.Search +// @Failure 400,404,500 {object} object +/* +// @Failure 400 {object} repository.APIError "We need ID!!" +// @Failure 404 {object} repository.APIError "Can not find ID" +*/ func SearchHandler(c *gin.Context) { - var search Search + // Starting time request + startTime := time.Now() + + var search repository.Search if err := c.BindJSON(&search); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } + + // End Time request + endTime := time.Now() + // execution time + latencyTime := endTime.Sub(startTime) + // c.JSON(http.StatusOK, gin.H{"message": "success"}) + log.Printf("Parsing : %s", reflect.TypeOf(search)) + log.Printf("Excuting Time : %s", latencyTime) c.JSON(http.StatusOK, gin.H{"message": search}) } + + diff --git a/swagger_service_start.sh b/swagger_service_start.sh index 9b36f18..fbbac47 100755 --- a/swagger_service_start.sh +++ b/swagger_service_start.sh @@ -1,5 +1,10 @@ #!/bin/bash set -e + +# -- +export PATH=$(go env GOPATH)/bin:$PATH +# -- + source ./DevOps_Shell/read_config.sh # -- @@ -12,6 +17,7 @@ SCRIPTDIR="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" # -- # swag init +# Finally, it’s time to generate the docs! All you need is one command — swag init # -- @@ -24,7 +30,8 @@ if [[ -z "$PORT" ]]; then echo $PORT fi -# export PORT=9088 +# export ES_HOST=9206 +# echo $ES_HOST -go run ./swagger.go $PORT +go run ./swagger.go # ./swagger