8000 Implement a way to generate the http endpoints by Nexucis · Pull Request #2 · perses/perses · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

Implement a way to generate the http endpoints #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ jobs:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: checkout
10000 uses: actions/checkout@v2
- name: generate files
run: make generate
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.idea/
bin/

internal/api/impl/v1/project/endpoint.go
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ fmt:
@echo ">> format code"
$(GO) fmt $(pkgs)

test:
test: generate
@echo ">> running all tests"
$(GO) test -count=1 -v $(pkgs)

.PHONY: build
build:
build: generate
@echo ">> build the perses api"
CGO_ENABLED=0 GOARCH=${GOARCH} $(GO) build -a -installsuffix cgo ${LDFLAGS} -o ./bin/perses ./cmd/perses

.PHONY: generate
generate:
$(GO) generate ./internal/api
4 changes: 4 additions & 0 deletions internal/api/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package api

// this file is just there to run the command generate
//go:generate go run generate.go -package=project -plural=projects -kind=Project
93 changes: 93 additions & 0 deletions internal/api/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// +build ignore

package main

import (
"bytes"
"flag"
"fmt"
"go/format"
"io/ioutil"
"log"
"os"
"text/template"
)

const tpl = `{{- $endpoint := . -}}
{{- $package := $endpoint.PackageName -}}
{{- $plural := $endpoint.Plural -}}
{{- $kind := $endpoint.Kind -}}
// Code generated. DO NOT EDIT
package {{ $package }}

import (
"github.com/labstack/echo/v4"
"github.com/perses/perses/internal/api/interface/v1/{{ $package }}"
"github.com/perses/perses/internal/api/shared"
v1 "github.com/perses/perses/pkg/model/api/v1"
)

type Endpoint struct {
toolbox shared.Toolbox
}

func NewEndpoint(service {{ $package }}.Service) *Endpoint {
return &Endpoint{
toolbox: shared.NewToolBox(service),
}
}

func (e *Endpoint) RegisterRoutes(g *echo.Group) {
projectGroup := g.Group("/{{ $plural }}")
projectGroup.POST("", e.Create)
}

func (e *Endpoint) Create(ctx echo.Context) error {
entity := &v1.{{ $kind }}{}
return e.toolbox.Create(ctx, entity)
}
`

var endpointTemplate = template.Must(
template.New("").Parse(tpl))

// endpoint is the struct that will be used to generate the template
type endpoint struct {
PackageName string
Plural string
Kind string
}

func main() {
pkg := flag.String("package", "", "the name of the package that needs to be generated. It should match the name of the resource you would like to expose through http")
plural := flag.String("plural", "", "the plural of the resource name")
kind := flag.String("kind", "", "the name of the resource with the appropriate cases")
flag.Parse()

if len(*pkg) == 0 || len(*plural) == 0 || len(*kind) == 0 {
log.Fatal("unable to generate endpoint, missing parameter")
}

folder := fmt.Sprintf("./impl/v1/%s", *pkg)
if _, err := os.Stat(folder); os.IsNotExist(err) {
os.Mkdir(folder, 0755)
}
endpointFile := fmt.Sprintf("%s/endpoint.go", folder)
ept := endpoint{
PackageName: *pkg,
Plural: *plural,
Kind: *kind,
}
builder := &bytes.Buffer{}
if err := endpointTemplate.Execute(builder, ept); err != nil {
log.Fatal("Error while executing the template:", err)
}
data, err := format.Source(builder.Bytes())
if err != nil {
log.Fatal("Error while formatting generated code:", err)
}
if err := ioutil.WriteFile(endpointFile, data, 0644); err != nil {
log.Fatal("Error writing endpoint file:", err)
}

}
28 changes: 0 additions & 28 deletions internal/api/impl/v1/project/endpoint.go

This file was deleted.

0