Skip to content
This repository has been archived by the owner on Jul 18, 2023. It is now read-only.

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
clambin committed Mar 16, 2021
0 parents commit 7ed4129
Show file tree
Hide file tree
Showing 16 changed files with 1,371 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .github/dependabot.yml
@@ -0,0 +1,15 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "gomod" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "daily"
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
19 changes: 19 additions & 0 deletions .github/workflows/analysis.yml
@@ -0,0 +1,19 @@
name: Analysis

on:
push:
pull_request:

jobs:
analyse:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: 1.15
- name: checks
run: |
go get -u golang.org/x/lint/golint
golint ./...
go vet ./...
70 changes: 70 additions & 0 deletions .github/workflows/codeql-analysis.yml
@@ -0,0 +1,70 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches:
- master
- develop
pull_request:
# The branches below must be a subset of the branches above
branches:
- develop
schedule:
- cron: '23 3 * * 6'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
language: [ 'go' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
# Learn more:
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed

steps:
- name: Checkout repository
uses: actions/checkout@v2

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
@@ -0,0 +1,22 @@
name: Test

on:
push:
pull_request_target:

jobs:
test:
runs-on: ubuntu-latest
container: golang:1.15
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: 1.15
- name: test
run: |
go test ./... -race -coverprofile=coverage.txt -covermode=atomic
- uses: codecov/codecov-action@v1
with:
file: coverage.txt
token: ${{ secrets.CODECOV_TOKEN }}
22 changes: 22 additions & 0 deletions LICENSE.md
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2021 Christophe Lambin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

8 changes: 8 additions & 0 deletions README.md
@@ -0,0 +1,8 @@
# grafana-json
![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/clambin/grafana-json?color=green&label=Release&style=plastic)
![Codecov](https://img.shields.io/codecov/c/gh/clambin/grafana-json?style=plastic)
![Test](https://github.com/clambin/grafana-json/workflows/Test/badge.svg)
![Go Report Card](https://goreportcard.com/badge/github.com/clambin/grafana-json)
![GitHub](https://img.shields.io/github/license/clambin/grafana-json?style=plastic)

Basic Go implementation of a Grafana Simple JSON API server
94 changes: 94 additions & 0 deletions endpoints.go
@@ -0,0 +1,94 @@
package grafana_json

import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
)

// endpoints

func (apiServer *APIServer) hello(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintf(w, "Hello")
}

func (apiServer *APIServer) search(w http.ResponseWriter, _ *http.Request) {
targets := apiServer.apiHandler.Search()
if output, err := json.Marshal(targets); err == nil {
w.WriteHeader(http.StatusOK)
_, _ = w.Write(output)
} else {
log.WithField("err", err).Warning("failed to create search response")
http.Error(w, "failed to create search response", http.StatusInternalServerError)
}
}

func (apiServer *APIServer) query(w http.ResponseWriter, req *http.Request) {
var (
err error
bytes []byte
request QueryRequest
responses []*QueryResponse
tableResponses []*QueryTableResponse
output []byte
)
defer req.Body.Close()

if bytes, err = ioutil.ReadAll(req.Body); err == nil {
err = json.Unmarshal(bytes, &request)
}

if err != nil {
log.WithField("err", err).Warning("failed to parse request")
http.Error(w, "failed to parse request", http.StatusBadRequest)
return
}

responses, tableResponses, err = apiServer.handleQueryRequest(&request)

if err == nil {
packed := make([]interface{}, 0)
for _, response := range responses {
packed = append(packed, response)
}
for _, response := range tableResponses {
packed = append(packed, response)
}

output, err = json.Marshal(packed)
}

if err == nil {
w.WriteHeader(http.StatusOK)
_, _ = w.Write(output)
} else {
log.WithField("err", err).Warning("unable to create response")
http.Error(w, "unable to create response", http.StatusInternalServerError)
}
}

func (apiServer *APIServer) handleQueryRequest(request *QueryRequest) (responses []*QueryResponse, tableResponses []*QueryTableResponse, err error) {
for _, target := range request.Targets {
switch target.Type {
case "timeserie", "":
var response *QueryResponse
if response, err = apiServer.apiHandler.Query(target.Target, request); err == nil {
response.Target = target.Target
responses = append(responses, response)
}
case "table":
var response *QueryTableResponse
if response, err = apiServer.apiHandler.QueryTable(target.Target, request); err == nil {
tableResponses = append(tableResponses, response)
}
default:
log.WithFields(log.Fields{"target": target.Target, "type": target.Type}).Warning("invalid target type")
err = fmt.Errorf("unsupport target type: %s", target.Type)
break
}
}
return
}
10 changes: 10 additions & 0 deletions go.mod
@@ -0,0 +1,10 @@
module github.com/clambin/grafana-json

go 1.15

require (
github.com/gorilla/mux v1.8.0
github.com/prometheus/client_golang v1.9.0
github.com/sirupsen/logrus v1.6.0
github.com/stretchr/testify v1.7.0
)

0 comments on commit 7ed4129

Please sign in to comment.