Skip to content

Commit

Permalink
Add env (#6)
Browse files Browse the repository at this point in the history
* feat(env): add dotenv

* feat(env): add env example
  • Loading branch information
vit0rr authored Oct 18, 2024
1 parent ba719c2 commit 84c8665
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
9 changes: 3 additions & 6 deletions api/internal/url-short/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"io"

"github.com/sqids/sqids-go"
gonanoid "github.com/matoous/go-nanoid"
"github.com/vit0rr/short-spot/pkg/deps"
"github.com/vit0rr/short-spot/pkg/log"
"go.mongodb.org/mongo-driver/bson"
Expand All @@ -31,10 +31,6 @@ func NewService(deps *deps.Deps, db *mongo.Database) *Service {

// Get short URL
func (s *Service) ShortUrl(c context.Context, b io.ReadCloser, dbclient mongo.Client) (*Response, error) {
sq, _ := sqids.New(sqids.Options{
MinLength: 10,
})

var body Body
err := json.NewDecoder(b).Decode(&body)
if err != nil {
Expand All @@ -52,7 +48,8 @@ func (s *Service) ShortUrl(c context.Context, b io.ReadCloser, dbclient mongo.Cl
}, err
}

id, err := sq.Encode([]uint64{1, 2, 3})
id, err := gonanoid.Nanoid(6)

if err != nil {
log.Error(c, "Failed to encode URL", log.ErrAttr(err))
return &Response{
Expand Down
17 changes: 16 additions & 1 deletion config/api.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package config

import (
"fmt"
"os"

"github.com/joho/godotenv"
)

// API related config
type API struct {
Mongo Mongo `hcl:"mongo,block"`
Expand All @@ -10,9 +17,17 @@ type Mongo struct {
}

func GetDefaultAPIConfig() API {
err := godotenv.Load()
if err != nil {
panic("Error loading .env file")
}

return API{
Mongo: Mongo{
Dsn: "mongodb://docker:docker@localhost:27017",
Dsn: fmt.Sprintf("mongodb://%s:%s@localhost:27017",
os.Getenv("MONGODB_USER"),
os.Getenv("MONGODB_PASS"),
),
},
}
}
2 changes: 2 additions & 0 deletions env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MONGODB_USER=
MONGODB_PASS=
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ go 1.22.2
require (
github.com/go-chi/chi/v5 v5.1.0
github.com/hashicorp/hcl/v2 v2.22.0
github.com/joho/godotenv v1.5.1
github.com/matoous/go-nanoid v1.5.1
github.com/segmentio/ksuid v1.0.4
github.com/sqids/sqids-go v0.4.1
go.mongodb.org/mongo-driver v1.17.1
)

Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/hcl/v2 v2.22.0 h1:hkZ3nCtqeJsDhPRFz5EA9iwcG1hNWGePOTw6oyul12M=
github.com/hashicorp/hcl/v2 v2.22.0/go.mod h1:62ZYHrXgPoX8xBnzl8QzbWq4dyDsDtfCRgIq1rbJEvA=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/matoous/go-nanoid v1.5.1 h1:aCjdvTyO9LLnTIi0fgdXhOPPvOHjpXN6Ik9DaNjIct4=
github.com/matoous/go-nanoid v1.5.1/go.mod h1:zyD2a71IubI24efhpvkJz+ZwfwagzgSO6UNiFsZKN7U=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
github.com/segmentio/ksuid v1.0.4 h1:sBo2BdShXjmcugAMwjugoGUdUV0pcxY5mW4xKRn3v4c=
github.com/segmentio/ksuid v1.0.4/go.mod h1:/XUiZBD3kVx5SmUOl55voK5yeAbBNNIed+2O73XgrPE=
github.com/sqids/sqids-go v0.4.1 h1:eQKYzmAZbLlRwHeHYPF35QhgxwZHLnlmVj9AkIj/rrw=
github.com/sqids/sqids-go v0.4.1/go.mod h1:EMwHuPQgSNFS0A49jESTfIQS+066XQTVhukrzEPScl8=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
Expand Down

0 comments on commit 84c8665

Please sign in to comment.