Skip to content

Commit c6b2426

Browse files
authored
Added option to set custom "valid-for" options (#35)
added option to set custom "valid-for" options Signed-off-by: Lasse Gaardsholt <lasse.gaardsholt@bestseller.com> Signed-off-by: Lasse Gaardsholt <lasse.gaardsholt@bestseller.com>
1 parent aa5dd15 commit c6b2426

File tree

6 files changed

+80
-20
lines changed

6 files changed

+80
-20
lines changed

api/api.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func StartServer() (internalServer *http.Server, externalServer *http.Server) {
5353
internal := mux.NewRouter()
5454
external := mux.NewRouter()
5555
external.HandleFunc("/api", NewHandler).Methods("POST")
56+
external.HandleFunc("/api/valid-for-options", ValidForHandler).Methods("GET")
5657
external.HandleFunc("/api/{id}", GetHandler).Methods("GET")
5758
external.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("./static"))))
5859

@@ -148,6 +149,52 @@ func NewHandler(w http.ResponseWriter, r *http.Request) {
148149
fmt.Fprintf(w, "%s", id)
149150
}
150151

152+
// ValidForHandler returns the options you can choose in "Valid for" field
153+
func ValidForHandler(w http.ResponseWriter, r *http.Request) {
154+
options := map[int]string{}
155+
156+
for _, v := range config.Config.ValidForOptions {
157+
options[v] = humanDuration(v)
158+
}
159+
160+
w.WriteHeader(http.StatusOK)
161+
w.Header().Set("Content-Type", "application/json")
162+
json.NewEncoder(w).Encode(options)
163+
}
164+
165+
// humanDuration converts duration in seconds to human readable format
166+
func humanDuration(duration int) string {
167+
if duration < 60 {
168+
if duration == 1 {
169+
return fmt.Sprintf("%d second", duration)
170+
}
171+
return fmt.Sprintf("%d seconds", duration)
172+
}
173+
174+
if duration < 3600 {
175+
duration = duration / 60
176+
if duration == 1 {
177+
return fmt.Sprintf("%d minute", duration)
178+
}
179+
return fmt.Sprintf("%d minutes", duration)
180+
}
181+
182+
if duration < 86400 {
183+
duration = duration / 60 / 60
184+
if duration == 1 {
185+
return fmt.Sprintf("%d hour", duration)
186+
}
187+
return fmt.Sprintf("%d hours", duration)
188+
}
189+
190+
duration = duration / 60 / 60 / 24
191+
if duration == 1 {
192+
return fmt.Sprintf("%d day", duration)
193+
}
194+
195+
return fmt.Sprintf("%d days", duration/60/60/24)
196+
}
197+
151198
// GetHandler retrieves a secret in the secret store
152199
func GetHandler(w http.ResponseWriter, r *http.Request) {
153200
vars := mux.Vars(r)

config/config.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import (
1111

1212
// GlobalConfig holds config parameters
1313
type GlobalConfig struct {
14-
ServerPort *int `required:"false" split_words:"true"`
15-
HealthPort *int `required:"false" split_words:"true"`
16-
ServerSalt string `required:"false" split_words:"true"`
17-
DatabaseType *string `required:"false" split_words:"true" default:"in-memory"`
18-
RedisServer *string `required:"false" split_words:"true"`
19-
RedisPort *int `required:"false" split_words:"true"`
20-
LogLevel string `required:"false" split_words:"true"`
14+
ServerPort *int `required:"false" split_words:"true"`
15+
HealthPort *int `required:"false" split_words:"true"`
16+
ServerSalt string `required:"false" split_words:"true"`
17+
DatabaseType *string `required:"false" split_words:"true" default:"in-memory"`
18+
RedisServer *string `required:"false" split_words:"true"`
19+
RedisPort *int `required:"false" split_words:"true"`
20+
LogLevel string `required:"false" split_words:"true"`
21+
ValidForOptions []int `required:"false" split_words:"true" default:"3600,7200,43200,86400"`
2122
}
2223

2324
var Config GlobalConfig

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ module github.com/Gaardsholt/pass-along
33
go 1.19
44

55
require (
6-
github.com/alicebob/miniredis/v2 v2.23.1
6+
github.com/alicebob/miniredis/v2 v2.30.0
77
github.com/gomodule/redigo v1.8.9
88
github.com/gorilla/mux v1.8.0
99
github.com/kelseyhightower/envconfig v1.4.0
1010
github.com/prometheus/client_golang v1.14.0
1111
github.com/rs/zerolog v1.28.0
12-
golang.org/x/crypto v0.4.0
12+
golang.org/x/crypto v0.5.0
1313
gotest.tools v2.2.0+incompatible
1414
)
1515

@@ -27,6 +27,6 @@ require (
2727
github.com/prometheus/common v0.37.0 // indirect
2828
github.com/prometheus/procfs v0.8.0 // indirect
2929
github.com/yuin/gopher-lua v0.0.0-20220504180219-658193537a64 // indirect
30-
golang.org/x/sys v0.3.0 // indirect
30+
golang.org/x/sys v0.4.0 // indirect
3131
google.golang.org/protobuf v1.28.1 // indirect
3232
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a h1:HbKu58rmZp
4242
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
4343
github.com/alicebob/miniredis/v2 v2.23.1 h1:jR6wZggBxwWygeXcdNyguCOCIjPsZyNUNlAkTx2fu0U=
4444
github.com/alicebob/miniredis/v2 v2.23.1/go.mod h1:84TWKZlxYkfgMucPBf5SOQBYJceZeQRFIaQgNMiCX6Q=
45+
github.com/alicebob/miniredis/v2 v2.30.0 h1:uA3uhDbCxfO9+DI/DuGeAMr9qI+noVWwGPNTFuKID5M=
46+
github.com/alicebob/miniredis/v2 v2.30.0/go.mod h1:84TWKZlxYkfgMucPBf5SOQBYJceZeQRFIaQgNMiCX6Q=
4547
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
4648
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
4749
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
@@ -233,6 +235,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
233235
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
234236
golang.org/x/crypto v0.4.0 h1:UVQgzMY87xqpKNgb+kDsll2Igd33HszWHFLmpaRMq/8=
235237
golang.org/x/crypto v0.4.0/go.mod h1:3quD/ATkf6oY+rnes5c3ExXTbLc8mueNue5/DoinL80=
238+
golang.org/x/crypto v0.5.0 h1:U/0M97KRkSFvyD/3FSmdP5W5swImpNgle/EHFhOsQPE=
239+
golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU=
236240
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
237241
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
238242
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
@@ -353,6 +357,8 @@ golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBc
353357
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
354358
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
355359
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
360+
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
361+
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
356362
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
357363
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
358364
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

static/index.html

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,7 @@ <h1>Pass-along</h1>
3939
</p>
4040

4141
<p>
42-
<select id="valid-for" name="valid-for" class="input">
43-
<option value="3600">Valid for 1 hour</option>
44-
<option value="7200">Valid for 2 hours</option>
45-
<option value="43200">Valid for 12 hours</option>
46-
<option value="86400">Valid for 24 hours</option>
47-
</select>
42+
<select id="valid-for" name="valid-for" class="input"></select>
4843
</p>
4944

5045
<p>

static/js/main.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
class SecretManager {
2-
// constructor(height, width) {
3-
// this.height = height;
4-
// this.width = width;
5-
// }
2+
constructor() {
3+
doCall("GET", "/api/valid-for-options", null, function(status, response) {
4+
const JsonResponse = JSON.parse(response);
5+
6+
let validForElement = document.getElementById("valid-for");
7+
8+
for (const key in JsonResponse) {
9+
var opt = document.createElement('option');
10+
opt.value = key;
11+
opt.innerHTML = `Valid for ${JsonResponse[key]}`;
12+
validForElement.appendChild(opt);
13+
}
14+
15+
});
16+
}
617

718
get mainContainer() {
819
return document.getElementById("main");

0 commit comments

Comments
 (0)