allow to use a configuration file
parse .capture.ini add information os start
This commit is contained in:
parent
cd5d927e7a
commit
9deb71e082
11
README.md
11
README.md
|
@ -8,7 +8,7 @@ Forked from [ofabricio/capture](https://github.com/ofabricio/capture).
|
||||||
## Running
|
## Running
|
||||||
|
|
||||||
```
|
```
|
||||||
./capture -url=https://example.com/
|
./capture -url=https://example.com/ -port 9000 -dashboard 9001 -captures 16
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Settings
|
#### Settings
|
||||||
|
@ -21,6 +21,15 @@ Forked from [ofabricio/capture](https://github.com/ofabricio/capture).
|
||||||
| `-captures` | Set how many captures to show in the dashboard. Default: *16* |
|
| `-captures` | Set how many captures to show in the dashboard. Default: *16* |
|
||||||
|
|
||||||
|
|
||||||
|
You can create a file named `.capture.ini` and set the configuration inside:
|
||||||
|
|
||||||
|
```
|
||||||
|
url = https://example.com/
|
||||||
|
port = 9000
|
||||||
|
dashboard = 9001
|
||||||
|
captures = 16
|
||||||
|
```
|
||||||
|
|
||||||
## Using
|
## Using
|
||||||
|
|
||||||
If you set your base url as `http://example.com/api`, now `http://localhost:9000` points to that
|
If you set your base url as `http://example.com/api`, now `http://localhost:9000` points to that
|
||||||
|
|
36
config.go
36
config.go
|
@ -2,6 +2,9 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"gopkg.in/ini.v1"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config has all the configuration parsed from the command line.
|
// Config has all the configuration parsed from the command line.
|
||||||
|
@ -14,11 +17,36 @@ type Config struct {
|
||||||
|
|
||||||
// ReadConfig reads the arguments from the command line.
|
// ReadConfig reads the arguments from the command line.
|
||||||
func ReadConfig() Config {
|
func ReadConfig() Config {
|
||||||
targetURL := flag.String("url", "https://jsonplaceholder.typicode.com", "Required. Set the url you want to proxy")
|
defaultTargetURL := "https://jsonplaceholder.typicode.com"
|
||||||
proxyPort := flag.String("port", "9000", "Set the proxy port")
|
defaultProxyPort := "9000"
|
||||||
dashboardPort := flag.String("dashboard", "9001", "Set the dashboard port")
|
defaultDashboardPort := "9001"
|
||||||
maxCaptures := flag.Int("captures", 16, "Set how many captures to show in the dashboard")
|
defaultMaxCaptures := 16
|
||||||
|
|
||||||
|
configFile := ".capture.ini"
|
||||||
|
|
||||||
|
if _, err := os.Stat(configFile); err == nil {
|
||||||
|
cfg, err := ini.Load(configFile)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Fail to read file %s: %v", configFile, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
section := cfg.Section("")
|
||||||
|
|
||||||
|
defaultTargetURL = section.Key("url").MustString(defaultTargetURL)
|
||||||
|
defaultProxyPort = section.Key("port").MustString(defaultProxyPort)
|
||||||
|
defaultDashboardPort = section.Key("dashboard").MustString(defaultDashboardPort)
|
||||||
|
defaultMaxCaptures = section.Key("captures").MustInt(defaultMaxCaptures)
|
||||||
|
}
|
||||||
|
|
||||||
|
targetURL := flag.String("url", defaultTargetURL, "Required. Set the url you want to proxy")
|
||||||
|
proxyPort := flag.String("port", defaultProxyPort, "Set the proxy port")
|
||||||
|
dashboardPort := flag.String("dashboard", defaultDashboardPort, "Set the dashboard port")
|
||||||
|
maxCaptures := flag.Int("captures", defaultMaxCaptures, "Set how many captures to show in the dashboard")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
return Config{
|
return Config{
|
||||||
TargetURL: *targetURL,
|
TargetURL: *targetURL,
|
||||||
ProxyPort: *proxyPort,
|
ProxyPort: *proxyPort,
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -1,3 +1,5 @@
|
||||||
module github.com/ofabricio/capture
|
module github.com/ofabricio/capture
|
||||||
|
|
||||||
go 1.16
|
go 1.16
|
||||||
|
|
||||||
|
require gopkg.in/ini.v1 v1.67.0 // indirect
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -0,0 +1,2 @@
|
||||||
|
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
|
||||||
|
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
1
main.go
1
main.go
|
@ -30,6 +30,7 @@ var dashboardHTML []byte
|
||||||
func main() {
|
func main() {
|
||||||
cfg := ReadConfig()
|
cfg := ReadConfig()
|
||||||
|
|
||||||
|
fmt.Printf("Target is %s", cfg.TargetURL)
|
||||||
fmt.Printf("\nListening on http://localhost:%s", cfg.ProxyPort)
|
fmt.Printf("\nListening on http://localhost:%s", cfg.ProxyPort)
|
||||||
fmt.Printf("\nDashboard on http://localhost:%s", cfg.DashboardPort)
|
fmt.Printf("\nDashboard on http://localhost:%s", cfg.DashboardPort)
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
|
|
Loading…
Reference in a new issue