up
This commit is contained in:
		
							parent
							
								
									68ef0fbf6b
								
							
						
					
					
						commit
						4150ebe27c
					
				@ -1,11 +1,8 @@
 | 
			
		||||
package client
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"encoding/binary"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
@ -18,7 +15,6 @@ type Client struct {
 | 
			
		||||
	httpClient *http.Client
 | 
			
		||||
	prefix     uint16
 | 
			
		||||
	gen        *generation.Generator
 | 
			
		||||
	domain     string // e.g. https://sho.rt
 | 
			
		||||
 | 
			
		||||
	db         *bolt.DB
 | 
			
		||||
	retryQueue chan shortenJob
 | 
			
		||||
@ -26,13 +22,13 @@ type Client struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NewClient with persistence and retry queue
 | 
			
		||||
func NewClient(serverURL, domain string) (*Client, error) {
 | 
			
		||||
func NewClient(serverURL string, folder string) (*Client, error) {
 | 
			
		||||
	httpClient, baseURL, err := createHTTPClient(serverURL)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	db, err := bolt.Open(dbFileName, 0600, &bolt.Options{Timeout: 1 * time.Second})
 | 
			
		||||
	db, err := bolt.Open(folder+"/"+dbFileName, 0600, &bolt.Options{Timeout: 1 * time.Second})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@ -40,7 +36,6 @@ func NewClient(serverURL, domain string) (*Client, error) {
 | 
			
		||||
	cli := &Client{
 | 
			
		||||
		serverURL:  baseURL,
 | 
			
		||||
		httpClient: httpClient,
 | 
			
		||||
		domain:     domain,
 | 
			
		||||
		db:         db,
 | 
			
		||||
		retryQueue: make(chan shortenJob, 1000),
 | 
			
		||||
		stopRetry:  make(chan struct{}),
 | 
			
		||||
@ -109,27 +104,10 @@ func NewClient(serverURL, domain string) (*Client, error) {
 | 
			
		||||
func (c *Client) Shorten(longURL string) string {
 | 
			
		||||
	shortID := c.gen.NextID()
 | 
			
		||||
 | 
			
		||||
	go func() {
 | 
			
		||||
		payload := map[string]string{
 | 
			
		||||
			"id":  shortID,
 | 
			
		||||
			"url": longURL,
 | 
			
		||||
		}
 | 
			
		||||
		data, _ := json.Marshal(payload)
 | 
			
		||||
	go c.enqueueJob(shortenJob{
 | 
			
		||||
		ID:  shortID,
 | 
			
		||||
		URL: longURL,
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
		req, err := http.NewRequest("POST", fmt.Sprintf("%s/shorten", c.serverURL), bytes.NewReader(data))
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println("shorten request build error:", err)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		req.Header.Set("Content-Type", "application/json")
 | 
			
		||||
 | 
			
		||||
		resp, err := c.httpClient.Do(req)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println("shorten request failed:", err)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		defer resp.Body.Close()
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	return fmt.Sprintf("%s/%s", c.domain, shortID)
 | 
			
		||||
	return shortID
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,11 +1,13 @@
 | 
			
		||||
package client
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"encoding/binary"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	bolt "go.etcd.io/bbolt"
 | 
			
		||||
@ -23,19 +25,18 @@ type shortenJob struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Client) registerPrefix() (uint16, error) {
 | 
			
		||||
	resp, err := c.httpClient.Post(fmt.Sprintf("%s/register", c.serverURL), "application/json", nil)
 | 
			
		||||
	resp, err := c.httpClient.Get(fmt.Sprintf("%s/register", c.serverURL))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return 0, err
 | 
			
		||||
	}
 | 
			
		||||
	defer resp.Body.Close()
 | 
			
		||||
 | 
			
		||||
	var result struct {
 | 
			
		||||
		Prefix uint16 `json:"prefix"`
 | 
			
		||||
	}
 | 
			
		||||
	if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
 | 
			
		||||
	bytes, err := io.ReadAll(resp.Body)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return 0, err
 | 
			
		||||
	}
 | 
			
		||||
	return result.Prefix, nil
 | 
			
		||||
 | 
			
		||||
	return binary.LittleEndian.Uint16(bytes), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Client) loadRetryJobs() {
 | 
			
		||||
@ -108,18 +109,10 @@ func (c *Client) deleteJobFromDB(job shortenJob) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Client) sendShortenJob(job shortenJob) error {
 | 
			
		||||
	payload := map[string]string{
 | 
			
		||||
		"id":  job.ID,
 | 
			
		||||
		"url": job.URL,
 | 
			
		||||
	}
 | 
			
		||||
	data, _ := json.Marshal(payload)
 | 
			
		||||
 | 
			
		||||
	req, err := http.NewRequest("POST", fmt.Sprintf("%s/shorten", c.serverURL), bytes.NewReader(data))
 | 
			
		||||
	req, err := http.NewRequest("POST", fmt.Sprintf("%s/shorten?s=%s", c.serverURL, job.ID), strings.NewReader(job.URL))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	req.Header.Set("Content-Type", "application/json")
 | 
			
		||||
 | 
			
		||||
	resp, err := c.httpClient.Do(req)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user