Web服务器配置文件(Nginx config等)解析工具,提供配置文件展示和修改接口
Context
JSON
format serialization structure to simplify serialization/deserialization operations, making the serialization structure more universal and unified.Changed the serialization structure of the Context
JSON
format, as well as the new functions for the Directive
and Comment
context objects.
The Context
JSON
format serialization structure changes as follows:
Before:
{
"main-config": "C:\\test\\test.conf",
"configs":
{
"C:\\test\\test.conf":
[
{
"http":
{
"params":
[
{
"inline": true, "comments": "test comment"
},
{
"server":
{
"params":
[
{
"directive": "server_name", "params": "testserver"
},
{
"location": {"value": "~ /test"}
},
{
"include":
{
"value": "conf.d\\include*conf",
"params": ["conf.d\\include.location1.conf", "conf.d\\include.location2.conf"]
}
}
]
}
}
]
}
}
],
"conf.d\\include.location1.conf":
[
{
"location": {"value": "~ /test1"}
}
],
"conf.d\\include.location2.conf":
[
{
"location": {"value": "^~ /test2"}
}
]
}
}
After:
{
"main-config": "C:\\test\\test.conf",
"configs":
{
"C:\\test\\test.conf":
{
"context-type": "config",
"value": "C:\\test\\test.conf",
"params":
[
{
"context-type": "http",
"params":
[
{
"context-type": "inline_comment", "value": "test comment"
},
{
"context-type": "server",
"params":
[
{
"context-type": "directive", "value": "server_name testserver"
},
{
"context-type": "location", "value": "~ /test"
},
{
"context-type": "include",
"value": "conf.d\\include*conf",
"params": ["conf.d\\include.location1.conf", "conf.d\\include.location2.conf"]
}
]
}
]
}
]
},
"conf.d\\include.location1.conf":
{
"context-type": "config",
"value": "conf.d\\include.location1.conf",
"params":
[
{
"context-type": "location", "value": "~ /test1"
}
]
},
"conf.d\\include.location2.conf":
{
"context-type": "config",
"value": "conf.d\\include.location2.conf",
"params":
[
{
"context-type": "location", "value": "^~ /test2"
}
]
}
}
}
The code migration example for creating function calls for Directive
and Comment
context objects is as follows:
Before:
import (
"github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration/context/local"
)
// new directive context
newDirective := local.NewDirective("some_directive", "some params")
// new comment context
newComment := local.NewComment("some comments", false)
newComment := local.NewComment("some inline comments", true)
After:
import (
"github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration/context/local"
"github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration/context_type"
)
// new directive context
newDirective := local.NewContext(context_type.TypeDirective, "some_directive some params")
// new comment context
newComment := local.NewContext(context_type.TypeComment, "some comments")
newInlineComment := local.NewContext(context_type.TypeInlineComment, "some inline comments")
pkg.go.dev
.disable contaminated product package versions v1.0.9
and v1.0.10
on pkg.go.dev
.
Code migration requires changing the version of the bifrost
package from v1.0.9 or v1.0.10 to v1.0.11, as shown in the following example:
go.mod
:
require (
github.com/ClessLi/bifrost v1.0.11
)
Dependabot
alert.KeyWords
.Match()
method.Main
Context.resolve
V3 version, as well as the update of the bifrost
service to the resolve
V3 version.KeyWords
class and conduct preliminary unit testing of related methods for this classlocal
library unit testing and repair.local
.Include
Context.replacing the nginx configuration resolving library from version V2
to V3
.
To migrate the code follow the example below:
Before:
import (
"github.com/ClessLi/bifrost/pkg/resolv/V2/nginx/configuration"
)
nginxConfFromPath, err := configuration.NewConfigurationFromPath(configAbsPath)
nginxConfFromJsonBytes, err := configuration.NewConfigurationFromJsonBytes(configJsonBytes)
After:
import (
"github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration"
)
nginxConfFromPath, err := configuration.NewNginxConfigFromFS(configAbsPath)
nginxConfFromJsonBytes, err := configuration.NewNginxConfigFromJsonBytes(configJsonBytes)
Example for querying and inserting context:
import (
"fmt"
"github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration"
"github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration/context"
"github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration/context/local"
)
conf, err := configuration.NewNginxConfigFromJsonBytes(jsondata)
if err != nil {
t.Fatal(err)
}
for _, pos := range conf.Main().QueryByKeyWords(context.NewKeyWords(context_type.TypeHttp)).Target().
QueryByKeyWords(context.NewKeyWords(context_type.TypeServer)).Target().
QueryAllByKeyWords(context.NewKeyWords(context_type.TypeDirective).SetStringMatchingValue("server_name test1.com")) { // query `server` context, its server name is "test1.com"
server, _ := pos.Position()
if server.QueryByKeyWords(context.NewKeyWords(context_type.TypeDirective).SetRegexpMatchingValue("^listen 80$")).Target().Error() != nil { // query `server` context, its listen port is 80
continue
}
// query the "proxy_pass" `directive` context, which is in `if` context(value: "($http_api_name != '')") and `location` context(value: "/test1-location")
ctx, idx := server.QueryByKeyWords(context.NewKeyWords(context_type.TypeLocation).SetRegexpMatchingValue(`^/test1-location$`)).Target().
QueryByKeyWords(context.NewKeyWords(context_type.TypeIf).SetRegexpMatchingValue(`^\(\$http_api_name != ''\)$`)).Target().
QueryByKeyWords(context.NewKeyWords(context_type.TypeDirective).SetStringMatchingValue("proxy_pass")).Position()
// insert an inline comment after the "proxy_pass" `directive` context
err = ctx.Insert(local.NewComment(fmt.Sprintf("[%s]test comments", time.Now().String()), true), idx+1).Error()
if err != nil {
return err
}
}
Examples for building nginx context object:
import (
"github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration/context/local"
"github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration/context_type"
)
// new main context
newMainContext, err := local.NewMain("/usr/local/nginx/conf/nginx.conf")
// new directive context
newDirective := local.NewDirective("some_directive", "some params")
// new comment context
newComment := local.NewComment("some comments", false)
// new other context
newConfig := local.NewContext(context_type.TypeConfig, "conf.d/location.conf")
newInclude := local.NewContext(context_type.TypeInclude, "conf.d/*.conf")
newHttp := local.NewContext(context_type.TypeHttp, "")
...
Location
contextTruncate
method of dumper
will modify the completed dump dataKeyWords
interface adds the feature of cascade retrievalBifrost
client connections, the obtained Bifrost
service
instances are the sameWatcherManager
cannot converge quickly, after the WebServerLogWatcher.Watch
client is disconnectedFileWatcher
cannot be closed normallyBifrost
client connections, the obtained Bifrost
service instances are the sameBifrost
grpc protocol changesWebServerLogWatcher
service test for grpc server and clientWebServerStatus
grpc server and clientthe grpc protocol of Bifrost
has been changed, and service authentication is not supported temporarily.
To migrate the code of the Bifrost
gRPC client follow the example below:
Before:
data, err := bifrostClient.ViewConfig(...) // view web server config
data, err := bifrostClient.GetConfig(...) // get web server config
resp, err := bifrostClient.UpdateConfig(..., reqData, ...) // update web server config
data, err := bifrostClient.Status(...) // view web server state
data, err := bifrostClient.ViewStatistics(...) // view web server statistics
watcher, err := bifrostClient.WatchLog(...) // watching web server log
After:
// web server config
servernames, err := bifrostClient.WebServerConfig().GetServerNames() // get web server names
data, err := bifrostClient.WebServerConfig().Get(servername) // view web server config
err := bifrostClient.WebServerConfig().Update(servername, data) // update web server config
// web server status
metrics, err := bifrostClient.WebServerStatus().Get() // view web server status
// web server statistics
statistics, err := bifrostClient.WebServerStatistics().Get(servername) // view web server statistics
// web server log watcher
outputChan, cancel, err := bifrostClient.WebServerLogWatcher().Watch(request) // watch web server log
New features and improvements Preliminary completion of micro service code, nginx configuration analysis library code refactoring, and independent of the daemon class. Make the whole code logic more clear.