Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
njavilas2015 committed Nov 7, 2024
1 parent f371857 commit 20f22bc
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 42 deletions.
4 changes: 3 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.git
qlub
nginx.conf
nginx.conf
conf.d
site
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
**nginx.conf
qlub
qlub
conf.d
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@

Para instalar `qlub`, puedes descargar el binario desde el siguiente enlace:

- [Descargar qlub](https://github.com/njavilas2015/qlub/releases/download/v1.0.2/qlub)
- [Descargar qlub](https://github.com/njavilas2015/qlub/releases/download/v1.0.3/qlub)

```bash
wget https://github.com/njavilas2015/qlub/releases/download/v1.0.2/qlub
wget https://github.com/njavilas2015/qlub/releases/download/v1.0.3/qlub
```

Después de descargar, asegúrate de que el binario sea ejecutable y mueve el archivo a un directorio en tu `PATH`:
Expand Down Expand Up @@ -98,6 +98,8 @@ services:

volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ./conf.d/:/etc/nginx/conf.d/

- /mnt/md0/data/letsencrypt/:/etc/letsencrypt/

depends_on:
Expand All @@ -119,6 +121,7 @@ services:
volumes:
- ./subdomains.json:/app/subdomains.json
- ./nginx.conf:/app/nginx.conf
- ./conf.d/:/app/conf.d/
```
## Build
Expand Down
5 changes: 4 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ services:

volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ./conf.d/:/etc/nginx/conf.d/

- /mnt/md0/data/letsencrypt/:/etc/letsencrypt/

depends_on:
Expand All @@ -27,4 +29,5 @@ services:
image: njavilas/qlub:server
volumes:
- ./subdomains.json:/app/subdomains.json
- ./nginx.conf:/app/nginx.conf
- ./nginx.conf:/app/nginx.conf
- ./conf.d/:/app/conf.d/
30 changes: 26 additions & 4 deletions internal/config/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,48 @@ import (
"text/template"
)

func GenerateNginxConfig(subdomains []Subdomain) error {
file, err := os.Create("nginx.conf")
func write(fileName string, name string, text string, data any) {

file, err := os.Create(fileName)

if err != nil {
log.Fatalf("Error creating nginx.conf: %v", err)
}

defer file.Close()

rawTemplate, err := template.New("nginx").Parse(NginxTemplate)
rawTemplate, err := template.New(name).Parse(text)

if err != nil {
log.Fatalf("Error parsing template: %v", err)
}

if err := rawTemplate.Execute(file, subdomains); err != nil {
if err := rawTemplate.Execute(file, data); err != nil {
log.Fatalf("Error generating config: %v", err)
}

fmt.Println("NGINX config generated successfully.")
}

func GenerateNginxConfig(subdomains []Subdomain) error {

write("nginx.conf", "nginx", DefaultNginxTemplate, "")

os.RemoveAll("conf.d")

err := os.MkdirAll("conf.d", os.ModePerm)

if err != nil {
log.Fatalf("Error creating directory: %v", err)
}

for index, subdomain := range subdomains {

fileName := fmt.Sprintf("conf.d/%v.conf", index+1)

write(fileName, "nginx", NginxTemplate, subdomain)

}

return nil
}
Expand Down
89 changes: 56 additions & 33 deletions internal/config/templates.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,71 @@
package config

const NginxTemplate = `events {
const DefaultNginxTemplate = `worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
{{- range $subdomain := . }}
include /etc/nginx/mime.types;
default_type application/octet-stream;
{{- range $location := $subdomain.Location }}
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
upstream {{ $location.Alias }}_upstream {
{{- range $instances := $location.Instances }}
server {{ $instances }}:{{ $location.Port }};
{{- end }}
}
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
include /etc/nginx/conf.d/*.conf;
}
`

const NginxTemplate = `# ------------------------------------------------------------
# {{ .Domain }}
# ------------------------------------------------------------
{{- range $location := .Location }}
upstream {{ $location.Alias }}_upstream {
{{- range $instances := $location.Instances }}
server {{ $instances }}:{{ $location.Port }};
{{- end }}
}
{{- end }}
server {
{{- if $subdomain.Ssl }}
listen 443 ssl;
ssl_certificate {{ $subdomain.SslCert }};
ssl_certificate_key {{ $subdomain.SslCertKey }};
{{- else }}
listen 80;
{{- end }}
server {
{{- if .Ssl }}
listen 443 ssl;
ssl_certificate {{ .SslCert }};
ssl_certificate_key {{ .SslCertKey }};
{{- else }}
listen 80;
{{- end }}
server_name {{ .Domain }};
server_name {{ $subdomain.Domain }};
{{- range $location := $subdomain.Location }}
location {{ $location.Path }} {
{{- if $location.Ssl }}
proxy_pass https://{{ $location.Alias }}_upstream;
{{- else }}
proxy_pass http://{{ $location.Alias }}_upstream;
{{- end }}
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
}
{{- range $location := .Location }}
location {{ $location.Path }} {
{{- if $location.Ssl }}
proxy_pass https://{{ $location.Alias }}_upstream;
{{- else }}
proxy_pass http://{{ $location.Alias }}_upstream;
{{- end }}
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
}
{{- end }}
}
Expand Down

0 comments on commit 20f22bc

Please sign in to comment.