mirror of
https://github.com/Luzifer/mondash.git
synced 2024-12-23 04:21:18 +00:00
Migrated to Gorilla mux instead of martini
This commit is contained in:
parent
3cdda0175f
commit
6d5375e55a
3 changed files with 28 additions and 20 deletions
|
@ -12,6 +12,8 @@ type Config struct {
|
||||||
BaseURL string
|
BaseURL string
|
||||||
APIToken string
|
APIToken string
|
||||||
|
|
||||||
|
Listen string
|
||||||
|
|
||||||
S3 struct {
|
S3 struct {
|
||||||
Bucket string
|
Bucket string
|
||||||
}
|
}
|
||||||
|
@ -27,6 +29,7 @@ func Load() *Config {
|
||||||
pflag.StringVar(&cfg.Storage, "storage", "s3", "Storage engine to use (s3, file)")
|
pflag.StringVar(&cfg.Storage, "storage", "s3", "Storage engine to use (s3, file)")
|
||||||
pflag.StringVar(&cfg.BaseURL, "baseurl", os.Getenv("BASE_URL"), "The Base-URL the application is running on for example https://mondash.org")
|
pflag.StringVar(&cfg.BaseURL, "baseurl", os.Getenv("BASE_URL"), "The Base-URL the application is running on for example https://mondash.org")
|
||||||
pflag.StringVar(&cfg.APIToken, "api-token", os.Getenv("API_TOKEN"), "API Token used for the /welcome dashboard (you can choose your own)")
|
pflag.StringVar(&cfg.APIToken, "api-token", os.Getenv("API_TOKEN"), "API Token used for the /welcome dashboard (you can choose your own)")
|
||||||
|
pflag.StringVar(&cfg.Listen, "listen", ":3000", "Address to listen on")
|
||||||
|
|
||||||
// S3
|
// S3
|
||||||
pflag.StringVar(&cfg.S3.Bucket, "s3Bucket", os.Getenv("S3Bucket"), "Bucket to use for S3 storage")
|
pflag.StringVar(&cfg.S3.Bucket, "s3Bucket", os.Getenv("S3Bucket"), "Bucket to use for S3 storage")
|
||||||
|
|
31
main.go
31
main.go
|
@ -10,7 +10,7 @@ import (
|
||||||
"github.com/Luzifer/mondash/config"
|
"github.com/Luzifer/mondash/config"
|
||||||
"github.com/Luzifer/mondash/storage"
|
"github.com/Luzifer/mondash/storage"
|
||||||
"github.com/flosch/pongo2"
|
"github.com/flosch/pongo2"
|
||||||
"github.com/go-martini/martini"
|
"github.com/gorilla/mux"
|
||||||
|
|
||||||
_ "github.com/flosch/pongo2-addons"
|
_ "github.com/flosch/pongo2-addons"
|
||||||
)
|
)
|
||||||
|
@ -31,25 +31,26 @@ func main() {
|
||||||
fmt.Printf("An error occurred while loading the storage handler: %s", err)
|
fmt.Printf("An error occurred while loading the storage handler: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
m := martini.Classic()
|
r := mux.NewRouter()
|
||||||
|
r.HandleFunc("/", handleRedirectWelcome).
|
||||||
|
Methods("GET")
|
||||||
|
r.HandleFunc("/create", handleCreateRandomDashboard).
|
||||||
|
Methods("GET")
|
||||||
|
r.HandleFunc("/{dashid}", handleDisplayDashboard).
|
||||||
|
Methods("GET")
|
||||||
|
|
||||||
// Assets are in assets folder
|
r.HandleFunc("/{dashid}/{metricid}", handlePutMetric).
|
||||||
m.Use(martini.Static("assets", martini.StaticOptions{Prefix: "/assets"}))
|
Methods("PUT")
|
||||||
|
|
||||||
// Real handlers
|
r.HandleFunc("/{dashid}", handleDeleteDashboard).
|
||||||
m.Get("/", handleRedirectWelcome)
|
Methods("DELETE")
|
||||||
m.Get("/create", handleCreateRandomDashboard)
|
r.HandleFunc("/{dashid}/{metricid}", handleDeleteMetric).
|
||||||
m.Get("/:dashid", handleDisplayDashboard)
|
Methods("DELETE")
|
||||||
|
|
||||||
m.Put("/:dashid/:metricid", handlePutMetric)
|
|
||||||
|
|
||||||
m.Delete("/:dashid", handleDeleteDashboard)
|
|
||||||
m.Delete("/:dashid/:metricid", handleDeleteMetric)
|
|
||||||
|
|
||||||
go runWelcomePage(cfg)
|
go runWelcomePage(cfg)
|
||||||
|
|
||||||
// GO!
|
http.Handle("/", r)
|
||||||
m.Run()
|
http.ListenAndServe(cfg.Listen, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateAPIKey() string {
|
func generateAPIKey() string {
|
||||||
|
|
|
@ -10,7 +10,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/flosch/pongo2"
|
"github.com/flosch/pongo2"
|
||||||
"github.com/go-martini/martini"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
func handleRedirectWelcome(res http.ResponseWriter, req *http.Request) {
|
func handleRedirectWelcome(res http.ResponseWriter, req *http.Request) {
|
||||||
|
@ -28,7 +28,8 @@ func handleCreateRandomDashboard(res http.ResponseWriter, req *http.Request) {
|
||||||
http.Redirect(res, req, fmt.Sprintf("/%s", urlProposal), http.StatusTemporaryRedirect)
|
http.Redirect(res, req, fmt.Sprintf("/%s", urlProposal), http.StatusTemporaryRedirect)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleDisplayDashboard(params martini.Params, res http.ResponseWriter) {
|
func handleDisplayDashboard(res http.ResponseWriter, req *http.Request) {
|
||||||
|
params := mux.Vars(req)
|
||||||
dash, err := loadDashboard(params["dashid"], store)
|
dash, err := loadDashboard(params["dashid"], store)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dash = &dashboard{APIKey: generateAPIKey(), Metrics: dashboardMetrics{}}
|
dash = &dashboard{APIKey: generateAPIKey(), Metrics: dashboardMetrics{}}
|
||||||
|
@ -51,7 +52,8 @@ func handleDisplayDashboard(params martini.Params, res http.ResponseWriter) {
|
||||||
}, res)
|
}, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleDeleteDashboard(params martini.Params, req *http.Request, res http.ResponseWriter) {
|
func handleDeleteDashboard(res http.ResponseWriter, req *http.Request) {
|
||||||
|
params := mux.Vars(req)
|
||||||
dash, err := loadDashboard(params["dashid"], store)
|
dash, err := loadDashboard(params["dashid"], store)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(res, "This dashboard does not exist.", http.StatusInternalServerError)
|
http.Error(res, "This dashboard does not exist.", http.StatusInternalServerError)
|
||||||
|
@ -67,7 +69,8 @@ func handleDeleteDashboard(params martini.Params, req *http.Request, res http.Re
|
||||||
http.Error(res, "OK", http.StatusOK)
|
http.Error(res, "OK", http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handlePutMetric(params martini.Params, req *http.Request, res http.ResponseWriter) {
|
func handlePutMetric(res http.ResponseWriter, req *http.Request) {
|
||||||
|
params := mux.Vars(req)
|
||||||
body, err := ioutil.ReadAll(req.Body)
|
body, err := ioutil.ReadAll(req.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(res, "Internal Server Error", http.StatusInternalServerError)
|
http.Error(res, "Internal Server Error", http.StatusInternalServerError)
|
||||||
|
@ -122,7 +125,8 @@ func handlePutMetric(params martini.Params, req *http.Request, res http.Response
|
||||||
http.Error(res, "OK", http.StatusOK)
|
http.Error(res, "OK", http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleDeleteMetric(params martini.Params, req *http.Request, res http.ResponseWriter) {
|
func handleDeleteMetric(res http.ResponseWriter, req *http.Request) {
|
||||||
|
params := mux.Vars(req)
|
||||||
dash, err := loadDashboard(params["dashid"], store)
|
dash, err := loadDashboard(params["dashid"], store)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dash = &dashboard{APIKey: req.Header.Get("Authorization"), Metrics: dashboardMetrics{}, DashboardID: params["dashid"]}
|
dash = &dashboard{APIKey: req.Header.Get("Authorization"), Metrics: dashboardMetrics{}, DashboardID: params["dashid"]}
|
||||||
|
|
Loading…
Reference in a new issue