console/server, apigen: feature flag for new generated console api

Added a feture flag which will be used to indicate if new generated console api is used.
Fixed some comments from previous PR.

Change-Id: Ice31c998b0b347028a491c971a648fd1269bfd49
This commit is contained in:
Vitalii Shpital 2022-02-11 17:06:52 +02:00 committed by Maximillian von Briesen
parent 1da9697995
commit ba6956db0f
6 changed files with 25 additions and 9 deletions

View File

@ -7,5 +7,6 @@ import "net/http"
// Auth exposes methods to control authentication process for each endpoint.
type Auth interface {
// IsAuthenticated checks if request is performed with all needed authorization credentials.
IsAuthenticated(r *http.Request) error
}

View File

@ -44,7 +44,7 @@ func (a *API) MustWrite(path string) {
panic(errs.Wrap(err))
}
err = os.WriteFile(path+"api.gen.go", generated, 0755)
err = os.WriteFile(path, generated, 0644)
if err != nil {
panic(errs.Wrap(err))
}
@ -112,15 +112,12 @@ func (a *API) generateGo() ([]byte, error) {
p("type %sService interface {", group.Name)
for _, method := range group.Endpoints {
responseType := reflect.TypeOf(method.Response)
if strings.Contains(responseType.String(), a.PackageName) {
p("%s(context.Context) (%s, api.HTTPError)", method.MethodName, responseType.Elem().Name())
} else {
p("%s(context.Context) (%s, api.HTTPError)", method.MethodName, responseType)
}
p("%s(context.Context) (%s, api.HTTPError)", method.MethodName, a.handleTypesPackage(responseType))
}
p("}")
p("")
p("// Handler is an api handler that exposes all %s related functionality.", group.Prefix)
p("type Handler struct {")
p("log *zap.Logger")
p("service %sService", group.Name)
@ -129,13 +126,14 @@ func (a *API) generateGo() ([]byte, error) {
p("")
p(
"func New%s(log *zap.Logger, service %sService, router *mux.Router) *Handler {",
"func New%s(log *zap.Logger, service %sService, router *mux.Router, auth api.Auth) *Handler {",
group.Name,
group.Name,
)
p("handler := &Handler{")
p("log: log,")
p("service: service,")
p("auth: auth,")
p("}")
p("")
p("%sRouter := router.PathPrefix(\"/api/v0/%s\").Subrouter()", group.Prefix, group.Prefix)
@ -202,3 +200,14 @@ func (a *API) generateGo() ([]byte, error) {
return output, nil
}
// handleTypesPackage handles the way some type is used in generated code.
// If type is from the same package then we use only type's name.
// If type is from external package then we use type along with it's appropriate package name.
func (a *API) handleTypesPackage(t reflect.Type) interface{} {
if strings.HasPrefix(t.String(), a.PackageName) {
return t.Elem().Name()
}
return t
}

4
satellite/console/consoleweb/consoleapi/api.gen.go Executable file → Normal file
View File

@ -22,16 +22,18 @@ type ProjectManagementService interface {
GetUserProjects(context.Context) ([]console.Project, api.HTTPError)
}
// Handler is an api handler that exposes all projects related functionality.
type Handler struct {
log *zap.Logger
service ProjectManagementService
auth api.Auth
}
func NewProjectManagement(log *zap.Logger, service ProjectManagementService, router *mux.Router) *Handler {
func NewProjectManagement(log *zap.Logger, service ProjectManagementService, router *mux.Router, auth api.Auth) *Handler {
handler := &Handler{
log: log,
service: service,
auth: auth,
}
projectsRouter := router.PathPrefix("/api/v0/projects").Subrouter()

View File

@ -29,5 +29,5 @@ func main() {
}
a.MustWrite("satellite/console/consoleweb/consoleapi/")
a.MustWrite("satellite/console/consoleweb/consoleapi/api.gen.go")
}

View File

@ -94,6 +94,7 @@ type Config struct {
NewProjectDashboard bool `help:"indicates if new project dashboard should be used" default:"false"`
NewNavigation bool `help:"indicates if new navigation structure should be rendered" default:"true"`
NewObjectsFlow bool `help:"indicates if new objects flow should be used" default:"true"`
GeneratedAPIEnabled bool `help:"indicates if generated console api should be used" default:"false"`
InactivityTimerEnabled bool `help:"indicates if session can be timed out due inactivity" default:"false"`
InactivityTimerDelay int `help:"inactivity timer delay in seconds" default:"600"`

View File

@ -178,6 +178,9 @@ compensation.withheld-percents: 75,75,75,50,50,50,25,25,25,0,0,0,0,0,0
# url link to general request page
# console.general-request-url: https://supportdcs.storj.io/hc/en-us/requests/new?ticket_form_id=360000379291
# indicates if generated console api should be used
# console.generated-api-enabled: false
# inactivity timer delay in seconds
# console.inactivity-timer-delay: 600