allow to use a configuration file

parse .capture.ini

add information os start
This commit is contained in:
Simon Vieille 2023-05-29 17:07:39 +02:00
parent cd5d927e7a
commit 9deb71e082
Signed by: deblan
GPG Key ID: 579388D585F70417
5 changed files with 47 additions and 5 deletions

View File

@ -8,7 +8,7 @@ Forked from [ofabricio/capture](https://github.com/ofabricio/capture).
## Running
```
./capture -url=https://example.com/
./capture -url=https://example.com/ -port 9000 -dashboard 9001 -captures 16
```
#### 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* |
You can create a file named `.capture.ini` and set the configuration inside:
```
url = https://example.com/
port = 9000
dashboard = 9001
captures = 16
```
## Using
If you set your base url as `http://example.com/api`, now `http://localhost:9000` points to that

View File

@ -2,6 +2,9 @@ package main
import (
"flag"
"fmt"
"gopkg.in/ini.v1"
"os"
)
// 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.
func ReadConfig() Config {
targetURL := flag.String("url", "https://jsonplaceholder.typicode.com", "Required. Set the url you want to proxy")
proxyPort := flag.String("port", "9000", "Set the proxy port")
dashboardPort := flag.String("dashboard", "9001", "Set the dashboard port")
maxCaptures := flag.Int("captures", 16, "Set how many captures to show in the dashboard")
defaultTargetURL := "https://jsonplaceholder.typicode.com"
defaultProxyPort := "9000"
defaultDashboardPort := "9001"
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()
return Config{
TargetURL: *targetURL,
ProxyPort: *proxyPort,

2
go.mod
View File

@ -1,3 +1,5 @@
module github.com/ofabricio/capture
go 1.16
require gopkg.in/ini.v1 v1.67.0 // indirect

2
go.sum
View File

@ -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=

View File

@ -30,6 +30,7 @@ var dashboardHTML []byte
func main() {
cfg := ReadConfig()
fmt.Printf("Target is %s", cfg.TargetURL)
fmt.Printf("\nListening on http://localhost:%s", cfg.ProxyPort)
fmt.Printf("\nDashboard on http://localhost:%s", cfg.DashboardPort)
fmt.Println()