Como fazer Deploy de uma Aplicação Golang no Windows Server?
Tags: go, webdev
Published: 2023-12-04 15:03:08

## 1. Preparar o Ambiente (Cliente e Servidor)
Servidor

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/29xfzdw9vfhshvvvo37i.png)


Endereço IP do Servidor: 192.168.179.142

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/syzrxjbwlhauwxxusqcw.png)


Endereço IP do Cliente: 192.168.179.1
 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qng35wtyo3t32njzm13t.png)



## 2. Instalar NSSM
O NSSM (Non-Sucking Service Manager) é necessário para configurar a aplicação como serviço.
Download: http://nssm.cc/download

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/orydvroxhrlma66t0yy9.png)

Localização do Download

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mhdkcofkqtc42tvjpa4i.png)

Extrair os ficheiros da pasta Zipada para um novo Directório 
Neste caso, a pasta no directório C:\Program Files\nssm-2.24

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z9hyd826whutv4dv5cu8.png)

 
Adicionar o caminho do executável na variável de ambiente: PATH
Caminho: C:\Program Files\nssm-2.24\win64
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qo9gs6ldqbrk8916n1kc.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fxy9z0n4ykqsq2zmxhzp.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jt7jw64xshw02lo20f6n.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nmh09whitoj2kmq5e6fz.png)





 
## 3. Fazer o Build da Aplicação
A aplicação tem 2 endpoints: 

- / - Retorna o título da aplicação
- /api – Retorna um JSON com as informações básicas

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/083yy312vl6ly2fyfym3.png)

Executar aplicação

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ero4rggg9vkuyrhqcumo.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1a6ri4lf6n7j3ynac3qn.png)

 
Gerar o binário para Windows
Comando: GOOS=windows go build –o NOME_EXECUTAVEL.exe

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gbhyb9o8vt819csatji8.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cbkjhbgyzp3opbxgipc0.png)




## 4. Implantar a Aplicação no Servidor
Copiar Executável para um directório do servidor. Criamos o directório C:\Applications\Golang-Windows copiamos o executável da aplicação.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jm762vor5qmaycayfr1x.png)




## 5. Criar um Serviço do Windows com NSSM
Sintaxe: nssm install NOME-SERVIÇO “Directorio\NOME_EXECUTAVEL.exe”
Comadndo: nssm install Golang-Windows "C:\Applications\Golang-Windows\Golang-Windows.exe"
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i69xyz72vd4pb89fkfse.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4c33q7zdx74otxkd1819.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k134w2w3m7t74fbpr9dy.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/712amqpm3mgcp3cvss13.png)




## 6. Testar Aplicação dentro do Servidor
http://localhost:9000/api
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/60sty8h1ftmvsgjyakgm.png)



## 7. Configurar Firewall (Habilitar uma porta para a aplicação)
Passos: Windows Defender Firewall > Inbound Rules > New Rule

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ikquiy1pnjetp0nb967e.png)

Seleccionar a opção Port
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r2hue0xysvev06mozlsa.png)

Seleccionar a opção TCP e definir o nº da porta local. Ex: 9000
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bp5bxrjn2npd1e9yqien.png)


Seleccionar a opção Allow the Connection
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xur7u8ghm01dxy55wcip.png)

Seleccionar as opções: Domain, Private e Public.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ga13fmno3dkdppuph4u3.png)

Atribuir um nome a Regra do FireWall Ex: Golang Windows App
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kj4yluwz9lhxn19bbbl6.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/021hdhdvqx4y10hxq8x5.png)




## 8. Testar a aplicação  na Máquina Cliente
Checar o IP do servidor

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/32mswb80xbq12e15neqc.png)

Fazer o acesso da máquina cliente (ex:  http://IP_SERVIDOR:Porta)
URL: http://192.168.179.143:9000

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/02zxj6bm7dzu750035bt.png)
 
URL: http://192.168.179.143:9000/api

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kajgquusz0ax50rc4noc.png)