Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
Added seaweedfs client with file upload and basic info stored (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
prabhuomkar authored Sep 19, 2021
1 parent 133d02f commit 83884ce
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 18 deletions.
4 changes: 4 additions & 0 deletions api/.golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ linters:
- gofumpt
- gci
- exhaustivestruct
linters-settings:
lll:
line-length: 128
tab-width: 2
11 changes: 11 additions & 0 deletions api/cmd/iris/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/http"
"time"

"iris/api/internal/config"
"iris/api/internal/graph/generated"
Expand All @@ -16,6 +17,7 @@ import (
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/go-chi/chi"
"github.com/linxGnu/goseaweedfs"
"github.com/rs/cors"
)

Expand All @@ -32,20 +34,29 @@ func main() {
panic(err)
}

seaweed, err := goseaweedfs.NewSeaweed(
cfg.CDN.URL, nil, cfg.CDN.ChunkSize,
&http.Client{Timeout: time.Duration(cfg.CDN.Timeout) * time.Minute})
if err != nil {
panic(err)
}

router := chi.NewRouter()
router.Use(cors.Default().Handler)

c := generated.Config{
Resolvers: &resolvers.Resolver{
Config: &cfg,
DB: db,
CDN: seaweed,
},
}

srv := handler.New(generated.NewExecutableSchema(c))
srv.AddTransport(transport.Options{})
srv.AddTransport(transport.POST{})
srv.AddTransport(transport.GET{})
srv.AddTransport(transport.MultipartForm{})
srv.Use(extension.Introspection{})

router.Handle("/", playground.Handler("graphql playground", "/graphql"))
Expand Down
1 change: 1 addition & 0 deletions api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/golang/snappy v0.0.4 // indirect
github.com/kelseyhightower/envconfig v1.4.0
github.com/klauspost/compress v1.13.5 // indirect
github.com/linxGnu/goseaweedfs v0.1.5 // indirect
github.com/rs/cors v1.8.0
github.com/vektah/gqlparser/v2 v2.1.0
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
Expand Down
6 changes: 6 additions & 0 deletions api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw=
github.com/linxGnu/goseaweedfs v0.1.5 h1:7dChPdq8+fsPH0yqxKEofPhiosaar4LWePm4M+1Taz0=
github.com/linxGnu/goseaweedfs v0.1.5/go.mod h1:Zwe/7H7FJaPQyMTNKXgv6fhVDw6qi34MMJQp1K0VLNc=
github.com/linxGnu/gumble v1.0.0 h1:OAJud8Hy4rmV9I5p/KTRiVpwwklMTd9Ankza3Mz7a4M=
github.com/linxGnu/gumble v1.0.0/go.mod h1:iyhNJpBHvJ0q2Hr41iiZRJyj6LLF47i2a9C9zLiucVY=
github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE=
github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0=
Expand Down Expand Up @@ -111,6 +115,7 @@ github.com/rs/cors v1.8.0 h1:P2KMzcFwrPoSjkF1WLRPsp3UMLyql8L4v9hQpVeK5so=
github.com/rs/cors v1.8.0/go.mod h1:EBwu+T5AvHOcXwvZIkQFjUN6s8Czyqw12GL/Y0tUyRM=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/scryner/lfreequeue v0.0.0-20121212074822-473f33702129/go.mod h1:0OrdloYlIayHGsgKYlwEnmdrPWmuYtbdS6Dm71PprFM=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/shurcooL/httpfs v0.0.0-20171119174359-809beceb2371/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
Expand All @@ -136,6 +141,7 @@ github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVM
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/urfave/cli/v2 v2.1.1 h1:Qt8FeAtxE/vfdrLmR3rxR6JRE0RoVmbXu8+6kZtYU4k=
github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
github.com/valyala/fastrand v1.0.0/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ=
github.com/vektah/dataloaden v0.2.1-0.20190515034641-a19b9a6e7c9e/go.mod h1:/HUdMve7rvxZma+2ZELQeNh88+003LL7Pf/CZ089j8U=
github.com/vektah/gqlparser/v2 v2.1.0 h1:uiKJ+T5HMGGQM2kRKQ8Pxw8+Zq9qhhZhz/lieYvCMns=
github.com/vektah/gqlparser/v2 v2.1.0/go.mod h1:SyUiHgLATUR8BiYURfTirrTcGpcE+4XkV2se04Px1Ms=
Expand Down
11 changes: 10 additions & 1 deletion api/internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package config

import "github.com/kelseyhightower/envconfig"
import (
"github.com/kelseyhightower/envconfig"
)

type (
Database struct {
URI string `envconfig:"DB_URI" default:"mongodb://root:root@database:5010/iris?authSource=admin"`
Name string `enconfig:"DB_NAME" default:"iris"`
}

CDN struct {
URL string `envconfig:"CDN_URL" default:"http://storage-master:5020"`
ChunkSize int64 `envconfig:"CDN_CHUNK_SIZE" default:"1048576"`
Timeout int64 `envconfig:"CDN_TIMEOUT" default:"5"`
}

Config struct {
Database
CDN
Port int `envconfig:"PORT" default:"5001"`
}
)
Expand Down
93 changes: 87 additions & 6 deletions api/internal/graph/generated/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions api/internal/graph/resolvers/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package resolvers
import (
"iris/api/internal/config"
"iris/api/pkg/mongo"

"github.com/linxGnu/goseaweedfs"
)

// This file will not be regenerated automatically.
Expand All @@ -12,4 +14,5 @@ import (
type Resolver struct {
Config *config.Config
DB *mongo.Connection
CDN *goseaweedfs.Seaweed
}
31 changes: 26 additions & 5 deletions api/internal/graph/resolvers/schema.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ package resolvers
import (
"context"
"errors"
"fmt"
"iris/api/internal/graph/generated"
"iris/api/internal/models"
"time"

"github.com/99designs/gqlgen/graphql"
"go.mongodb.org/mongo-driver/bson"
Expand All @@ -16,7 +18,28 @@ import (
)

func (r *mutationResolver) Upload(ctx context.Context, file graphql.Upload) (bool, error) {
return false, nil
result, err := r.CDN.Upload(file.File, file.Filename, file.Size, "", "")
if err != nil {
return false, err
}

// later(omkar): Calculate media metadata

_, err = r.DB.Collection(models.ColMediaItems).InsertOne(ctx, bson.D{
{Key: "imageUrl", Value: fmt.Sprintf("http://%s/%s", result.Server, result.FileID)},
{Key: "description", Value: nil},
{Key: "mimeType", Value: result.MimeType},
{Key: "fileName", Value: result.FileName},
{Key: "fileSize", Value: result.FileSize},
{Key: "mediaMetadata", Value: nil},
{Key: "createdAt", Value: time.Now()},
{Key: "updatedAt", Value: time.Now()},
})
if err != nil {
return false, err
}

return true, nil
}

func (r *mutationResolver) UpdateEntity(ctx context.Context, id string, name string) (bool, error) {
Expand Down Expand Up @@ -98,17 +121,15 @@ func (r *queryResolver) MediaItems(ctx context.Context, page *int, limit *int) (
}, nil
}

func (r *queryResolver) Search(ctx context.Context, q string,
page *int, limit *int) (*models.MediaItemConnection, error) {
func (r *queryResolver) Search(ctx context.Context, q string, page *int, limit *int) (*models.MediaItemConnection, error) {
return nil, nil
}

func (r *queryResolver) Explore(ctx context.Context) (*models.ExploreResponse, error) {
return nil, nil
}

func (r *queryResolver) Entity(ctx context.Context, id string,
page *int, limit *int) (*models.MediaItemConnection, error) {
func (r *queryResolver) Entity(ctx context.Context, id string, page *int, limit *int) (*models.MediaItemConnection, error) {
return nil, nil
}

Expand Down
7 changes: 5 additions & 2 deletions api/internal/models/mediaitem.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package models

import "time"

const ColMediaItems = "mediaitems"

type (
Expand All @@ -9,9 +11,10 @@ type (
ImageURL string `json:"imageUrl"`
MimeType string `json:"mimeType"`
FileName string `json:"fileName"`
FileSize int64 `json:"fileSize"`
MediaMetadata *MediaMetaData `json:"mediaMetadata"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}

MediaMetaData struct {
Expand Down
Loading

0 comments on commit 83884ce

Please sign in to comment.