Browse Source

Update client and eokify command with ttl

master
Tyler Sommer 1 year ago
parent
commit
ae146b9767
Signed by: tyler-sommer GPG Key ID: C09C010500DBD008
2 changed files with 17 additions and 14 deletions
  1. +9
    -8
      client.go
  2. +8
    -6
      cmd/eokify/main.go

+ 9
- 8
client.go View File

@ -7,6 +7,7 @@ import (
"io/ioutil"
"net/http"
"net/url"
"time"
)
type ShortURL struct {
@ -22,34 +23,34 @@ type Client struct {
Token string
}
func NewClient(token string) *Client {
func NewClient(endpoint, token string) *Client {
return &Client{
http: &http.Client{},
Endpoint: "https://eok.vin/new",
Endpoint: endpoint,
Token: token,
}
}
func NewInsecureClient(token string) *Client {
func NewInsecureClient(endpoint, token string) *Client {
return &Client{
http: &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}},
Endpoint: "https://localhost:3000/new",
Endpoint: endpoint,
Token: token,
}
}
func (c *Client) NewShortURLString(lu string) (*ShortURL, error) {
func (c *Client) NewShortURLString(lu string, ttl time.Duration) (*ShortURL, error) {
ou, err := url.Parse(lu)
if err != nil {
return nil, err
}
return c.NewShortURL(ou)
return c.NewShortURL(ou, ttl)
}
func (c *Client) NewShortURL(ou *url.URL) (*ShortURL, error) {
func (c *Client) NewShortURL(ou *url.URL, ttl time.Duration) (*ShortURL, error) {
resp, err := c.http.PostForm(
c.Endpoint,
url.Values{"token": {c.Token}, "url": {ou.String()}})
url.Values{"token": {c.Token}, "url": {ou.String()}, "ttl": {ttl.String()}})
if err != nil {
return nil, err
}

+ 8
- 6
cmd/eokify/main.go View File

@ -5,6 +5,7 @@ import (
"fmt"
"os"
"log"
"time"
"github.com/veonik/eokvin"
)
@ -12,11 +13,13 @@ import (
var token string
var endpoint string
var insecure bool
var ttl time.Duration
func main() {
flag.StringVar(&token, "token", "", "Secret token, required")
flag.StringVar(&endpoint, "endpoint", "https://eok.vin/new", "URL for the running eokvin server")
flag.StringVar(&endpoint, "endpoint", "https://localhost:3000/new", "URL for the running eokvin server")
flag.BoolVar(&insecure, "insecure", false,"If enabled, allows endpoint to be insecure")
flag.DurationVar(&ttl, "ttl", 12 * time.Hour, "Short URL expires after this long")
flag.Parse()
if token == "" {
@ -35,14 +38,13 @@ func main() {
var c *eokvin.Client
if !insecure {
c = eokvin.NewClient(token)
c = eokvin.NewClient(endpoint, token)
} else {
c = eokvin.NewInsecureClient(token)
c = eokvin.NewInsecureClient(endpoint, token)
}
c.Endpoint = endpoint
s, err := c.NewShortURLString(u)
s, err := c.NewShortURLString(u, ttl)
if err != nil {
log.Fatalf("error creating short URL: %s", err.Error())
}
fmt.Println(s.String())
fmt.Printf("Short URL: %s\t(valid until %s)\n", s, time.Now().Add(ttl).Format("Jan 02 15:04 MST"))
}

Loading…
Cancel
Save