2016-09-12 19:12:30 +00:00
|
|
|
package restapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2016-09-16 17:05:09 +00:00
|
|
|
"log"
|
2016-09-12 19:12:30 +00:00
|
|
|
"net/http"
|
2016-09-16 17:05:09 +00:00
|
|
|
"strings"
|
2016-09-12 19:12:30 +00:00
|
|
|
|
|
|
|
errors "github.com/go-openapi/errors"
|
|
|
|
runtime "github.com/go-openapi/runtime"
|
|
|
|
middleware "github.com/go-openapi/runtime/middleware"
|
2016-09-19 16:36:48 +00:00
|
|
|
"github.com/go-openapi/swag"
|
2016-09-12 19:12:30 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-09-19 16:36:48 +00:00
|
|
|
"github.com/influxdata/mrfusion"
|
2016-09-16 22:50:49 +00:00
|
|
|
"github.com/influxdata/mrfusion/dist"
|
2016-09-21 22:03:07 +00:00
|
|
|
"github.com/influxdata/mrfusion/handlers"
|
|
|
|
"github.com/influxdata/mrfusion/influx"
|
2016-09-12 23:30:03 +00:00
|
|
|
"github.com/influxdata/mrfusion/mock"
|
2016-09-12 19:12:30 +00:00
|
|
|
"github.com/influxdata/mrfusion/restapi/operations"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This file is safe to edit. Once it exists it will not be overwritten
|
|
|
|
|
|
|
|
//go:generate swagger generate server --target .. --name --spec ../swagger.yaml --with-context
|
|
|
|
|
2016-09-19 16:36:48 +00:00
|
|
|
var devFlags = struct {
|
|
|
|
Develop bool `short:"d" long:"develop" description:"Run server in develop mode."`
|
|
|
|
}{}
|
|
|
|
|
2016-09-21 22:03:07 +00:00
|
|
|
var influxFlags = struct {
|
2016-09-22 21:20:40 +00:00
|
|
|
Server string `short:"s" long:"server" description:"Full URL of InfluxDB server (http://localhost:8086)" env:"INFLUX_HOST"`
|
2016-09-21 22:03:07 +00:00
|
|
|
}{}
|
|
|
|
|
2016-09-12 19:12:30 +00:00
|
|
|
func configureFlags(api *operations.MrFusionAPI) {
|
2016-09-19 16:36:48 +00:00
|
|
|
api.CommandLineOptionsGroups = []swag.CommandLineOptionsGroup{
|
|
|
|
swag.CommandLineOptionsGroup{
|
|
|
|
ShortDescription: "Develop Mode server",
|
|
|
|
LongDescription: "Server will use the ui/build directory directly.",
|
|
|
|
Options: &devFlags,
|
|
|
|
},
|
2016-09-21 22:03:07 +00:00
|
|
|
swag.CommandLineOptionsGroup{
|
|
|
|
ShortDescription: "Default Time Series Backend",
|
|
|
|
LongDescription: "Specify the url of an InfxluDB server",
|
|
|
|
Options: &influxFlags,
|
|
|
|
},
|
2016-09-19 16:36:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assets() mrfusion.Assets {
|
|
|
|
if devFlags.Develop {
|
|
|
|
return &dist.DebugAssets{
|
2016-09-20 02:08:32 +00:00
|
|
|
Dir: "ui/build",
|
|
|
|
Default: "ui/build/index.html",
|
2016-09-19 16:36:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return &dist.BindataAssets{
|
2016-09-20 02:08:32 +00:00
|
|
|
Prefix: "ui/build",
|
|
|
|
Default: "index.html",
|
2016-09-19 16:36:48 +00:00
|
|
|
}
|
2016-09-12 19:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func configureAPI(api *operations.MrFusionAPI) http.Handler {
|
|
|
|
// configure the api here
|
|
|
|
api.ServeError = errors.ServeError
|
|
|
|
|
|
|
|
// Set your custom logger if needed. Default one is log.Printf
|
|
|
|
// Expected interface func(string, ...interface{})
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
// s.api.Logger = log.Printf
|
|
|
|
|
|
|
|
api.JSONConsumer = runtime.JSONConsumer()
|
|
|
|
|
|
|
|
api.JSONProducer = runtime.JSONProducer()
|
|
|
|
|
2016-09-12 23:30:03 +00:00
|
|
|
mockHandler := mock.NewHandler()
|
2016-09-12 23:12:31 +00:00
|
|
|
|
2016-09-26 23:44:45 +00:00
|
|
|
api.DeleteSourcesIDUsersUserIDExplorationsExplorationIDHandler = operations.DeleteSourcesIDUsersUserIDExplorationsExplorationIDHandlerFunc(mockHandler.DeleteExploration)
|
|
|
|
api.GetSourcesIDUsersUserIDExplorationsExplorationIDHandler = operations.GetSourcesIDUsersUserIDExplorationsExplorationIDHandlerFunc(mockHandler.Exploration)
|
|
|
|
api.GetSourcesIDUsersUserIDExplorationsHandler = operations.GetSourcesIDUsersUserIDExplorationsHandlerFunc(mockHandler.Explorations)
|
|
|
|
api.PatchSourcesIDUsersUserIDExplorationsExplorationIDHandler = operations.PatchSourcesIDUsersUserIDExplorationsExplorationIDHandlerFunc(mockHandler.UpdateExploration)
|
|
|
|
api.PostSourcesIDUsersUserIDExplorationsHandler = operations.PostSourcesIDUsersUserIDExplorationsHandlerFunc(mockHandler.NewExploration)
|
|
|
|
|
2016-09-12 19:12:30 +00:00
|
|
|
api.DeleteDashboardsIDHandler = operations.DeleteDashboardsIDHandlerFunc(func(ctx context.Context, params operations.DeleteDashboardsIDParams) middleware.Responder {
|
|
|
|
return middleware.NotImplemented("operation .DeleteDashboardsID has not yet been implemented")
|
|
|
|
})
|
|
|
|
api.DeleteSourcesIDHandler = operations.DeleteSourcesIDHandlerFunc(func(ctx context.Context, params operations.DeleteSourcesIDParams) middleware.Responder {
|
|
|
|
return middleware.NotImplemented("operation .DeleteSourcesID has not yet been implemented")
|
|
|
|
})
|
|
|
|
api.DeleteSourcesIDRolesRoleIDHandler = operations.DeleteSourcesIDRolesRoleIDHandlerFunc(func(ctx context.Context, params operations.DeleteSourcesIDRolesRoleIDParams) middleware.Responder {
|
|
|
|
return middleware.NotImplemented("operation .DeleteSourcesIDRolesRoleID has not yet been implemented")
|
|
|
|
})
|
2016-09-26 23:44:45 +00:00
|
|
|
|
2016-09-12 19:12:30 +00:00
|
|
|
api.DeleteSourcesIDUsersUserIDHandler = operations.DeleteSourcesIDUsersUserIDHandlerFunc(func(ctx context.Context, params operations.DeleteSourcesIDUsersUserIDParams) middleware.Responder {
|
|
|
|
return middleware.NotImplemented("operation .DeleteSourcesIDUsersUserID has not yet been implemented")
|
|
|
|
})
|
|
|
|
api.GetHandler = operations.GetHandlerFunc(func(ctx context.Context, params operations.GetParams) middleware.Responder {
|
|
|
|
return middleware.NotImplemented("operation .Get has not yet been implemented")
|
|
|
|
})
|
|
|
|
api.GetDashboardsHandler = operations.GetDashboardsHandlerFunc(func(ctx context.Context, params operations.GetDashboardsParams) middleware.Responder {
|
|
|
|
return middleware.NotImplemented("operation .GetDashboards has not yet been implemented")
|
|
|
|
})
|
|
|
|
api.GetDashboardsIDHandler = operations.GetDashboardsIDHandlerFunc(func(ctx context.Context, params operations.GetDashboardsIDParams) middleware.Responder {
|
|
|
|
return middleware.NotImplemented("operation .GetDashboardsID has not yet been implemented")
|
|
|
|
})
|
2016-09-20 02:08:32 +00:00
|
|
|
|
|
|
|
api.GetSourcesHandler = operations.GetSourcesHandlerFunc(mockHandler.Sources)
|
|
|
|
api.GetSourcesIDHandler = operations.GetSourcesIDHandlerFunc(mockHandler.SourcesID)
|
|
|
|
|
2016-09-12 19:12:30 +00:00
|
|
|
api.GetSourcesIDPermissionsHandler = operations.GetSourcesIDPermissionsHandlerFunc(func(ctx context.Context, params operations.GetSourcesIDPermissionsParams) middleware.Responder {
|
|
|
|
return middleware.NotImplemented("operation .GetSourcesIDPermissions has not yet been implemented")
|
|
|
|
})
|
|
|
|
api.GetSourcesIDRolesHandler = operations.GetSourcesIDRolesHandlerFunc(func(ctx context.Context, params operations.GetSourcesIDRolesParams) middleware.Responder {
|
|
|
|
return middleware.NotImplemented("operation .GetSourcesIDRoles has not yet been implemented")
|
|
|
|
})
|
|
|
|
api.GetSourcesIDRolesRoleIDHandler = operations.GetSourcesIDRolesRoleIDHandlerFunc(func(ctx context.Context, params operations.GetSourcesIDRolesRoleIDParams) middleware.Responder {
|
|
|
|
return middleware.NotImplemented("operation .GetSourcesIDRolesRoleID has not yet been implemented")
|
|
|
|
})
|
2016-09-12 23:12:31 +00:00
|
|
|
|
2016-09-12 19:12:30 +00:00
|
|
|
api.GetSourcesIDUsersHandler = operations.GetSourcesIDUsersHandlerFunc(func(ctx context.Context, params operations.GetSourcesIDUsersParams) middleware.Responder {
|
|
|
|
return middleware.NotImplemented("operation .GetSourcesIDUsers has not yet been implemented")
|
|
|
|
})
|
|
|
|
api.GetSourcesIDUsersUserIDHandler = operations.GetSourcesIDUsersUserIDHandlerFunc(func(ctx context.Context, params operations.GetSourcesIDUsersUserIDParams) middleware.Responder {
|
|
|
|
return middleware.NotImplemented("operation .GetSourcesIDUsersUserID has not yet been implemented")
|
|
|
|
})
|
2016-09-12 23:12:31 +00:00
|
|
|
|
2016-09-12 19:12:30 +00:00
|
|
|
api.PatchSourcesIDHandler = operations.PatchSourcesIDHandlerFunc(func(ctx context.Context, params operations.PatchSourcesIDParams) middleware.Responder {
|
|
|
|
return middleware.NotImplemented("operation .PatchSourcesID has not yet been implemented")
|
|
|
|
})
|
|
|
|
api.PatchSourcesIDRolesRoleIDHandler = operations.PatchSourcesIDRolesRoleIDHandlerFunc(func(ctx context.Context, params operations.PatchSourcesIDRolesRoleIDParams) middleware.Responder {
|
|
|
|
return middleware.NotImplemented("operation .PatchSourcesIDRolesRoleID has not yet been implemented")
|
|
|
|
})
|
2016-09-26 23:44:45 +00:00
|
|
|
|
2016-09-12 19:12:30 +00:00
|
|
|
api.PatchSourcesIDUsersUserIDHandler = operations.PatchSourcesIDUsersUserIDHandlerFunc(func(ctx context.Context, params operations.PatchSourcesIDUsersUserIDParams) middleware.Responder {
|
|
|
|
return middleware.NotImplemented("operation .PatchSourcesIDUsersUserID has not yet been implemented")
|
|
|
|
})
|
|
|
|
api.PostDashboardsHandler = operations.PostDashboardsHandlerFunc(func(ctx context.Context, params operations.PostDashboardsParams) middleware.Responder {
|
|
|
|
return middleware.NotImplemented("operation .PostDashboards has not yet been implemented")
|
|
|
|
})
|
2016-09-23 21:42:54 +00:00
|
|
|
api.PostSourcesHandler = operations.PostSourcesHandlerFunc(mockHandler.NewSource)
|
2016-09-12 23:30:03 +00:00
|
|
|
|
2016-09-21 22:03:07 +00:00
|
|
|
if len(influxFlags.Server) > 0 {
|
|
|
|
c, err := influx.NewClient(influxFlags.Server)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
h := handlers.InfluxProxy{
|
|
|
|
TimeSeries: c,
|
|
|
|
}
|
|
|
|
api.PostSourcesIDProxyHandler = operations.PostSourcesIDProxyHandlerFunc(h.Proxy)
|
|
|
|
} else {
|
|
|
|
api.PostSourcesIDProxyHandler = operations.PostSourcesIDProxyHandlerFunc(mockHandler.Proxy)
|
|
|
|
}
|
2016-09-12 23:30:03 +00:00
|
|
|
|
2016-09-12 19:12:30 +00:00
|
|
|
api.PostSourcesIDRolesHandler = operations.PostSourcesIDRolesHandlerFunc(func(ctx context.Context, params operations.PostSourcesIDRolesParams) middleware.Responder {
|
|
|
|
return middleware.NotImplemented("operation .PostSourcesIDRoles has not yet been implemented")
|
|
|
|
})
|
|
|
|
api.PostSourcesIDUsersHandler = operations.PostSourcesIDUsersHandlerFunc(func(ctx context.Context, params operations.PostSourcesIDUsersParams) middleware.Responder {
|
|
|
|
return middleware.NotImplemented("operation .PostSourcesIDUsers has not yet been implemented")
|
|
|
|
})
|
2016-09-26 23:44:45 +00:00
|
|
|
|
2016-09-12 19:12:30 +00:00
|
|
|
api.PutDashboardsIDHandler = operations.PutDashboardsIDHandlerFunc(func(ctx context.Context, params operations.PutDashboardsIDParams) middleware.Responder {
|
|
|
|
return middleware.NotImplemented("operation .PutDashboardsID has not yet been implemented")
|
|
|
|
})
|
|
|
|
|
2016-09-15 16:28:51 +00:00
|
|
|
api.GetSourcesIDMonitoredHandler = operations.GetSourcesIDMonitoredHandlerFunc(mockHandler.MonitoredServices)
|
|
|
|
|
2016-09-12 19:12:30 +00:00
|
|
|
api.ServerShutdown = func() {}
|
|
|
|
|
2016-09-16 17:05:09 +00:00
|
|
|
handler := setupGlobalMiddleware(api.Serve(setupMiddlewares))
|
|
|
|
return handler
|
2016-09-12 19:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The TLS configuration before HTTPS server starts.
|
|
|
|
func configureTLS(tlsConfig *tls.Config) {
|
|
|
|
// Make all necessary changes to the TLS configuration here.
|
|
|
|
}
|
|
|
|
|
|
|
|
// The middleware configuration is for the handler executors. These do not apply to the swagger.json document.
|
|
|
|
// The middleware executes after routing but before authentication, binding and validation
|
|
|
|
func setupMiddlewares(handler http.Handler) http.Handler {
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
|
|
|
|
// The middleware configuration happens before anything, this middleware also applies to serving the swagger.json document.
|
|
|
|
// So this is a good place to plug in a panic handling middleware, logging and metrics
|
|
|
|
func setupGlobalMiddleware(handler http.Handler) http.Handler {
|
2016-09-16 17:05:09 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
|
2016-09-16 18:47:58 +00:00
|
|
|
if strings.Contains(r.URL.Path, "/chronograf/v1") {
|
|
|
|
handler.ServeHTTP(w, r)
|
2016-09-16 17:05:09 +00:00
|
|
|
return
|
2016-09-19 21:16:33 +00:00
|
|
|
} else if r.URL.Path == "//" {
|
|
|
|
http.Redirect(w, r, "/index.html", http.StatusFound)
|
|
|
|
} else {
|
2016-09-19 20:41:51 +00:00
|
|
|
assets().Handler().ServeHTTP(w, r)
|
2016-09-16 17:05:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
2016-09-12 19:12:30 +00:00
|
|
|
}
|