1
0
Fork 0
mirror of https://github.com/Luzifer/mondash.git synced 2024-11-09 16:10:01 +00:00

Migrated to Gorilla mux instead of martini

This commit is contained in:
Knut Ahlers 2015-07-06 22:23:46 +02:00
parent 3cdda0175f
commit 6d5375e55a
3 changed files with 28 additions and 20 deletions

View file

@ -12,6 +12,8 @@ type Config struct {
BaseURL string
APIToken string
Listen string
S3 struct {
Bucket string
}
@ -27,6 +29,7 @@ func Load() *Config {
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.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
pflag.StringVar(&cfg.S3.Bucket, "s3Bucket", os.Getenv("S3Bucket"), "Bucket to use for S3 storage")

31
main.go
View file

@ -10,7 +10,7 @@ import (
"github.com/Luzifer/mondash/config"
"github.com/Luzifer/mondash/storage"
"github.com/flosch/pongo2"
"github.com/go-martini/martini"
"github.com/gorilla/mux"
_ "github.com/flosch/pongo2-addons"
)
@ -31,25 +31,26 @@ func main() {
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
m.Use(martini.Static("assets", martini.StaticOptions{Prefix: "/assets"}))
r.HandleFunc("/{dashid}/{metricid}", handlePutMetric).
Methods("PUT")
// Real handlers
m.Get("/", handleRedirectWelcome)
m.Get("/create", handleCreateRandomDashboard)
m.Get("/:dashid", handleDisplayDashboard)
m.Put("/:dashid/:metricid", handlePutMetric)
m.Delete("/:dashid", handleDeleteDashboard)
m.Delete("/:dashid/:metricid", handleDeleteMetric)
r.HandleFunc("/{dashid}", handleDeleteDashboard).
Methods("DELETE")
r.HandleFunc("/{dashid}/{metricid}", handleDeleteMetric).
Methods("DELETE")
go runWelcomePage(cfg)
// GO!
m.Run()
http.Handle("/", r)
http.ListenAndServe(cfg.Listen, nil)
}
func generateAPIKey() string {

View file

@ -10,7 +10,7 @@ import (
"time"
"github.com/flosch/pongo2"
"github.com/go-martini/martini"
"github.com/gorilla/mux"
)
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)
}
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)
if err != nil {
dash = &dashboard{APIKey: generateAPIKey(), Metrics: dashboardMetrics{}}
@ -51,7 +52,8 @@ func handleDisplayDashboard(params martini.Params, res http.ResponseWriter) {
}, 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)
if err != nil {
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)
}
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)
if err != nil {
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)
}
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)
if err != nil {
dash = &dashboard{APIKey: req.Header.Get("Authorization"), Metrics: dashboardMetrics{}, DashboardID: params["dashid"]}