Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #82 from bancodobrasil/develop
Browse files Browse the repository at this point in the history
v1.8.2
  • Loading branch information
eduardaguterres authored Jul 21, 2023
2 parents 86d198b + 4391f0a commit a09798d
Show file tree
Hide file tree
Showing 35 changed files with 891 additions and 267 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ _data/
.env.local
.json
.vscode

.DS_Store
data/

__debug_bin
45 changes: 45 additions & 0 deletions README-PTBR.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

[![Go Reference](https://pkg.go.dev/badge/github.com/abu-lang/goabu.svg)](https://pkg.go.dev/github.com/bancodobrasil/featws-api)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/bancodobrasil/featws-api/blob/develop/LICENSE)

# Featws API [![About_en](https://github.com/yammadev/flag-icons/blob/master/png/BR.png?raw=true)](https://github.com/bancodobrasil/featws-api/blob/develop/README.md)


## Como executar

Para executar este projeto, você precisa ter certos pré-requisitos configurados em sua máquina. Esses pré-requisitos incluem:

- [Golang](https://go.dev/doc/install)
- [Swaggo](https://github.com/swaggo/swag/blob/master/README_pt.md#come%C3%A7ando)
- [Docker](https://www.docker.com/)


Para executar o projeto, siga estes passos:

- Abra o terminal no diretório do projeto e execute o comando `go mod tidy` para garantir que todas as dependências necessárias estejam instaladas.

- Em seguida, execute o comando `swag init` para inicializar o Swagger e gerar a documentação da API necessária.

- Abra o Docker da sua máquina, ou algum semelhante, para garantir que ele está funcionado. Logo, abra o terminal no repositório do projeto e rode `docker compose up`.

- Após os containers serem iniciados e estarem rodando, execute o comando `make run` no terminal do projeto para iniciar o projeto.

O projeto será executado em `localhost:9007`. Para acessar a documentação do Swagger, [clique aqui](http://localhost:9007/swagger/index.html#/).

Seguindo estes passos, o projeto estará em execução e você poderá acessar a documentação da API através do Swagger.

## GoDoc

Para acessar a documentação do GoDoc, primeiro instale o GoDoc na sua máquina. Abra um terminal e digite:

````
go get golang.org/x/tools/cmd/godoc
````

Em seguida rode no terminal do repositório o comando a seguir:

````
godoc -http=:6060
````

O GoDoc será executado em `localhost:6060`. Para acessar a documentação do GoDoc, basta [clicar aqui](http://localhost:6060/pkg/).
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,42 @@
# featws-backoffice-api
[![Go Reference](https://pkg.go.dev/badge/github.com/abu-lang/goabu.svg)](https://pkg.go.dev/github.com/bancodobrasil/featws-api)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/bancodobrasil/featws-api/blob/develop/LICENSE)

# Featws API [![About_en](https://github.com/yammadev/flag-icons/blob/master/png/BR.png?raw=true)](https://github.com/bancodobrasil/featws-api/blob/develop/README-PTBR.md)
## How to run

In order to run this project, you need to have certain prerequisites set up on your machine. These prerequisites include:

- [Golang](https://go.dev/doc/install)
- [Swaggo](https://github.com/swaggo/swag/blob/master/README_pt.md#come%C3%A7ando)
- [Docker](https://www.docker.com/)


To run the project, follow these steps:

- Open the terminal in the project directory and run the command `go mod tidy` to ensure that all required dependencies are installed.

- Then, run the command `swag init` to initialize Swagger and generate the necessary API documentation.

- Open Docker on your machine or a similar application to ensure that it is working properly. Then, open the terminal in the project repository and run `docker compose up`.

- Once the containers are started and running, run the command `make run` in the project terminal to start the project.

The project will run on `localhost:9007`. To access the Swagger documentation [click here](http://localhost:9007/swagger/index.html#/).

By following these steps, the project will be up and running, and you will be able to access the API documentation through Swagger.

## GoDoc

To access the GoDoc documentation, first install GoDoc on your machine. Open a terminal and type:

````
go get golang.org/x/tools/cmd/godoc
````

Then run the following command in the repository terminal:

````
godoc -http=:6060
````

GoDoc will run on `localhost:6060`. To access the GoDoc documentation, just [click here](http://localhost:6060/pkg/).
22 changes: 19 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,23 @@ import (
"github.com/spf13/viper"
)

// Config ...
// Config type contains various configuration parameters for a Go application, including database
// and GitLab API credentials, authentication mode, and external host information.
//
// Property:
// - AllowOrigins: This property specifies the allowed origins for CORS requests. It is a string value that can contain multiple origins separated by commas.
// - Port: The port number on which the server will listen for incoming requests.
// - MysqlURI: The URI for connecting to the MySQL database used by the API.
// - Migrate: it's used to specify whether to run database migrations or not. If the value is set to "true", the application will run database migrations on startup. If the value is set to "false", the application will not run database migrations.
// - GitlabToken: This's a token used for authentication with GitLab API. It allows the application to access GitLab resources on behalf of a user or a bot account.
// - GitlabURL: The URL of the GitLab instance that the API will interact with.
// - GitlabNamespace: The namespace or group name in GitLab where the project is located.
// - GitlabPrefix: The GitlabPrefix property is a string that represents the prefix to be used for all GitLab API requests. It is used to specify the namespace or group where the project's located. For example, if the GitlabPrefix is set to "mygroup/myproject", all API requests will be made.
// - GitlabDefaultBranch: This property represents the default branch name for a GitLab repository. When creating a new repository, GitLab will use this branch as the default branch.
// - GitlabCIScript - GitlabCIScript is a property in the Config struct that represents the GitLab CI script that will be used for building and testing the project. It is specified in the configuration file using the key "FEATWS_API_GITLAB_CI_SCRIPT".
// - ExternalHost - This property represents the external host name or IP address of the server where the application is running. It is used to configure the application to listen on a specific network interface or to generate URLs that can be accessed from outside the server.
// - OpenAMURL: The URL of the OpenAM server used for authentication.
// - AuthMode - This property specifies the authentication mode used by the API. It can have values like "jwt", "oauth2", "basic", etc.
type Config struct {
AllowOrigins string `mapstructure:"ALLOW_ORIGINS"`
Port string `mapstructure:"PORT"`
Expand All @@ -26,7 +42,7 @@ type Config struct {

var config = &Config{}

// LoadConfig ...
// LoadConfig loads configuration settings from a file and sets default values if they are no present.
func LoadConfig() (err error) {
viper.AddConfigPath("./")
viper.SetConfigFile(".env")
Expand Down Expand Up @@ -62,7 +78,7 @@ func LoadConfig() (err error) {
return
}

// GetConfig ...
// GetConfig returns a pointer to a Config object.
func GetConfig() *Config {
return config
}
19 changes: 15 additions & 4 deletions controllers/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type HealthController struct {
health healthcheck.Handler
}

// NewHealthController ...
// NewHealthController returns a new instance of the HealthController struct with a newHandler.
func NewHealthController() *HealthController {
return &HealthController{
health: newHandler(),
Expand All @@ -30,6 +30,8 @@ func NewHealthController() *HealthController {

var health = healthcheck.NewHandler()

// newHandler creates a new healthcheck handler and adds liveness and readiness checks for goroutine
// count, Gitlab URL, and database connection.
func newHandler() healthcheck.Handler {
cfg := config.GetConfig()
health.AddLivenessCheck("goroutine-threshold", goroutine.Count(100))
Expand Down Expand Up @@ -58,7 +60,8 @@ func newHandler() healthcheck.Handler {
return health
}

// Get was the function that allow follow the url
// Get returns a check function that performs an HTTP GET request to a specified URL with a
// specified timeout and returns an error if the response status code is not 200.
func Get(url string, timeout time.Duration) checks.Check {
client := http.Client{
Timeout: timeout,
Expand All @@ -80,12 +83,20 @@ func Get(url string, timeout time.Duration) checks.Check {
}
}

// HealthLiveHandler ...
// HealthLiveHandler is returning a Gin middleware function that wraps the `LiveEndpoint` method of the
// `health` handler of the `HealthController` struct. The `LiveEndpoint` method is used to check if the
// application is alive and responding to requests. The `gin.WrapH` function is used to convert the
// `http.HandlerFunc` returned by `LiveEndpoint` into a `gin.HandlerFunc` that can be used as
// middleware in a Gin router.
func (c *HealthController) HealthLiveHandler() gin.HandlerFunc {
return gin.WrapH(http.HandlerFunc(c.health.LiveEndpoint))
}

// HealthReadyHandler ...
// HealthReadyHandler is returning a Gin middleware function that wraps the `ReadyEndpoint` method of the
// `health` handler of the `HealthController` struct. The `ReadyEndpoint` method is used to check if
// the application is ready to receive requests. The `gin.WrapH` function is used to convert the
// `http.HandlerFunc` returned by `ReadyEndpoint` into a `gin.HandlerFunc` that can be used as
// middleware in a Gin router.
func (c *HealthController) HealthReadyHandler() gin.HandlerFunc {
return gin.WrapH(http.HandlerFunc(c.health.ReadyEndpoint))
}
2 changes: 1 addition & 1 deletion controllers/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/gin-gonic/gin"
)

// HomeHandler ...
// HomeHandler function returns a JSON response with a message indicating that the FeatWS API is working.
func HomeHandler(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "FeatWS API Works!!!",
Expand Down
3 changes: 2 additions & 1 deletion controllers/home_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import (
// assert.Equal(t, mockUserResp, string(responseData))

// }

// TestUnitHomeHandler is a unit test for the HomeHandler function in a Go web application.
func TestUnitHomeHandler(t *testing.T) {
gin.SetMode(gin.TestMode)

Expand All @@ -42,5 +44,4 @@ func TestUnitHomeHandler(t *testing.T) {

controllers.HomeHandler(c)
assert.Equal(t, http.StatusOK, w.Code)

}
Loading

0 comments on commit a09798d

Please sign in to comment.