@@ -2,6 +2,7 @@ package purge
22
33import (
44 "context"
5+ "encoding/binary"
56 "errors"
67 "fmt"
78 "net/http"
@@ -13,6 +14,7 @@ import (
1314 storagev1 "github.com/omalloc/tavern/api/defined/v1/storage"
1415 "github.com/omalloc/tavern/contrib/log"
1516 "github.com/omalloc/tavern/internal/constants"
17+ "github.com/omalloc/tavern/pkg/encoding"
1618 "github.com/omalloc/tavern/plugin"
1719 "github.com/omalloc/tavern/storage"
1820)
@@ -48,10 +50,26 @@ func (r *PurgePlugin) Stop(ctx context.Context) error {
4850}
4951
5052func (r * PurgePlugin ) AddRouter (router * http.ServeMux ) {
53+
54+ codec := encoding .GetDefaultCodec ()
55+ sharedkv := storage .Current ().SharedKV ()
56+
5157 router .Handle ("/plugin/purge/tasks" , http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
52- // TODO: query sharedkv purge task list
58+ // query sharedkv purge task list
59+
60+ purgeTaskMap := make (map [string ]uint64 )
61+
62+ sharedkv .IteratePrefix (req .Context (), []byte ("dir/" ), func (key , val []byte ) error {
63+ purgeTaskMap [string (key )[4 :]] = binary .LittleEndian .Uint64 (val )
64+ return nil
65+ })
5366
54- var payload []byte
67+ // marshal to json
68+ payload , err := codec .Marshal (purgeTaskMap )
69+ if err != nil {
70+ w .WriteHeader (http .StatusInternalServerError )
71+ return
72+ }
5573
5674 w .Header ().Set ("Content-Length" , fmt .Sprintf ("%d" , len (payload )))
5775 w .Header ().Set ("Content-Type" , "application/json" )
@@ -72,6 +90,7 @@ func (r *PurgePlugin) HandleFunc(next http.HandlerFunc) http.HandlerFunc {
7290 ipPort := strings .Split (req .RemoteAddr , ":" )
7391 if _ , ok := r .allowAddr [ipPort [0 ]]; ! ok {
7492 w .WriteHeader (http .StatusForbidden )
93+ _metricPurgeRequestsTotal .WithLabelValues ("403" ).Inc ()
7594 return
7695 }
7796
@@ -83,6 +102,7 @@ func (r *PurgePlugin) HandleFunc(next http.HandlerFunc) http.HandlerFunc {
83102 u , err1 := url .Parse (storeUrl )
84103 if err1 != nil {
85104 r .log .Errorf ("failed to parse storeUrl %s: %s" , storeUrl , err1 )
105+ _metricPurgeRequestsTotal .WithLabelValues ("500" ).Inc ()
86106 return
87107 }
88108
@@ -98,6 +118,7 @@ func (r *PurgePlugin) HandleFunc(next http.HandlerFunc) http.HandlerFunc {
98118 if _ , err := current .SharedKV ().Get (context .Background (),
99119 []byte (fmt .Sprintf ("if/domain/%s" , u .Host ))); err != nil && errors .Is (err , storagev1 .ErrKeyNotFound ) {
100120 r .log .Infof ("purge dir %s but is not caching in the service" , u .Host )
121+ _metricPurgeRequestsTotal .WithLabelValues ("404" ).Inc ()
101122 return
102123 }
103124
@@ -106,11 +127,13 @@ func (r *PurgePlugin) HandleFunc(next http.HandlerFunc) http.HandlerFunc {
106127 w .Header ().Set ("Content-Length" , "0" )
107128 w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
108129 w .WriteHeader (http .StatusNotFound )
130+ _metricPurgeRequestsTotal .WithLabelValues ("404" ).Inc ()
109131 return
110132 }
111133
112134 r .log .Errorf ("purge dir %s failed: %v" , storeUrl , err )
113135 w .WriteHeader (http .StatusInternalServerError )
136+ _metricPurgeRequestsTotal .WithLabelValues ("500" ).Inc ()
114137 return
115138 }
116139
@@ -119,6 +142,7 @@ func (r *PurgePlugin) HandleFunc(next http.HandlerFunc) http.HandlerFunc {
119142 w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
120143 w .WriteHeader (http .StatusOK )
121144 _ , _ = w .Write (payload )
145+ _metricPurgeRequestsTotal .WithLabelValues ("200" ).Inc ()
122146 return
123147 }
124148
@@ -129,12 +153,14 @@ func (r *PurgePlugin) HandleFunc(next http.HandlerFunc) http.HandlerFunc {
129153 w .Header ().Set ("Content-Length" , "0" )
130154 w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
131155 w .WriteHeader (http .StatusNotFound )
156+ _metricPurgeRequestsTotal .WithLabelValues ("404" ).Inc ()
132157 return
133158 }
134159
135160 // others error
136161 r .log .Errorf ("purge %s failed: %v" , storeUrl , err )
137162 w .WriteHeader (http .StatusInternalServerError )
163+ _metricPurgeRequestsTotal .WithLabelValues ("500" ).Inc ()
138164 return
139165 }
140166
@@ -143,6 +169,7 @@ func (r *PurgePlugin) HandleFunc(next http.HandlerFunc) http.HandlerFunc {
143169 w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
144170 w .WriteHeader (http .StatusOK )
145171 _ , _ = w .Write (payload )
172+ _metricPurgeRequestsTotal .WithLabelValues ("200" ).Inc ()
146173 }
147174}
148175
0 commit comments