Skip to content

Commit

Permalink
console/server, apigen: feature flag for new generated console api
Browse files Browse the repository at this point in the history
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
  • Loading branch information
VitaliiShpital authored and mobyvb committed Feb 28, 2022
1 parent 1da9697 commit ba6956d
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions private/api/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
23 changes: 16 additions & 7 deletions private/apigen/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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 changes: 3 additions & 1 deletion satellite/console/consoleweb/consoleapi/api.gen.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion satellite/console/consoleweb/consoleapi/gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ func main() {

}

a.MustWrite("satellite/console/consoleweb/consoleapi/")
a.MustWrite("satellite/console/consoleweb/consoleapi/api.gen.go")
}
1 change: 1 addition & 0 deletions satellite/console/consoleweb/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down
3 changes: 3 additions & 0 deletions scripts/testdata/satellite-config.yaml.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit ba6956d

Please sign in to comment.