Skip to content
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

Add templates support on top of prometheus rules #2

Merged
merged 15 commits into from
Mar 8, 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
vendor/

# IDEs
.idea
Expand Down
14 changes: 13 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@ clean:
rm -rf siren dist/

test:
go list ./... | grep -v extern | xargs go test -count 1 -cover -race -timeout 1m
go test ./... -coverprofile=coverage.out

test-coverage: test
go tool cover -html=coverage.out

dist:
@bash ./scripts/build.sh

check-swagger:
which swagger || (GO111MODULE=off go get -u github.com/go-swagger/go-swagger/cmd/swagger)

swagger: check-swagger
GO111MODULE=on go mod vendor && GO111MODULE=off swagger generate spec -o ./swagger.yaml --scan-models

swagger-serve: check-swagger
swagger serve -F=swagger swagger.yaml
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ Siren requires the following dependencies:
* Golang (version 1.15 or above)
* Git

Run the application dependecies using Docker:

```
$ docker-compose up
```

Update the configs(db credentials etc.) as per your dev machine and docker configs.

Run the following commands to compile from source

```
Expand All @@ -23,7 +31,13 @@ $ go build main.go
To run tests locally

```
$ go test
$ make test
```

To run tests locally with coverage

```
$ make test-coverage
```

To run server locally
Expand All @@ -32,6 +46,9 @@ To run server locally
$ go run main.go serve
```

To view swagger docs of HTTP APIs visit `/docs` route on the server.
e.g. [http://localhost:3000/docs](http://localhost:3000/docs)

#### Config

The config file used by application is `config.yaml` which should be present at the root of this directory.
Expand Down
12 changes: 12 additions & 0 deletions api/handlers/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package handlers

import (
"net/http"
)

func SwaggerFile() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./swagger.yaml")
return
}
}
109 changes: 109 additions & 0 deletions api/handlers/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package handlers

import (
"encoding/json"
"errors"
"github.com/gorilla/mux"
"github.com/odpf/siren/domain"
"net/http"
)

// UpsertTemplates handler
func UpsertTemplates(service domain.TemplatesService) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var template domain.Template
err := json.NewDecoder(r.Body).Decode(&template)
if err != nil {
badRequest(w, err)
return
}
createdTemplate, err := service.Upsert(&template)
if err != nil && err.Error() == ("name cannot be empty") {
badRequest(w, err)
return
}
if err != nil && err.Error() == ("body cannot be empty") {
badRequest(w, err)
return
}
if err != nil {
internalServerError(w, err)
return
}
returnJSON(w, createdTemplate)
return
}
}

// IndexTemplates handler
func IndexTemplates(service domain.TemplatesService) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
tag := r.URL.Query().Get("tag")
templates, err := service.Index(tag)
if err != nil {
internalServerError(w, err)
return
}
returnJSON(w, templates)
return
}
}

// GetTemplates handler
func GetTemplates(service domain.TemplatesService) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
name := params["name"]
templates, err := service.GetByName(name)
if err != nil {
internalServerError(w, err)
return
}
if templates == nil {
NotFound(w, errors.New(notFoundErrorMessage))
return
}
returnJSON(w, templates)
return
}
}

// DeleteTemplates handler
func DeleteTemplates(service domain.TemplatesService) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
name := params["name"]
err := service.Delete(name)
if err != nil {
internalServerError(w, err)
return
}
returnJSON(w, nil)
return
}
}

// RenderTemplates handler
func RenderTemplates(service domain.TemplatesService) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
name := params["name"]
var body map[string]string
err := json.NewDecoder(r.Body).Decode(&body)
if err != nil {
badRequest(w, err)
return
}
renderedBody, err := service.Render(name, body)
if err != nil && err.Error() == "template not found" {
NotFound(w, err)
return
}
if err != nil {
internalServerError(w, err)
return
}
returnJSON(w, renderedBody)
return
}
}
Loading