summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2018-07-08 21:12:37 -0700
committerWolfy-J <[email protected]>2018-07-08 21:12:37 -0700
commitc78c38d07ecc7a9fb9ac089c2aee18d8e51db601 (patch)
tree97421672d0972fe9dcb23c55302f5960dccdce80 /cmd
parentca92fcaf9b2290b3bf2124eed6b0db6860010712 (diff)
mod updated
Diffstat (limited to 'cmd')
-rw-r--r--cmd/rr/.rr.yaml56
-rw-r--r--cmd/rr/LICENSE21
-rw-r--r--cmd/rr/cmd/root.go128
-rw-r--r--cmd/rr/cmd/serve.go49
-rw-r--r--cmd/rr/cmd/version.go11
-rw-r--r--cmd/rr/debug/debugger.go117
-rw-r--r--cmd/rr/http/reset.go61
-rw-r--r--cmd/rr/http/workers.go165
-rw-r--r--cmd/rr/main.go67
-rw-r--r--cmd/rr/utils/cprint.go28
10 files changed, 703 insertions, 0 deletions
diff --git a/cmd/rr/.rr.yaml b/cmd/rr/.rr.yaml
new file mode 100644
index 00000000..5ea6b345
--- /dev/null
+++ b/cmd/rr/.rr.yaml
@@ -0,0 +1,56 @@
+# rpc bus allows php application and external clients to talk to rr services.
+rpc:
+ # enable rpc server
+ enable: true
+
+ # rpc connection DSN. Supported TCP and Unix sockets.
+ listen: tcp://127.0.0.1:6001
+
+# http service configuration.
+http:
+ # set to false to disable http server.
+ enable: true
+
+ # http host to listen.
+ address: 0.0.0.0:8080
+
+ # max POST request size, including file uploads in MB.
+ maxRequest: 200
+
+ # file upload configuration.
+ uploads:
+ # list of file extensions which are forbidden for uploading.
+ forbid: [".php", ".exe", ".bat"]
+
+ # http worker pool configuration.
+ workers:
+ # php worker command.
+ command: "php psr-worker.php pipes"
+
+ # connection method (pipes, tcp://:9000, unix://socket.unix).
+ relay: "pipes"
+
+ # worker pool configuration.
+ pool:
+ # number of workers to be serving.
+ numWorkers: 4
+
+ # maximum jobs per worker, 0 - unlimited.
+ maxJobs: 0
+
+ # for how long worker is allowed to be bootstrapped.
+ allocateTimeout: 60
+
+ # amount of time given to worker to gracefully destruct itself.
+ destroyTimeout: 60
+
+# static file serving.
+static:
+ # serve http static files
+ enable: true
+
+ # root directory for static file (http would not serve .php and .htaccess files).
+ dir: "public"
+
+ # list of extensions for forbid for serving.
+ forbid: [".php", ".htaccess"] \ No newline at end of file
diff --git a/cmd/rr/LICENSE b/cmd/rr/LICENSE
new file mode 100644
index 00000000..efb98c87
--- /dev/null
+++ b/cmd/rr/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2018 SpiralScout
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE. \ No newline at end of file
diff --git a/cmd/rr/cmd/root.go b/cmd/rr/cmd/root.go
new file mode 100644
index 00000000..086f518c
--- /dev/null
+++ b/cmd/rr/cmd/root.go
@@ -0,0 +1,128 @@
+// Copyright (c) 2018 SpiralScout
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+
+package cmd
+
+import (
+ "github.com/sirupsen/logrus"
+ "github.com/spf13/cobra"
+ "github.com/spf13/viper"
+ "github.com/spiral/roadrunner/cmd/rr/utils"
+ "github.com/spiral/roadrunner/service"
+ "os"
+)
+
+// Service bus for all the commands.
+var (
+ cfgFile string
+ verbose bool
+
+ // Logger - shared logger.
+ Logger = logrus.New()
+
+ // Container - shared service bus.
+ Container = service.NewContainer(Logger)
+
+ // CLI is application endpoint.
+ CLI = &cobra.Command{
+ Use: "rr",
+ SilenceErrors: true,
+ SilenceUsage: true,
+ Short: utils.Sprintf(
+ "<green>RoadRunner, PHP Application Server:</reset>\nVersion: <yellow+hb>%s</reset>, %s",
+ Version,
+ BuildTime,
+ ),
+ }
+)
+
+// ViperWrapper provides interface bridge between v configs and service.Config.
+type ViperWrapper struct {
+ v *viper.Viper
+}
+
+// get nested config section (sub-map), returns nil if section not found.
+func (w *ViperWrapper) Get(key string) service.Config {
+ sub := w.v.Sub(key)
+ if sub == nil {
+ return nil
+ }
+
+ return &ViperWrapper{sub}
+}
+
+// Unmarshal unmarshal config data into given struct.
+func (w *ViperWrapper) Unmarshal(out interface{}) error {
+ return w.v.Unmarshal(out)
+}
+
+// Execute adds all child commands to the CLI command and sets flags appropriately.
+// This is called by main.main(). It only needs to happen once to the CLI.
+func Execute() {
+ if err := CLI.Execute(); err != nil {
+ utils.Printf("<red+hb>Error:</reset> <red>%s</reset>\n", err)
+ os.Exit(1)
+ }
+}
+
+func init() {
+ CLI.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")
+ CLI.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is .rr.yaml)")
+
+ cobra.OnInitialize(func() {
+ if verbose {
+ Logger.SetLevel(logrus.DebugLevel)
+ }
+
+ if cfg := initConfig(cfgFile, []string{"."}, ".rr"); cfg != nil {
+ if err := Container.Init(cfg); err != nil {
+ utils.Printf("<red+hb>Error:</reset> <red>%s</reset>\n", err)
+ os.Exit(1)
+ }
+ }
+ })
+}
+
+func initConfig(cfgFile string, path []string, name string) service.Config {
+ cfg := viper.New()
+
+ if cfgFile != "" {
+ // Use cfg file from the flag.
+ cfg.SetConfigFile(cfgFile)
+ } else {
+ // automatic location
+ for _, p := range path {
+ cfg.AddConfigPath(p)
+ }
+
+ cfg.SetConfigName(name)
+ }
+
+ // read in environment variables that match
+ cfg.AutomaticEnv()
+
+ // If a cfg file is found, read it in.
+ if err := cfg.ReadInConfig(); err != nil {
+ Logger.Warnf("config: %s", err)
+ return nil
+ }
+
+ return &ViperWrapper{cfg}
+}
diff --git a/cmd/rr/cmd/serve.go b/cmd/rr/cmd/serve.go
new file mode 100644
index 00000000..8028395a
--- /dev/null
+++ b/cmd/rr/cmd/serve.go
@@ -0,0 +1,49 @@
+// Copyright (c) 2018 SpiralScout
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+
+package cmd
+
+import (
+ "github.com/spf13/cobra"
+ "os"
+ "os/signal"
+ "syscall"
+)
+
+var stopSignal = make(chan os.Signal, 1)
+
+func init() {
+ CLI.AddCommand(&cobra.Command{
+ Use: "serve",
+ Short: "Serve RoadRunner service(s)",
+ Run: serveHandler,
+ })
+
+ signal.Notify(stopSignal, os.Interrupt, os.Kill, syscall.SIGTERM)
+}
+
+func serveHandler(cmd *cobra.Command, args []string) {
+ go func() {
+ <-stopSignal
+ Container.Stop()
+ }()
+
+ Container.Serve()
+}
diff --git a/cmd/rr/cmd/version.go b/cmd/rr/cmd/version.go
new file mode 100644
index 00000000..26744922
--- /dev/null
+++ b/cmd/rr/cmd/version.go
@@ -0,0 +1,11 @@
+package cmd
+
+import "time"
+
+var (
+ // Version - defines build version.
+ Version = "local"
+
+ // BuildTime - defined build time.
+ BuildTime = time.Now().Format(time.RFC1123)
+)
diff --git a/cmd/rr/debug/debugger.go b/cmd/rr/debug/debugger.go
new file mode 100644
index 00000000..8ec116c2
--- /dev/null
+++ b/cmd/rr/debug/debugger.go
@@ -0,0 +1,117 @@
+package debug
+
+import (
+ "fmt"
+ "github.com/sirupsen/logrus"
+ "github.com/spiral/roadrunner"
+ "github.com/spiral/roadrunner/cmd/rr/utils"
+ rrhttp "github.com/spiral/roadrunner/service/http"
+ "net/http"
+ "strings"
+)
+
+// Listener creates new debug listener.
+func Listener(logger *logrus.Logger) func(event int, ctx interface{}) {
+ return (&debugger{logger}).listener
+}
+
+// listener provide debug callback for system events. With colors!
+type debugger struct{ logger *logrus.Logger }
+
+// listener listens to http events and generates nice looking output.
+func (s *debugger) listener(event int, ctx interface{}) {
+ // http events
+ switch event {
+ case rrhttp.EventResponse:
+ e := ctx.(*rrhttp.ResponseEvent)
+ s.logger.Info(utils.Sprintf(
+ "<cyan+h>%s</reset> %s <white+hb>%s</reset> %s",
+ e.Request.RemoteAddr,
+ statusColor(e.Response.Status),
+ e.Request.Method,
+ e.Request.URI,
+ ))
+ case rrhttp.EventError:
+ e := ctx.(*rrhttp.ErrorEvent)
+
+ if _, ok := e.Error.(roadrunner.JobError); ok {
+ s.logger.Info(utils.Sprintf(
+ "%s <white+hb>%s</reset> %s",
+ statusColor(500),
+ e.Request.Method,
+ uri(e.Request),
+ ))
+ } else {
+ s.logger.Info(utils.Sprintf(
+ "%s <white+hb>%s</reset> %s <red>%s</reset>",
+ statusColor(500),
+ e.Request.Method,
+ uri(e.Request),
+ e.Error,
+ ))
+ }
+ }
+
+ switch event {
+ case roadrunner.EventWorkerKill:
+ w := ctx.(*roadrunner.Worker)
+ s.logger.Warning(utils.Sprintf(
+ "<white+hb>worker.%v</reset> <yellow>killed</red>",
+ *w.Pid,
+ ))
+ case roadrunner.EventWorkerError:
+ err := ctx.(roadrunner.WorkerError)
+ s.logger.Error(utils.Sprintf(
+ "<white+hb>worker.%v</reset> <red>%s</reset>",
+ *err.Worker.Pid,
+ err.Caused,
+ ))
+ }
+
+ // outputs
+ switch event {
+ case roadrunner.EventStderrOutput:
+ s.logger.Warning(utils.Sprintf("<yellow+h>%s</reset>", strings.Trim(string(ctx.([]byte)), "\r\n")))
+ }
+
+ // rr server events
+ switch event {
+ case roadrunner.EventServerFailure:
+ s.logger.Error(utils.Sprintf("<red>server is dead</reset>"))
+ }
+
+ // pool events
+ switch event {
+ case roadrunner.EventPoolConstruct:
+ s.logger.Debug(utils.Sprintf("<cyan>new worker pool</reset>"))
+ case roadrunner.EventPoolError:
+ s.logger.Error(utils.Sprintf("<red>%s</reset>", ctx))
+ }
+
+ //s.logger.Warning(event, ctx)
+}
+
+func statusColor(status int) string {
+ if status < 300 {
+ return utils.Sprintf("<green>%v</reset>", status)
+ }
+
+ if status < 400 {
+ return utils.Sprintf("<cyan>%v</reset>", status)
+ }
+
+ if status < 500 {
+ return utils.Sprintf("<yellow>%v</reset>", status)
+ }
+
+ return utils.Sprintf("<red>%v</reset>", status)
+}
+
+// uri fetches full uri from request in a form of string (including https scheme if TLS connection is enabled).
+func uri(r *http.Request) string {
+ if r.TLS != nil {
+ return fmt.Sprintf("https://%s%s", r.Host, r.URL.String())
+ }
+
+ return fmt.Sprintf("http://%s%s", r.Host, r.URL.String())
+}
diff --git a/cmd/rr/http/reset.go b/cmd/rr/http/reset.go
new file mode 100644
index 00000000..3bc089ec
--- /dev/null
+++ b/cmd/rr/http/reset.go
@@ -0,0 +1,61 @@
+// Copyright (c) 2018 SpiralScout
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+
+package http
+
+import (
+ "errors"
+ "github.com/spf13/cobra"
+ rr "github.com/spiral/roadrunner/cmd/rr/cmd"
+ "github.com/spiral/roadrunner/cmd/rr/utils"
+ "github.com/spiral/roadrunner/service"
+ "github.com/spiral/roadrunner/service/rpc"
+)
+
+func init() {
+ rr.CLI.AddCommand(&cobra.Command{
+ Use: "http:reset",
+ Short: "Reload RoadRunner worker pools for the HTTP service",
+ RunE: reloadHandler,
+ })
+}
+
+func reloadHandler(cmd *cobra.Command, args []string) error {
+ svc, st := rr.Container.Get(rpc.ID)
+ if st < service.StatusOK {
+ return errors.New("RPC service is not configured")
+ }
+
+ client, err := svc.(*rpc.Service).Client()
+ if err != nil {
+ return err
+ }
+ defer client.Close()
+
+ utils.Printf("<green>restarting http worker pool</reset>: ")
+
+ var r string
+ if err := client.Call("http.Reset", true, &r); err != nil {
+ return err
+ }
+
+ utils.Printf("<green+hb>done</reset>\n")
+ return nil
+}
diff --git a/cmd/rr/http/workers.go b/cmd/rr/http/workers.go
new file mode 100644
index 00000000..b03c273f
--- /dev/null
+++ b/cmd/rr/http/workers.go
@@ -0,0 +1,165 @@
+// Copyright (c) 2018 SpiralScout
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+
+package http
+
+import (
+ "errors"
+ tm "github.com/buger/goterm"
+ "github.com/dustin/go-humanize"
+ "github.com/olekukonko/tablewriter"
+ "github.com/shirou/gopsutil/process"
+ "github.com/spf13/cobra"
+ rr "github.com/spiral/roadrunner/cmd/rr/cmd"
+ "github.com/spiral/roadrunner/cmd/rr/utils"
+ "github.com/spiral/roadrunner/service"
+ "github.com/spiral/roadrunner/service/http"
+ rrpc "github.com/spiral/roadrunner/service/rpc"
+ "net/rpc"
+ "os"
+ "os/signal"
+ "strconv"
+ "syscall"
+ "time"
+)
+
+var (
+ interactive bool
+ stopSignal = make(chan os.Signal, 1)
+)
+
+func init() {
+ workersCommand := &cobra.Command{
+ Use: "http:workers",
+ Short: "List workers associated with RoadRunner HTTP service",
+ RunE: workersHandler,
+ }
+
+ workersCommand.Flags().BoolVarP(
+ &interactive,
+ "interactive",
+ "i",
+ false,
+ "render interactive workers table",
+ )
+
+ rr.CLI.AddCommand(workersCommand)
+
+ signal.Notify(stopSignal, syscall.SIGTERM)
+ signal.Notify(stopSignal, syscall.SIGINT)
+}
+
+func workersHandler(cmd *cobra.Command, args []string) (err error) {
+ defer func() {
+ if r, ok := recover().(error); ok {
+ err = r
+ }
+ }()
+
+ svc, st := rr.Container.Get(rrpc.ID)
+ if st < service.StatusOK {
+ return errors.New("RPC service is not configured")
+ }
+
+ client, err := svc.(*rrpc.Service).Client()
+ if err != nil {
+ return err
+ }
+ defer client.Close()
+
+ if !interactive {
+ showWorkers(client)
+ return nil
+ }
+
+ tm.Clear()
+ for {
+ select {
+ case <-stopSignal:
+ return nil
+ case <-time.NewTicker(time.Millisecond * 500).C:
+ tm.MoveCursor(1, 1)
+ showWorkers(client)
+ tm.Flush()
+ }
+ }
+}
+
+func showWorkers(client *rpc.Client) {
+ var r http.WorkerList
+ if err := client.Call("http.Workers", true, &r); err != nil {
+ panic(err)
+ }
+
+ tw := tablewriter.NewWriter(os.Stdout)
+ tw.SetHeader([]string{"PID", "Status", "Execs", "Memory", "Created"})
+ tw.SetColMinWidth(0, 7)
+ tw.SetColMinWidth(1, 9)
+ tw.SetColMinWidth(2, 7)
+ tw.SetColMinWidth(3, 7)
+ tw.SetColMinWidth(4, 18)
+
+ for _, w := range r.Workers {
+ tw.Append([]string{
+ strconv.Itoa(w.Pid),
+ renderStatus(w.Status),
+ renderJobs(w.NumJobs),
+ renderMemory(w.Pid),
+ renderAlive(time.Unix(0, w.Created)),
+ })
+ }
+
+ tw.Render()
+}
+
+func renderStatus(status string) string {
+ switch status {
+ case "inactive":
+ return utils.Sprintf("<yellow>inactive</reset>")
+ case "ready":
+ return utils.Sprintf("<cyan>ready</reset>")
+ case "working":
+ return utils.Sprintf("<green>working</reset>")
+ case "stopped":
+ return utils.Sprintf("<red>stopped</reset>")
+ case "errored":
+ return utils.Sprintf("<red>errored</reset>")
+ }
+
+ return status
+}
+
+func renderJobs(number int64) string {
+ return humanize.Comma(int64(number))
+}
+
+func renderAlive(t time.Time) string {
+ return humanize.RelTime(t, time.Now(), "ago", "")
+}
+
+func renderMemory(pid int) string {
+ p, _ := process.NewProcess(int32(pid))
+ i, err := p.MemoryInfo()
+ if err != nil {
+ return err.Error()
+ }
+
+ return humanize.Bytes(i.RSS)
+}
diff --git a/cmd/rr/main.go b/cmd/rr/main.go
new file mode 100644
index 00000000..03bef9bd
--- /dev/null
+++ b/cmd/rr/main.go
@@ -0,0 +1,67 @@
+// MIT License
+//
+// Copyright (c) 2018 SpiralScout
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+
+package main
+
+import (
+ rr "github.com/spiral/roadrunner/cmd/rr/cmd"
+
+ // services (plugins)
+ "github.com/spiral/roadrunner/service/http"
+ "github.com/spiral/roadrunner/service/rpc"
+ "github.com/spiral/roadrunner/service/static"
+
+ // cli plugins
+ "github.com/spiral/roadrunner/cmd/rr/debug"
+ _ "github.com/spiral/roadrunner/cmd/rr/http"
+
+ "github.com/sirupsen/logrus"
+ "github.com/spf13/cobra"
+)
+
+var debugMode bool
+
+func main() {
+ // forcing text based logging
+ rr.Logger.Formatter = &logrus.TextFormatter{ForceColors: true}
+
+ // provides ability to make local connection to services
+ rr.Container.Register(rpc.ID, &rpc.Service{})
+
+ // http serving
+ rr.Container.Register(http.ID, &http.Service{})
+
+ // serving static files
+ rr.Container.Register(static.ID, &static.Service{})
+
+ // debug mode
+ rr.CLI.PersistentFlags().BoolVarP(&debugMode, "debug", "d", false, "debug mode")
+ cobra.OnInitialize(func() {
+ if debugMode {
+ service, _ := rr.Container.Get(http.ID)
+ service.(*http.Service).AddListener(debug.Listener(rr.Logger))
+ }
+ })
+
+ // you can register additional commands using cmd.CLI
+ rr.Execute()
+}
diff --git a/cmd/rr/utils/cprint.go b/cmd/rr/utils/cprint.go
new file mode 100644
index 00000000..020975ec
--- /dev/null
+++ b/cmd/rr/utils/cprint.go
@@ -0,0 +1,28 @@
+package utils
+
+import (
+ "fmt"
+ "github.com/mgutz/ansi"
+ "regexp"
+ "strings"
+)
+
+var reg *regexp.Regexp
+
+func init() {
+ reg, _ = regexp.Compile(`<([^>]+)>`)
+}
+
+// Printf works identically to fmt.Print but adds `<white+hb>color formatting support for CLI</reset>`.
+func Printf(format string, args ...interface{}) {
+ fmt.Print(Sprintf(format, args...))
+}
+
+// Sprintf works identically to fmt.Sprintf but adds `<white+hb>color formatting support for CLI</reset>`.
+func Sprintf(format string, args ...interface{}) string {
+ format = reg.ReplaceAllStringFunc(format, func(s string) string {
+ return ansi.ColorCode(strings.Trim(s, "<>/"))
+ })
+
+ return fmt.Sprintf(format, args...)
+}