The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
grafana/pkg/api/api.go

80 lines
2.1 KiB

package api
import (
"html/template"
log "github.com/alecthomas/log4go"
"github.com/gin-gonic/gin"
12 years ago
"github.com/gorilla/sessions"
"github.com/torkelo/grafana-pro/pkg/components"
11 years ago
"github.com/torkelo/grafana-pro/pkg/configuration"
"github.com/torkelo/grafana-pro/pkg/models"
"github.com/torkelo/grafana-pro/pkg/stores"
)
type HttpServer struct {
port string
shutdown chan bool
store stores.Store
renderer *components.PhantomRenderer
router *gin.Engine
11 years ago
cfg *configuration.Cfg
}
12 years ago
var sessionStore = sessions.NewCookieStore([]byte("something-very-secret"))
11 years ago
func NewHttpServer(cfg *configuration.Cfg, store stores.Store) *HttpServer {
self := &HttpServer{}
11 years ago
self.cfg = cfg
self.port = cfg.Http.Port
self.store = store
self.renderer = &components.PhantomRenderer{ImagesDir: "data/png", PhantomDir: "_vendor/phantomjs"}
return self
}
func (self *HttpServer) ListenAndServe() {
log.Info("Starting Http Listener on port %v", self.port)
defer func() { self.shutdown <- true }()
self.router = gin.Default()
self.router.Use(CacheHeadersMiddleware())
12 years ago
self.router.Static("/public", "./public")
self.router.Static("/app", "./public/app")
self.router.Static("/img", "./public/img")
// register & parse templates
templates := template.New("templates")
templates.Delims("[[", "]]")
templates.ParseFiles("./views/index.html")
self.router.SetHTMLTemplate(templates)
for _, fn := range routeHandlers {
fn(self)
}
12 years ago
// register default route
self.router.GET("/", self.auth(), self.index)
self.router.GET("/dashboard/*_", self.auth(), self.index)
self.router.GET("/admin/*_", self.auth(), self.index)
self.router.GET("/account/*_", self.auth(), self.index)
self.router.Run(":" + self.port)
}
func (self *HttpServer) index(c *gin.Context) {
viewModel := &IndexDto{}
userAccount, _ := c.Get("userAccount")
11 years ago
account, _ := userAccount.(*models.Account)
initCurrentUserDto(&viewModel.User, account)
c.HTML(200, "index.html", viewModel)
}
12 years ago
func CacheHeadersMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Add("Cache-Control", "max-age=0, public, must-revalidate, proxy-revalidate")
}
}