From 5d6ee506b0a23dd369a8f8dced05fbf70b34c5ed Mon Sep 17 00:00:00 2001 From: Vitalii Shpital Date: Fri, 10 Dec 2021 19:15:33 +0200 Subject: [PATCH] private/apigen: initial typings and methods Initial typings and methods for the future API Management implementation. Blueprint - https://review.dev.storj.io/c/storj/storj/+/6341 Change-Id: I9afe66a7610a3a80e764b317ddd211f125b1ea43 --- private/apigen/api.go | 32 +++++++++++ private/apigen/endpoint.go | 56 +++++++++++++++++++ .../console/consoleweb/consoleapi/gen/main.go | 26 +++++++++ 3 files changed, 114 insertions(+) create mode 100644 private/apigen/api.go create mode 100644 private/apigen/endpoint.go create mode 100644 satellite/console/consoleweb/consoleapi/gen/main.go diff --git a/private/apigen/api.go b/private/apigen/api.go new file mode 100644 index 000000000..a1721c3e4 --- /dev/null +++ b/private/apigen/api.go @@ -0,0 +1,32 @@ +// Copyright (C) 2022 Storj Labs, Inc. +// See LICENSE for copying information. + +package apigen + +// API represents specific API's configuration. +type API struct { + Version string + Description string + EndpointGroups []*EndpointGroup +} + +// New creates new API with specific configuration. +func New(version, description string) *API { + return &API{ + Version: version, + Description: description, + } +} + +// Group adds new endpoints group to API. +func (a *API) Group(name, prefix string) *EndpointGroup { + group := &EndpointGroup{ + Name: name, + Prefix: prefix, + Endpoints: make(map[PathMethod]*Endpoint), + } + + a.EndpointGroups = append(a.EndpointGroups, group) + + return group +} diff --git a/private/apigen/endpoint.go b/private/apigen/endpoint.go new file mode 100644 index 000000000..1a42cfd37 --- /dev/null +++ b/private/apigen/endpoint.go @@ -0,0 +1,56 @@ +// Copyright (C) 2022 Storj Labs, Inc. +// See LICENSE for copying information. + +package apigen + +import "net/http" + +// Endpoint represents endpoint's configuration. +type Endpoint struct { + Name string + Description string + MethodName string + NoCookieAuth bool + NoAPIAuth bool + Request interface{} + Response interface{} + Params []string +} + +// CookieAuth returns endpoint's cookie auth status. +func (e *Endpoint) CookieAuth() bool { + return !e.NoCookieAuth +} + +// APIAuth returns endpoint's API auth status. +func (e *Endpoint) APIAuth() bool { + return !e.NoAPIAuth +} + +// PathMethod represents endpoint's path and method type. +type PathMethod struct { + Path string + Method string +} + +// EndpointGroup represents endpoints group. +type EndpointGroup struct { + Name string + Prefix string + Endpoints map[PathMethod]*Endpoint +} + +// Get adds new GET endpoint to endpoints group. +func (eg *EndpointGroup) Get(path string, endpoint *Endpoint) { + eg.addEndpoint(path, http.MethodGet, endpoint) +} + +// addEndpoint adds new endpoint to endpoints list. +func (eg *EndpointGroup) addEndpoint(path, method string, endpoint *Endpoint) { + pathMethod := PathMethod{ + Path: path, + Method: method, + } + + eg.Endpoints[pathMethod] = endpoint +} diff --git a/satellite/console/consoleweb/consoleapi/gen/main.go b/satellite/console/consoleweb/consoleapi/gen/main.go new file mode 100644 index 000000000..e6c74f230 --- /dev/null +++ b/satellite/console/consoleweb/consoleapi/gen/main.go @@ -0,0 +1,26 @@ +// Copyright (C) 2022 Storj Labs, Inc. +// See LICENSE for copying information. + +package main + +//go:generate go run ./ + +import ( + "storj.io/storj/private/apigen" + "storj.io/storj/satellite/console" +) + +func main() { + api := apigen.New("v1", "") + + { + g := api.Group("Projects", "projects") + + g.Get("/", &apigen.Endpoint{ + Name: "List Projects", + Description: "Lists all projects user has", + MethodName: "ListUserProjects", + Response: []console.Project{}, + }) + } +}