Skip to content

Commit

Permalink
refactor: simplify archivista, move API to server
Browse files Browse the repository at this point in the history
This commit simplifies the archivista cmd (`cmd/archivista`),
removing the API logic to the server, where all handlers for
HTTP requests are implemented.

This also includes the API Swagger documentation.

Signed-off-by: Kairo de Araujo <[email protected]>
  • Loading branch information
kairoaraujo committed Jan 10, 2024
1 parent 3cc15de commit 0186b98
Show file tree
Hide file tree
Showing 8 changed files with 700 additions and 42 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ lint: ## Run linter
@go vet ./...


.PHONY: docs
docs: ## Generate swagger docs
@go install github.com/swaggo/swag/cmd/swag@latest
@swag init -o docs -d internal/server -g server.go -pd

.PHONY: db-migrations
db-migrations: ## Run the migrations for the database
Expand Down
29 changes: 6 additions & 23 deletions cmd/archivista/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 The Archivista Contributors
// Copyright 2022-2024 The Archivista Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,13 +30,8 @@ import (
"syscall"
"time"

"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/handlers"
"github.com/gorilla/mux"
"github.com/in-toto/archivista"
"github.com/in-toto/archivista/internal/config"
"github.com/in-toto/archivista/internal/metadatastorage/sqlstore"
"github.com/in-toto/archivista/internal/objectstorage/blobstore"
Expand Down Expand Up @@ -108,22 +103,10 @@ func main() {
logrus.Infof("executing phase 3: create and register http service (time since start: %s)", time.Since(startTime))
// ********************************************************************************
now = time.Now()
server := server.New(sqlStore, fileStore)
router := mux.NewRouter()
router.HandleFunc("/download/{gitoid}", server.GetHandler)
router.HandleFunc("/upload", server.StoreHandler)

if cfg.EnableGraphql {
client := sqlStore.GetClient()
srv := handler.NewDefaultServer(archivista.NewSchema(client))
srv.Use(entgql.Transactioner{TxOpener: client})
router.Handle("/query", srv)
if cfg.GraphqlWebClientEnable {
router.Handle("/",
playground.Handler("Archivista", "/query"),
)
}
}

// initialize the server
sqlClient := sqlStore.GetClient()
server := server.New(sqlStore, fileStore, cfg, sqlClient)

listenAddress := cfg.ListenOn
listenAddress = strings.ToLower(strings.TrimSpace(listenAddress))
Expand All @@ -146,7 +129,7 @@ func main() {
handlers.AllowedOrigins(cfg.CORSAllowOrigins),
handlers.AllowedMethods([]string{"GET", "POST", "OPTIONS"}),
handlers.AllowedHeaders([]string{"Accept", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization"}),
)(router)); err != nil {
)(server.Router())); err != nil {
logrus.Fatalf("unable to start http server: %+v", err)
}
}()
Expand Down
230 changes: 230 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
// Package docs Code generated by swaggo/swag. DO NOT EDIT
package docs

import "github.com/swaggo/swag"

const docTemplate = `{
"schemes": {{ marshal .Schemes }},
"swagger": "2.0",
"info": {
"description": "{{escape .Description}}",
"title": "{{.Title}}",
"contact": {
"name": "Archivista Contributors",
"url": "https://github.com/in-toto/archivista/issues/new"
},
"license": {
"url": "https://opensource.org/licenses/Apache-2"
},
"version": "{{.Version}}"
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/donwload/{gitoid}": {
"post": {
"description": "download an attestation",
"produces": [
"application/json"
],
"summary": "Download",
"deprecated": true,
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/dsse.Envelope"
}
}
}
}
},
"/upload": {
"post": {
"description": "stores an attestation",
"produces": [
"application/json"
],
"summary": "Store",
"deprecated": true,
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/api.StoreResponse"
}
}
}
}
},
"/v1/donwload/{gitoid}": {
"post": {
"description": "download an attestation",
"produces": [
"application/json"
],
"tags": [
"attestation"
],
"summary": "Download",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/dsse.Envelope"
}
}
}
}
},
"/v1/query": {
"post": {
"description": "GraphQL query",
"produces": [
"application/json"
],
"tags": [
"graphql"
],
"summary": "Query GraphQL",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/archivista.Resolver"
}
}
}
}
},
"/v1/upload": {
"post": {
"description": "stores an attestation",
"produces": [
"application/json"
],
"tags": [
"attestation"
],
"summary": "Store",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/api.StoreResponse"
}
}
}
}
}
},
"definitions": {
"api.StoreResponse": {
"type": "object",
"properties": {
"gitoid": {
"type": "string"
}
}
},
"archivista.Resolver": {
"type": "object"
},
"dsse.Envelope": {
"type": "object",
"properties": {
"payload": {
"type": "array",
"items": {
"type": "integer"
}
},
"payloadType": {
"type": "string"
},
"signatures": {
"type": "array",
"items": {
"$ref": "#/definitions/dsse.Signature"
}
}
}
},
"dsse.Signature": {
"type": "object",
"properties": {
"certificate": {
"type": "array",
"items": {
"type": "integer"
}
},
"intermediates": {
"type": "array",
"items": {
"type": "array",
"items": {
"type": "integer"
}
}
},
"keyid": {
"type": "string"
},
"sig": {
"type": "array",
"items": {
"type": "integer"
}
},
"timestamps": {
"type": "array",
"items": {
"$ref": "#/definitions/dsse.SignatureTimestamp"
}
}
}
},
"dsse.SignatureTimestamp": {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "integer"
}
},
"type": {
"$ref": "#/definitions/dsse.SignatureTimestampType"
}
}
},
"dsse.SignatureTimestampType": {
"type": "string",
"enum": [
"tsp"
],
"x-enum-varnames": [
"TimestampRFC3161"
]
}
}
}`

// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{
Version: "v1",
Host: "",
BasePath: "",
Schemes: []string{},
Title: "Archivista API",
Description: "Archivista API",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
LeftDelim: "{{",
RightDelim: "}}",
}

func init() {
swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo)
}
Loading

0 comments on commit 0186b98

Please sign in to comment.