Skip to content

Commit

Permalink
Added option to set custom "valid-for" options (#35)
Browse files Browse the repository at this point in the history
added option to set custom "valid-for" options

Signed-off-by: Lasse Gaardsholt <[email protected]>

Signed-off-by: Lasse Gaardsholt <[email protected]>
  • Loading branch information
Gaardsholt authored Jan 6, 2023
1 parent aa5dd15 commit c6b2426
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 20 deletions.
47 changes: 47 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func StartServer() (internalServer *http.Server, externalServer *http.Server) {
internal := mux.NewRouter()
external := mux.NewRouter()
external.HandleFunc("/api", NewHandler).Methods("POST")
external.HandleFunc("/api/valid-for-options", ValidForHandler).Methods("GET")
external.HandleFunc("/api/{id}", GetHandler).Methods("GET")
external.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("./static"))))

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

// ValidForHandler returns the options you can choose in "Valid for" field
func ValidForHandler(w http.ResponseWriter, r *http.Request) {
options := map[int]string{}

for _, v := range config.Config.ValidForOptions {
options[v] = humanDuration(v)
}

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(options)
}

// humanDuration converts duration in seconds to human readable format
func humanDuration(duration int) string {
if duration < 60 {
if duration == 1 {
return fmt.Sprintf("%d second", duration)
}
return fmt.Sprintf("%d seconds", duration)
}

if duration < 3600 {
duration = duration / 60
if duration == 1 {
return fmt.Sprintf("%d minute", duration)
}
return fmt.Sprintf("%d minutes", duration)
}

if duration < 86400 {
duration = duration / 60 / 60
if duration == 1 {
return fmt.Sprintf("%d hour", duration)
}
return fmt.Sprintf("%d hours", duration)
}

duration = duration / 60 / 60 / 24
if duration == 1 {
return fmt.Sprintf("%d day", duration)
}

return fmt.Sprintf("%d days", duration/60/60/24)
}

// GetHandler retrieves a secret in the secret store
func GetHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
Expand Down
15 changes: 8 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import (

// GlobalConfig holds config parameters
type GlobalConfig struct {
ServerPort *int `required:"false" split_words:"true"`
HealthPort *int `required:"false" split_words:"true"`
ServerSalt string `required:"false" split_words:"true"`
DatabaseType *string `required:"false" split_words:"true" default:"in-memory"`
RedisServer *string `required:"false" split_words:"true"`
RedisPort *int `required:"false" split_words:"true"`
LogLevel string `required:"false" split_words:"true"`
ServerPort *int `required:"false" split_words:"true"`
HealthPort *int `required:"false" split_words:"true"`
ServerSalt string `required:"false" split_words:"true"`
DatabaseType *string `required:"false" split_words:"true" default:"in-memory"`
RedisServer *string `required:"false" split_words:"true"`
RedisPort *int `required:"false" split_words:"true"`
LogLevel string `required:"false" split_words:"true"`
ValidForOptions []int `required:"false" split_words:"true" default:"3600,7200,43200,86400"`
}

var Config GlobalConfig
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ module github.com/Gaardsholt/pass-along
go 1.19

require (
github.com/alicebob/miniredis/v2 v2.23.1
github.com/alicebob/miniredis/v2 v2.30.0
github.com/gomodule/redigo v1.8.9
github.com/gorilla/mux v1.8.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/prometheus/client_golang v1.14.0
github.com/rs/zerolog v1.28.0
golang.org/x/crypto v0.4.0
golang.org/x/crypto v0.5.0
gotest.tools v2.2.0+incompatible
)

Expand All @@ -27,6 +27,6 @@ require (
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/yuin/gopher-lua v0.0.0-20220504180219-658193537a64 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/sys v0.4.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a h1:HbKu58rmZp
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
github.com/alicebob/miniredis/v2 v2.23.1 h1:jR6wZggBxwWygeXcdNyguCOCIjPsZyNUNlAkTx2fu0U=
github.com/alicebob/miniredis/v2 v2.23.1/go.mod h1:84TWKZlxYkfgMucPBf5SOQBYJceZeQRFIaQgNMiCX6Q=
github.com/alicebob/miniredis/v2 v2.30.0 h1:uA3uhDbCxfO9+DI/DuGeAMr9qI+noVWwGPNTFuKID5M=
github.com/alicebob/miniredis/v2 v2.30.0/go.mod h1:84TWKZlxYkfgMucPBf5SOQBYJceZeQRFIaQgNMiCX6Q=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down Expand Up @@ -233,6 +235,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.4.0 h1:UVQgzMY87xqpKNgb+kDsll2Igd33HszWHFLmpaRMq/8=
golang.org/x/crypto v0.4.0/go.mod h1:3quD/ATkf6oY+rnes5c3ExXTbLc8mueNue5/DoinL80=
golang.org/x/crypto v0.5.0 h1:U/0M97KRkSFvyD/3FSmdP5W5swImpNgle/EHFhOsQPE=
golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -353,6 +357,8 @@ golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
7 changes: 1 addition & 6 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@ <h1>Pass-along</h1>
</p>

<p>
<select id="valid-for" name="valid-for" class="input">
<option value="3600">Valid for 1 hour</option>
<option value="7200">Valid for 2 hours</option>
<option value="43200">Valid for 12 hours</option>
<option value="86400">Valid for 24 hours</option>
</select>
<select id="valid-for" name="valid-for" class="input"></select>
</p>

<p>
Expand Down
19 changes: 15 additions & 4 deletions static/js/main.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
class SecretManager {
// constructor(height, width) {
// this.height = height;
// this.width = width;
// }
constructor() {
doCall("GET", "/api/valid-for-options", null, function(status, response) {
const JsonResponse = JSON.parse(response);

let validForElement = document.getElementById("valid-for");

for (const key in JsonResponse) {
var opt = document.createElement('option');
opt.value = key;
opt.innerHTML = `Valid for ${JsonResponse[key]}`;
validForElement.appendChild(opt);
}

});
}

get mainContainer() {
return document.getElementById("main");
Expand Down

0 comments on commit c6b2426

Please sign in to comment.