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
a2a0e039
Commit
a2a0e039
authored
Dec 18, 2014
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Work on data source proxying, #6
parent
d69258e2
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
98 additions
and
3 deletions
+98
-3
pkg/api/api.go
+4
-1
pkg/api/api_config.go
+5
-1
pkg/api/api_dataproxy.go
+63
-0
pkg/models/datasource.go
+15
-1
pkg/stores/sqlstore/sqlstore_datasource.go
+11
-0
No files found.
pkg/api/api.go
View file @
a2a0e039
...
...
@@ -25,13 +25,16 @@ func Register(m *macaron.Macaron) {
m
.
Post
(
"/api/account/using/:id"
,
auth
,
SetUsingAccount
)
m
.
Get
(
"/api/account/others"
,
auth
,
GetOtherAccounts
)
// datasources
// data
sources
m
.
Get
(
"/admin/datasources/"
,
auth
,
Index
)
m
.
Get
(
"/api/admin/datasources/list"
,
auth
,
GetDataSources
)
m
.
Put
(
"/api/admin/datasources"
,
auth
,
AddDataSource
)
m
.
Post
(
"/api/admin/datasources"
,
auth
,
UpdateDataSource
)
m
.
Delete
(
"/api/admin/datasources/:id"
,
auth
,
DeleteDataSource
)
// data source proxy
m
.
Any
(
"/api/datasources/proxy/:name/*"
,
auth
,
ProxyDataSourceRequest
)
// user register
m
.
Get
(
"/register/*_"
,
Index
)
m
.
Post
(
"/api/account"
,
CreateAccount
)
...
...
pkg/api/api_config.go
View file @
a2a0e039
...
...
@@ -25,9 +25,13 @@ func renderConfig(data *configJsTmplModel) string {
datasources
:=
make
(
map
[
string
]
interface
{})
for
_
,
ds
:=
range
data
.
DataSources
{
url
:=
ds
.
Url
if
ds
.
Access
==
m
.
DS_ACCESS_PROXY
{
url
=
"/api/datasources/proxy/"
+
ds
.
Name
}
datasources
[
ds
.
Name
]
=
map
[
string
]
interface
{}{
"type"
:
ds
.
Type
,
"url"
:
ds
.
U
rl
,
"url"
:
u
rl
,
}
}
...
...
pkg/api/api_dataproxy.go
0 → 100644
View file @
a2a0e039
package
api
import
(
"net/http"
"net/http/httputil"
"net/url"
"strings"
"github.com/torkelo/grafana-pro/pkg/bus"
"github.com/torkelo/grafana-pro/pkg/log"
"github.com/torkelo/grafana-pro/pkg/middleware"
m
"github.com/torkelo/grafana-pro/pkg/models"
)
func
singleJoiningSlash
(
a
,
b
string
)
string
{
aslash
:=
strings
.
HasSuffix
(
a
,
"/"
)
bslash
:=
strings
.
HasPrefix
(
b
,
"/"
)
switch
{
case
aslash
&&
bslash
:
return
a
+
b
[
1
:
]
case
!
aslash
&&
!
bslash
:
return
a
+
"/"
+
b
}
return
a
+
b
}
func
NewReverseProxy
(
target
*
url
.
URL
,
proxyPath
string
)
*
httputil
.
ReverseProxy
{
targetQuery
:=
target
.
RawQuery
director
:=
func
(
req
*
http
.
Request
)
{
req
.
URL
.
Scheme
=
target
.
Scheme
req
.
URL
.
Host
=
target
.
Host
req
.
URL
.
Path
=
singleJoiningSlash
(
target
.
Path
,
proxyPath
)
if
targetQuery
==
""
||
req
.
URL
.
RawQuery
==
""
{
req
.
URL
.
RawQuery
=
targetQuery
+
req
.
URL
.
RawQuery
}
else
{
req
.
URL
.
RawQuery
=
targetQuery
+
"&"
+
req
.
URL
.
RawQuery
}
log
.
Info
(
"Proxy: %v"
,
req
.
URL
.
Path
)
}
return
&
httputil
.
ReverseProxy
{
Director
:
director
}
}
// TODO: need to cache datasources
func
ProxyDataSourceRequest
(
c
*
middleware
.
Context
)
{
name
:=
c
.
Params
(
":name"
)
query
:=
m
.
GetDataSourceByNameQuery
{
Name
:
name
,
AccountId
:
c
.
GetAccountId
(),
}
err
:=
bus
.
Dispatch
(
&
query
)
if
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Unable to load datasource meta data"
,
err
)
}
proxyPath
:=
c
.
Params
(
"*"
)
url
,
_
:=
url
.
Parse
(
query
.
Result
.
Url
)
proxy
:=
NewReverseProxy
(
url
,
proxyPath
)
proxy
.
ServeHTTP
(
c
.
RW
(),
c
.
Req
.
Request
)
}
pkg/models/datasource.go
View file @
a2a0e039
package
models
import
"time"
import
(
"errors"
"time"
)
const
(
DS_GRAPHITE
=
"graphite"
...
...
@@ -10,6 +13,11 @@ const (
DS_ACCESS_PROXY
=
"proxy"
)
// Typed errors
var
(
ErrDataSourceNotFound
=
errors
.
New
(
"Data source not found"
)
)
type
DsType
string
type
DsAccess
string
...
...
@@ -34,6 +42,12 @@ type GetDataSourcesQuery struct {
Result
[]
*
DataSource
}
type
GetDataSourceByNameQuery
struct
{
Name
string
AccountId
int64
Result
DataSource
}
type
AddDataSourceCommand
struct
{
AccountId
int64
Name
string
...
...
pkg/stores/sqlstore/sqlstore_datasource.go
View file @
a2a0e039
...
...
@@ -14,6 +14,17 @@ func init() {
bus
.
AddHandler
(
"sql"
,
AddDataSource
)
bus
.
AddHandler
(
"sql"
,
DeleteDataSource
)
bus
.
AddHandler
(
"sql"
,
UpdateDataSource
)
bus
.
AddHandler
(
"sql"
,
GetDataSourceByName
)
}
func
GetDataSourceByName
(
query
*
m
.
GetDataSourceByNameQuery
)
error
{
sess
:=
x
.
Limit
(
100
,
0
)
.
Where
(
"account_id=? AND name=?"
,
query
.
AccountId
,
query
.
Name
)
has
,
err
:=
sess
.
Get
(
&
query
.
Result
)
if
!
has
{
return
m
.
ErrDataSourceNotFound
}
return
err
}
func
GetDataSources
(
query
*
m
.
GetDataSourcesQuery
)
error
{
...
...
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