I have this file:
upstream frontends {
server service-example1.example.com;
server service-example2.example.com;
}
server {
listen 80;
location / {
proxy_pass http://frontends;
}
}
What I want to do is to remove and add lines in the upstream frontends section. Removing lines is quite easy:
package main
import (
"bytes"
"fmt"
"io/ioutil"
)
func main() {
content, err := ioutil.ReadFile("nginx.conf")
if err != nil {
panic(err)
}
lines := bytes.Replace(content, []byte("server service-example1.example.com;"), []byte(""), 1)
fmt.Println(string(lines))
err = ioutil.WriteFile("nginx.conf", lines, 0644)
if err != nil {
panic(err)
}
}
However, I find it a lot more difficult to add the same line that I removed again. My scenario is that I want to remove the line containing "server service-example2.example.com;", reload nginx conf. Later I want to add the same line at it's previous position and reload nginx again.
I find it difficult to get the exact byte position in the file where I want to add the line again with *(f File) WriteAt. I've been looking at this example: https://play.golang.org/p/eaWYAkxyLI, but cannot figure out a good way how to do it.
Anyone got any idea how I can solve this?