Skip to content

Commit

Permalink
server urls added, resource moved to the root path (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
enver-bisevac committed Oct 18, 2022
1 parent 7317ec9 commit 41cbb66
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.

package repo
package resource

import (
"net/http"
Expand Down
4 changes: 4 additions & 0 deletions internal/api/openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func Generate() *openapi3.Spec {
reflector.Spec.Info.
WithTitle("API Specification").
WithVersion(version.Version.String())
reflector.Spec.Servers = []openapi3.Server{{
URL: "/api/v1/",
}}

//
// register endpoints
Expand All @@ -43,6 +46,7 @@ func Generate() *openapi3.Spec {
buildUsers(&reflector)
spaceOperations(&reflector)
repoOperations(&reflector)
resourceOperations(&reflector)

//
// define security scheme
Expand Down
23 changes: 0 additions & 23 deletions internal/api/openapi/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,6 @@ func repoOperations(reflector *openapi3.Reflector) {
_ = reflector.SetJSONResponse(&createRepository, new(usererror.Error), http.StatusForbidden)
_ = reflector.Spec.AddOperation(http.MethodPost, "/repos", createRepository)

opListGitignore := openapi3.Operation{}
opListGitignore.WithTags("repository")
opListGitignore.WithMapOfAnything(map[string]interface{}{"operationId": "listGitignore"})
_ = reflector.SetRequest(&opListGitignore, new(gitignoreRequest), http.MethodGet)
_ = reflector.SetJSONResponse(&opListGitignore, []string{}, http.StatusOK)
_ = reflector.SetJSONResponse(&opListGitignore, new(usererror.Error), http.StatusInternalServerError)
_ = reflector.SetJSONResponse(&opListGitignore, new(usererror.Error), http.StatusUnauthorized)
_ = reflector.SetJSONResponse(&opListGitignore, new(usererror.Error), http.StatusForbidden)
_ = reflector.Spec.AddOperation(http.MethodGet, "/repos/resources/gitignore", opListGitignore)

opListLicenses := openapi3.Operation{}
opListLicenses.WithTags("repository")
opListLicenses.WithMapOfAnything(map[string]interface{}{"operationId": "listLicenses"})
_ = reflector.SetRequest(&opListLicenses, new(licenseRequest), http.MethodGet)
_ = reflector.SetJSONResponse(&opListLicenses, []struct {
Label string `json:"label"`
Value string `json:"value"`
}{}, http.StatusOK)
_ = reflector.SetJSONResponse(&opListLicenses, new(usererror.Error), http.StatusInternalServerError)
_ = reflector.SetJSONResponse(&opListLicenses, new(usererror.Error), http.StatusUnauthorized)
_ = reflector.SetJSONResponse(&opListLicenses, new(usererror.Error), http.StatusForbidden)
_ = reflector.Spec.AddOperation(http.MethodGet, "/repos/resources/license", opListLicenses)

opFind := openapi3.Operation{}
opFind.WithTags("repository")
opFind.WithMapOfAnything(map[string]interface{}{"operationId": "findRepository"})
Expand Down
37 changes: 37 additions & 0 deletions internal/api/openapi/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2022 Harness Inc. All rights reserved.
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.

package openapi

import (
"github.com/harness/gitness/internal/api/usererror"
"github.com/swaggest/openapi-go/openapi3"
"net/http"
)

//nolint:funlen
func resourceOperations(reflector *openapi3.Reflector) {
opListGitignore := openapi3.Operation{}
opListGitignore.WithTags("resource")
opListGitignore.WithMapOfAnything(map[string]interface{}{"operationId": "listGitignore"})
_ = reflector.SetRequest(&opListGitignore, new(gitignoreRequest), http.MethodGet)
_ = reflector.SetJSONResponse(&opListGitignore, []string{}, http.StatusOK)
_ = reflector.SetJSONResponse(&opListGitignore, new(usererror.Error), http.StatusInternalServerError)
_ = reflector.SetJSONResponse(&opListGitignore, new(usererror.Error), http.StatusUnauthorized)
_ = reflector.SetJSONResponse(&opListGitignore, new(usererror.Error), http.StatusForbidden)
_ = reflector.Spec.AddOperation(http.MethodGet, "/resources/gitignore", opListGitignore)

opListLicenses := openapi3.Operation{}
opListLicenses.WithTags("resource")
opListLicenses.WithMapOfAnything(map[string]interface{}{"operationId": "listLicenses"})
_ = reflector.SetRequest(&opListLicenses, new(licenseRequest), http.MethodGet)
_ = reflector.SetJSONResponse(&opListLicenses, []struct {
Label string `json:"label"`
Value string `json:"value"`
}{}, http.StatusOK)
_ = reflector.SetJSONResponse(&opListLicenses, new(usererror.Error), http.StatusInternalServerError)
_ = reflector.SetJSONResponse(&opListLicenses, new(usererror.Error), http.StatusUnauthorized)
_ = reflector.SetJSONResponse(&opListLicenses, new(usererror.Error), http.StatusForbidden)
_ = reflector.Spec.AddOperation(http.MethodGet, "/resources/license", opListLicenses)
}
13 changes: 9 additions & 4 deletions internal/router/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package router
import (
"context"
"fmt"
"github.com/harness/gitness/internal/api/handler/resource"
"net/http"

"github.com/harness/gitness/internal/api/controller/repo"
Expand Down Expand Up @@ -105,6 +106,7 @@ func setupRoutesV1(r chi.Router, config *types.Config, repoCtrl *repo.Controller
setupAdmin(r, userCtrl)
setupAccount(r, userCtrl)
setupSystem(r)
setupResources(r)
}

func setupSpaces(r chi.Router, spaceCtrl *space.Controller) {
Expand Down Expand Up @@ -141,10 +143,6 @@ func setupRepos(r chi.Router, config *types.Config, repoCtrl *repo.Controller) {
r.Route("/repos", func(r chi.Router) {
// Create takes path and parentId via body, not uri
r.Post("/", handlerrepo.HandleCreate(config, repoCtrl))
r.Route("/resources", func(r chi.Router) {
r.Get("/gitignore", handlerrepo.HandleGitIgnore())
r.Get("/license", handlerrepo.HandleLicence())
})
r.Route(fmt.Sprintf("/{%s}", request.PathParamRepoRef), func(r chi.Router) {
// repo level operations
r.Get("/", handlerrepo.HandleFind(repoCtrl))
Expand Down Expand Up @@ -231,6 +229,13 @@ func setupSystem(r chi.Router) {
})
}

func setupResources(r chi.Router) {
r.Route("/resources", func(r chi.Router) {
r.Get("/gitignore", resource.HandleGitIgnore())
r.Get("/license", resource.HandleLicence())
})
}

func setupAdmin(r chi.Router, _ *user.Controller) {
r.Route("/users", func(r chi.Router) {
r.Post("/", func(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit 41cbb66

Please sign in to comment.