-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathmain.go
66 lines (57 loc) · 1.48 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"log"
"github.com/tufanbarisyildirim/gonginx/config"
"github.com/tufanbarisyildirim/gonginx/dumper"
"github.com/tufanbarisyildirim/gonginx/parser"
)
func addCustomDirective(fullConf string, blockName string, directiveName string, directiveValue string) (string, error) {
p := parser.NewStringParser(fullConf)
conf, err := p.Parse()
if err != nil {
return "", fmt.Errorf("failed to parse config: %w", err)
}
blocks := conf.FindDirectives(blockName)
if len(blocks) == 0 {
return "", fmt.Errorf("no such block: %s", blockName)
}
block := blocks[0].GetBlock()
newDirective := &config.Directive{
Name: directiveName,
Parameters: []config.Parameter{{Value: directiveValue}},
}
realBlock := block.(*config.Block)
realBlock.Directives = append(realBlock.Directives, newDirective)
return dumper.DumpConfig(conf, dumper.IndentedStyle), nil
}
func main() {
fullConf := `http{
upstream my_backend{
server 127.0.0.1:443;
server 127.0.0.2:443 backup;
}
server {
listen 8080;
location / {
root /var/www/html;
index index.html;
}
}
server {
listen 9090;
location / {
root /var/www/html;
index index.html;
}
}
}`
blockName := "server"
directiveName := "access_log"
directiveValue := "/var/log/nginx/access.log"
newFullConf, err := addCustomDirective(fullConf, blockName, directiveName, directiveValue)
if err != nil {
log.Fatalf("Error adding custom directive: %v", err)
}
fmt.Println("New Full Config:", newFullConf)
}