Skip to content

Commit

Permalink
API Links List (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
bbengfort committed Feb 17, 2024
1 parent 201e9c8 commit 64fdbf2
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 137 deletions.
81 changes: 79 additions & 2 deletions cmd/rtnl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import (
)

var (
conf config.Config
svc api.Service
conf config.Config
svc api.Service
store *badger.DB

timeout = 30 * time.Second
)
Expand Down Expand Up @@ -101,6 +102,14 @@ func main() {
},
},
},
{
Name: "list",
Category: "client",
Usage: "get list of short urls stored on the server",
Action: listLinks,
Before: makeClient,
Flags: []cli.Flag{},
},
{
Name: "info",
Category: "client",
Expand Down Expand Up @@ -136,6 +145,15 @@ func main() {
Before: makeClient,
Flags: []cli.Flag{},
},
{
Name: "db:keys",
Category: "debug",
Usage: "print out all of the keys in the local database",
Action: dbKeys,
Before: openStore,
After: closeStore,
Flags: []cli.Flag{},
},
}

if err := app.Run(os.Args); err != nil {
Expand Down Expand Up @@ -312,6 +330,18 @@ func shorten(c *cli.Context) (err error) {
return display(out)
}

func listLinks(c *cli.Context) (err error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

var out *api.ShortURLList
if out, err = svc.ShortURLList(ctx, nil); err != nil {
return cli.Exit(err, 1)
}

return display(out)
}

func info(c *cli.Context) (err error) {
if c.NArg() == 0 {
return cli.Exit("specify at least one short url ID to get info for", 1)
Expand Down Expand Up @@ -413,6 +443,30 @@ func status(c *cli.Context) (err error) {
return display(status)
}

//===========================================================================
// Debug Commands
//===========================================================================

func dbKeys(c *cli.Context) error {
err := store.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false
it := txn.NewIterator(opts)
defer it.Close()
for it.Rewind(); it.Valid(); it.Next() {
item := it.Item()
k := item.Key()
fmt.Printf("key=%s\n", k)
}
return nil
})

if err != nil {
return cli.Exit(err, 1)
}
return nil
}

//===========================================================================
// Helper Commands
//===========================================================================
Expand All @@ -431,6 +485,29 @@ func makeClient(c *cli.Context) (err error) {
return nil
}

func openStore(c *cli.Context) (err error) {
if err = configure(c); err != nil {
return err
}

opts := badger.DefaultOptions(conf.Storage.DataPath)
opts.ReadOnly = conf.Storage.ReadOnly
opts.Logger = nil

if store, err = badger.Open(opts); err != nil {
return cli.Exit(err, 1)
}

return nil
}

func closeStore(c *cli.Context) (err error) {
if err = store.Close(); err != nil {
return cli.Exit(err, 1)
}
return nil
}

func display(v any) error {
encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", " ")
Expand Down
7 changes: 6 additions & 1 deletion pkg/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ type Service interface {
Status(context.Context) (*StatusReply, error)

// URL Management
// TODO: edit short url with details
ShortURLList(context.Context, *PageQuery) (*ShortURLList, error)
ShortenURL(context.Context, *LongURL) (*ShortURL, error)
ShortURLInfo(context.Context, string) (*ShortURL, error)
DeleteShortURL(context.Context, string) error

// Campaigns

// Websockets
Updates(context.Context, string) (<-chan *Click, error)
}

Expand Down Expand Up @@ -65,7 +70,7 @@ type ShortURL struct {
URL string `json:"url"`
AltURL string `json:"alt_url,omitempty"`
Title string `json:"title"`
Description string `json:"description"`
Description string `json:"description,omitempty"`
Visits uint64 `json:"visits"`
Expires *time.Time `json:"expires,omitempty"`
Created *time.Time `json:"created,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/rtnl/rtnl.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func (s *Server) Routes(router *gin.Engine) (err error) {

// Permenant Routes
router.GET("/:id", s.Redirect)
router.GET("/:id/info", s.ShortURLDetail)
router.GET("/:id/info", s.Authenticate, s.ShortURLDetail)
router.DELETE("/:id", s.Authenticate, s.DeleteShortURL)

// Web Links
Expand Down
20 changes: 19 additions & 1 deletion pkg/rtnl/shorten.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,27 @@ func (s *Server) ShortURLList(c *gin.Context) {
}

// Retrieve the page from the database
// TODO: pass the page query to the listing function
var urls []*models.ShortURL
if urls, err = s.db.List(); err != nil {
log.Warn().Err(err).Msg("could not retrieve short url list from db")
c.JSON(http.StatusInternalServerError, api.ErrorResponse("could not complete request"))
return
}

// Create the API response to send back to the user.
out = &api.ShortURLList{}
out = &api.ShortURLList{
URLs: make([]*api.ShortURL, 0, len(urls)),
Page: &api.PageQuery{},
}

for _, url := range urls {
out.URLs = append(out.URLs, &api.ShortURL{
URL: base62.Encode(url.ID),
Title: url.Title,
Visits: url.Visits,
})
}

c.Negotiate(http.StatusOK, gin.Negotiate{
Offered: []string{gin.MIMEHTML, gin.MIMEJSON},
Expand Down

0 comments on commit 64fdbf2

Please sign in to comment.