Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
f2ce8f26
Commit
f2ce8f26
authored
Jan 21, 2016
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3810 from raintank/apiPlugin
ApiPlugin custom header support
parents
b150452d
dcde1422
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
104 additions
and
19 deletions
+104
-19
pkg/api/api_plugin.go
+51
-12
pkg/models/app_settings.go
+14
-1
pkg/plugins/api_plugin.go
+14
-6
pkg/plugins/app_plugin.go
+12
-0
pkg/services/sqlstore/app_settings.go
+13
-0
No files found.
pkg/api/api_plugin.go
View file @
f2ce8f26
package
api
import
(
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"text/template"
"gopkg.in/macaron.v1"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/middleware"
m
"github.com/grafana/grafana/pkg/models"
...
...
@@ -34,32 +38,28 @@ func InitApiPluginRoutes(r *macaron.Macaron) {
handlers
=
append
(
handlers
,
middleware
.
RoleAuth
(
m
.
ROLE_EDITOR
,
m
.
ROLE_ADMIN
))
}
}
handlers
=
append
(
handlers
,
ApiPlugin
(
route
.
Url
))
handlers
=
append
(
handlers
,
ApiPlugin
(
route
,
plugin
.
IncludedInAppId
))
r
.
Route
(
url
,
route
.
Method
,
handlers
...
)
log
.
Info
(
"Plugin: Adding route %s"
,
url
)
}
}
}
func
ApiPlugin
(
route
Url
string
)
macaron
.
Handler
{
func
ApiPlugin
(
route
*
plugins
.
ApiPluginRoute
,
includedInAppId
string
)
macaron
.
Handler
{
return
func
(
c
*
middleware
.
Context
)
{
path
:=
c
.
Params
(
"*"
)
//Create a HTTP header with the context in it.
ctx
,
err
:=
json
.
Marshal
(
c
.
SignedInUser
)
if
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"failed to marshal context to json."
,
err
)
return
}
targetUrl
,
_
:=
url
.
Parse
(
routeUrl
)
proxy
:=
NewApiPluginProxy
(
string
(
ctx
),
path
,
targetUrl
)
proxy
:=
NewApiPluginProxy
(
c
,
path
,
route
,
includedInAppId
)
proxy
.
Transport
=
dataProxyTransport
proxy
.
ServeHTTP
(
c
.
Resp
,
c
.
Req
.
Request
)
}
}
func
NewApiPluginProxy
(
ctx
string
,
proxyPath
string
,
targetUrl
*
url
.
URL
)
*
httputil
.
ReverseProxy
{
func
NewApiPluginProxy
(
ctx
*
middleware
.
Context
,
proxyPath
string
,
route
*
plugins
.
ApiPluginRoute
,
includedInAppId
string
)
*
httputil
.
ReverseProxy
{
targetUrl
,
_
:=
url
.
Parse
(
route
.
Url
)
director
:=
func
(
req
*
http
.
Request
)
{
req
.
URL
.
Scheme
=
targetUrl
.
Scheme
req
.
URL
.
Host
=
targetUrl
.
Host
req
.
Host
=
targetUrl
.
Host
...
...
@@ -69,7 +69,46 @@ func NewApiPluginProxy(ctx string, proxyPath string, targetUrl *url.URL) *httput
// clear cookie headers
req
.
Header
.
Del
(
"Cookie"
)
req
.
Header
.
Del
(
"Set-Cookie"
)
req
.
Header
.
Add
(
"Grafana-Context"
,
ctx
)
//Create a HTTP header with the context in it.
ctxJson
,
err
:=
json
.
Marshal
(
ctx
.
SignedInUser
)
if
err
!=
nil
{
ctx
.
JsonApiErr
(
500
,
"failed to marshal context to json."
,
err
)
return
}
req
.
Header
.
Add
(
"Grafana-Context"
,
string
(
ctxJson
))
// add custom headers defined in the plugin config.
for
_
,
header
:=
range
route
.
Headers
{
var
contentBuf
bytes
.
Buffer
t
,
err
:=
template
.
New
(
"content"
)
.
Parse
(
header
.
Content
)
if
err
!=
nil
{
ctx
.
JsonApiErr
(
500
,
fmt
.
Sprintf
(
"could not parse header content template for header %s."
,
header
.
Name
),
err
)
return
}
jsonData
:=
make
(
map
[
string
]
interface
{})
if
includedInAppId
!=
""
{
//lookup appSettings
query
:=
m
.
GetAppSettingByAppIdQuery
{
OrgId
:
ctx
.
OrgId
,
AppId
:
includedInAppId
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
ctx
.
JsonApiErr
(
500
,
"failed to get AppSettings of includedAppId."
,
err
)
return
}
jsonData
=
query
.
Result
.
JsonData
}
err
=
t
.
Execute
(
&
contentBuf
,
jsonData
)
if
err
!=
nil
{
ctx
.
JsonApiErr
(
500
,
fmt
.
Sprintf
(
"failed to execute header content template for header %s."
,
header
.
Name
),
err
)
return
}
log
.
Debug
(
"Adding header to proxy request. %s: %s"
,
header
.
Name
,
contentBuf
.
String
())
req
.
Header
.
Add
(
header
.
Name
,
contentBuf
.
String
())
}
}
return
&
httputil
.
ReverseProxy
{
Director
:
director
}
...
...
pkg/models/app_settings.go
View file @
f2ce8f26
package
models
import
"time"
import
(
"errors"
"time"
)
var
(
ErrAppSettingNotFound
=
errors
.
New
(
"AppSetting not found"
)
)
type
AppSettings
struct
{
Id
int64
...
...
@@ -33,3 +40,9 @@ type GetAppSettingsQuery struct {
OrgId
int64
Result
[]
*
AppSettings
}
type
GetAppSettingByAppIdQuery
struct
{
AppId
string
OrgId
int64
Result
*
AppSettings
}
pkg/plugins/api_plugin.go
View file @
f2ce8f26
...
...
@@ -7,12 +7,13 @@ import (
)
type
ApiPluginRoute
struct
{
Path
string
`json:"path"`
Method
string
`json:"method"`
ReqSignedIn
bool
`json:"reqSignedIn"`
ReqGrafanaAdmin
bool
`json:"reqGrafanaAdmin"`
ReqRole
models
.
RoleType
`json:"reqRole"`
Url
string
`json:"url"`
Path
string
`json:"path"`
Method
string
`json:"method"`
ReqSignedIn
bool
`json:"reqSignedIn"`
ReqGrafanaAdmin
bool
`json:"reqGrafanaAdmin"`
ReqRole
models
.
RoleType
`json:"reqRole"`
Url
string
`json:"url"`
Headers
[]
ApiPluginHeader
`json:"headers"`
}
type
ApiPlugin
struct
{
...
...
@@ -20,11 +21,18 @@ type ApiPlugin struct {
Routes
[]
*
ApiPluginRoute
`json:"routes"`
}
type
ApiPluginHeader
struct
{
Name
string
`json:"name"`
Content
string
`json:"content"`
}
func
(
app
*
ApiPlugin
)
Load
(
decoder
*
json
.
Decoder
,
pluginDir
string
)
error
{
if
err
:=
decoder
.
Decode
(
&
app
);
err
!=
nil
{
return
err
}
app
.
PluginDir
=
pluginDir
ApiPlugins
[
app
.
Id
]
=
app
return
nil
}
pkg/plugins/app_plugin.go
View file @
f2ce8f26
...
...
@@ -59,6 +59,18 @@ func (app *AppPlugin) Load(decoder *json.Decoder, pluginDir string) error {
}
}
// check if we have child apiPlugins
for
_
,
plugin
:=
range
ApiPlugins
{
if
strings
.
HasPrefix
(
plugin
.
PluginDir
,
app
.
PluginDir
)
{
plugin
.
IncludedInAppId
=
app
.
Id
app
.
Includes
=
append
(
app
.
Includes
,
AppIncludeInfo
{
Name
:
plugin
.
Name
,
Id
:
plugin
.
Id
,
Type
:
plugin
.
Type
,
})
}
}
Apps
[
app
.
Id
]
=
app
return
nil
}
pkg/services/sqlstore/app_settings.go
View file @
f2ce8f26
...
...
@@ -9,6 +9,7 @@ import (
func
init
()
{
bus
.
AddHandler
(
"sql"
,
GetAppSettings
)
bus
.
AddHandler
(
"sql"
,
GetAppSettingByAppId
)
bus
.
AddHandler
(
"sql"
,
UpdateAppSettings
)
}
...
...
@@ -19,6 +20,18 @@ func GetAppSettings(query *m.GetAppSettingsQuery) error {
return
sess
.
Find
(
&
query
.
Result
)
}
func
GetAppSettingByAppId
(
query
*
m
.
GetAppSettingByAppIdQuery
)
error
{
appSetting
:=
m
.
AppSettings
{
OrgId
:
query
.
OrgId
,
AppId
:
query
.
AppId
}
has
,
err
:=
x
.
Get
(
&
appSetting
)
if
err
!=
nil
{
return
err
}
else
if
has
==
false
{
return
m
.
ErrAppSettingNotFound
}
query
.
Result
=
&
appSetting
return
nil
}
func
UpdateAppSettings
(
cmd
*
m
.
UpdateAppSettingsCmd
)
error
{
return
inTransaction2
(
func
(
sess
*
session
)
error
{
var
app
m
.
AppSettings
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment