Skip to content

Commit

Permalink
API to return a selected field with /only?filed=XX (#12)
Browse files Browse the repository at this point in the history
* API to return a selected field with /only?filed=XX

* readme
  • Loading branch information
yas-nyan authored Jul 20, 2023
1 parent 417469e commit 28c9fef
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 5 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,20 @@ yas-nyan@mptcp:~$ curl localhost:8931

- you can filter record with query params.

for all fileds.
```
-
curl localhost:8931/?dport=5201
curl localhost:8931/?src=2001:db8::1
curl localhost:8931/?src=2001:db8::1&dst=64:ff9b::192.0.2.1
```

One optional field

```
curl localhost:8931/only?field=Mss
curl localhost:8931/only?filed=Minrtt?src=2001:db8::1
```

## config and args

- args
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ require (
github.com/jackc/pgx/v5 v5.4.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/k0kubun/pp v3.0.1+incompatible // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/spf13/afero v1.9.5 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/k0kubun/pp v3.0.1+incompatible h1:3tqvf7QgUnZ5tXO6pNAZlrvHgl6DvifjDrd9g2S9Z40=
github.com/k0kubun/pp v3.0.1+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand All @@ -149,6 +151,10 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
Expand Down Expand Up @@ -345,6 +351,8 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
68 changes: 64 additions & 4 deletions pkg/sockmon/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"net/http"
"os/exec"
"reflect"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -84,6 +85,7 @@ func fn(cmd *cobra.Command, args []string) error {
go func() {
http.HandleFunc("/", handlerDefault)
http.HandleFunc("/rtt", handlerRttOnly)
http.HandleFunc("/only", handerByFieldName)
if err := http.ListenAndServe(bindAddress, nil); err != nil {
log.Fatalf("Invalid bind address. err: %s", err)
}
Expand Down Expand Up @@ -157,14 +159,72 @@ func input(in string) {
}
}

func handerByFieldName(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()

c := FilterByParams(params, cache)

onlyParams, ok := params["field"]
if !ok {
http.Error(w, "Missing ?field= query parameter", http.StatusBadRequest)
return
}

// パラメータは一つと仮定する
onlyFiled := onlyParams[0]

// onlyFiledがExtに存在するかチェック
v := reflect.ValueOf(SocketExtendedInformation{})
vf := v.FieldByName(onlyFiled)
if !(vf.IsValid()) {
http.Error(w, fmt.Sprintf("Invalid input ?filed=%s", onlyFiled), http.StatusBadRequest)
return
}

// 返信用のmapを作る
fieldType := vf.Type()
mapType := reflect.MapOf(reflect.TypeOf(""), fieldType) // map[string]<filedType>
rtnMap := reflect.MakeMap(mapType)

for k, v := range c {
extVal := reflect.ValueOf(v.Ext)
// .ExtがSocketに存在しない場合。これは絶対に起こり得ないが、reflectionしているので念のため。
if extVal.Kind() != reflect.Struct {
log.Errorf("An exception occurred that should never have happened during reflection. extVal.Kind: %s reflect.Struct: %s", extVal.Kind(), reflect.Struct)
http.Error(w, fmt.Sprintf("Unexpected type for Ext in Socket %s", k), http.StatusInternalServerError)
return
}
// ここも事前にチェックしているが、reflectionしているので念のため。
val := extVal.FieldByName(onlyFiled)
if !val.IsValid() {
log.Errorf("An exception occurred that should never have happened during reflection. extVal.Kind: %s reflect.Struct: %s", extVal.Kind(), reflect.Struct)
http.Error(w, fmt.Sprintf("Field %s does not exist in Socket %s", onlyFiled, k), http.StatusInternalServerError)
return
}

rtnMap.SetMapIndex(reflect.ValueOf(k), val)
}

// rtnMapをInterfaceに変換してjsonにエンコード
out, err := json.MarshalIndent(rtnMap.Interface(), "", " ")
if err != nil {
http.Error(w, fmt.Sprintf("Failed to encode result: %v", err), http.StatusInternalServerError)
return
}

io.WriteString(w, string(out))
return

}

func handlerDefault(w http.ResponseWriter, r *http.Request) {
// filter by query parameters
params := r.URL.Query()

c := FilterByParams(params, cache)
out, err := json.MarshalIndent(&c, "", " ")

out, err := json.MarshalIndent(&c, "", " ")
if err != nil {
io.WriteString(w, fmt.Sprintf("{'err':'%s'}\n", err.Error()))
http.Error(w, fmt.Sprintf("Failed to encode result: %v", err), http.StatusInternalServerError)
return
}
io.WriteString(w, fmt.Sprintf("%s\n", string(out)))
Expand All @@ -182,7 +242,7 @@ func handlerRttOnly(w http.ResponseWriter, r *http.Request) {

out, err := json.MarshalIndent(&rttcache, "", " ")
if err != nil {
io.WriteString(w, fmt.Sprintf("{'err':'%s'}\n", err.Error()))
http.Error(w, fmt.Sprintf("Failed to encode result: %v", err), http.StatusInternalServerError)
return
}
io.WriteString(w, fmt.Sprintf("%s\n", string(out)))
Expand Down

0 comments on commit 28c9fef

Please sign in to comment.