Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding ability to download an artifact #23

Merged
merged 1 commit into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 42 additions & 9 deletions cmd/archivist/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package main
import (
"context"
"fmt"
"io"
"net"
"net/http"
"os"
Expand All @@ -30,19 +31,19 @@ import (
"syscall"
"time"

"entgo.io/contrib/entgql"
root "github.com/testifysec/archivist"

"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/testifysec/archivist-api/pkg/api/archivist"
"github.com/testifysec/archivist/internal/config"
"github.com/testifysec/archivist/internal/metadatastorage/mysqlstore"
"github.com/testifysec/archivist/internal/objectstorage/blobstore"
"github.com/testifysec/archivist/internal/objectstorage/filestore"
"github.com/testifysec/archivist/internal/server"

"entgo.io/contrib/entgql"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
nested "github.com/antonfisher/nested-logrus-formatter"
"github.com/gorilla/mux"
"github.com/networkservicemesh/sdk/pkg/tools/debug"
"github.com/networkservicemesh/sdk/pkg/tools/grpcutils"
"github.com/networkservicemesh/sdk/pkg/tools/log"
Expand Down Expand Up @@ -135,7 +136,7 @@ func main() {

log.FromContext(ctx).WithField("duration", time.Since(now)).Infof("completed phase 4: create and register grpc server")
// ********************************************************************************
log.FromContext(ctx).Infof("executing phase 4: create and register grpc service (time since start: %s)", time.Since(startTime))
log.FromContext(ctx).Infof("executing phase 5: create and register http service (time since start: %s)", time.Since(startTime))
// ********************************************************************************
now = time.Now()

Expand All @@ -144,12 +145,19 @@ func main() {
srv := handler.NewDefaultServer(root.NewSchema(client))
srv.Use(entgql.Transactioner{TxOpener: client})

router := mux.NewRouter()

if cfg.GraphqlWebClientEnable {
http.Handle("/",
router.Handle("/",
playground.Handler("Archivist", "/query"),
)
}
http.Handle("/query", srv)
router.Handle("/query", srv)

p := &proxy{
client: fileStore,
}
router.Handle("/download/{gitoid}", p)

gqlAddress := cfg.GraphqlListenOn
gqlAddress = strings.ToLower(strings.TrimSpace(gqlAddress))
Expand All @@ -168,15 +176,15 @@ func main() {
}

go func() {
if err := http.Serve(gqlListener, nil); err != nil {
if err := http.Serve(gqlListener, router); err != nil {
log.FromContext(ctx).Error("http server terminated", err)
}
}()
} else {
log.FromContext(ctx).Info("graphql disabled, skipping")
}

log.FromContext(ctx).WithField("duration", time.Since(now)).Infof("completed phase 5: create and register graphql server")
log.FromContext(ctx).WithField("duration", time.Since(now)).Infof("completed phase 5: create and register http service")

log.FromContext(ctx).Infof("startup complete (time since start: %s)", time.Since(startTime))

Expand Down Expand Up @@ -264,3 +272,28 @@ func exitOnErrCh(ctx context.Context, cancel context.CancelFunc, errCh <-chan er
cancel()
}(ctx, errCh)
}

type proxy struct {
client server.ObjectStorer
}

func (p *proxy) ServeHTTP(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
ctx := context.Background()
attestation, err := p.client.Get(ctx, &archivist.GetRequest{
Gitoid: vars["gitoid"],
})
if err != nil {
log.Default().Error(err)
// TODO handle error codes more effectively
w.WriteHeader(400)
return
}
_, err = io.Copy(w, attestation)
if err != nil {
log.Default().Error(err)
// TODO handle error codes more effectively
w.WriteHeader(500)
return
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/git-bom/gitbom-go v0.0.0-20220502033008-4a48bb2317f7
github.com/go-sql-driver/mysql v1.6.0
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/hashicorp/go-multierror v1.1.1
github.com/kelseyhightower/envconfig v1.4.0
github.com/minio/minio-go v6.0.14+incompatible
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
github.com/gorilla/mux v0.0.0-20181024020800-521ea7b17d02/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
Expand Down