Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bin/mirage
Binary file not shown.
84 changes: 63 additions & 21 deletions cmd/mirage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,55 @@
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"

"mirage/internal/config"
"mirage/internal/logger"
"mirage/internal/proxy"
"mirage/internal/recorder"
"mirage/internal/ui"
"mirage/internal/updater"

"github.com/pkg/browser"
"github.com/spf13/cobra"
)

const version = "0.1.0"

func main() {
var port int
var configPath string
var noBrowser bool

var rootCmd = &cobra.Command{
Use: "mirage",
Short: "Mirage is an API mocking gateway",
Long: `Mirage intercepts HTTP requests and allows mocking responses, recording traffic, and simulating network conditions.`,
Use: "mirage",
Short: "Mirage is an API mocking gateway",
Long: `Mirage intercepts HTTP requests and allows mocking responses, recording traffic, and simulating network conditions.`,
Version: version,
}

var startCmd = &cobra.Command{
Use: "start",
Short: "Start the proxy server",
Run: func(cmd *cobra.Command, args []string) {
logger.PrintBanner(version)

addr := fmt.Sprintf(":%d", port)
fmt.Printf("Starting Mirage proxy on %s...\n", addr)
dashboardURL := fmt.Sprintf("http://localhost:%d/__mirage/", port)

var cfg *config.Config
if configPath != "" {
var err error
cfg, err = config.LoadConfig(configPath)
if err != nil {
log.Fatalf("Failed to load config: %v", err)
logger.LogError(fmt.Sprintf("Failed to load config: %v", err))
os.Exit(1)
}
fmt.Printf("Loaded configuration from %s (%d scenarios)\n", configPath, len(cfg.Scenarios))
logger.LogSuccess(fmt.Sprintf("Loaded %d scenarios from %s", len(cfg.Scenarios), configPath))
} else {
fmt.Println("No config file specified (-c), running in pure proxy mode")
logger.LogInfo("No config specified, running in pure proxy mode")
}

p := proxy.NewProxy(cfg, nil)
Expand All @@ -58,8 +67,17 @@
}
})

logger.LogSuccess(fmt.Sprintf("Server started on %s", addr))
logger.LogInfo(fmt.Sprintf("Dashboard: %s", dashboardURL))
fmt.Println()

if !noBrowser {
go browser.OpenURL(dashboardURL)

Check failure on line 75 in cmd/mirage/main.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `browser.OpenURL` is not checked (errcheck)
}

if err := http.ListenAndServe(addr, handler); err != nil {
log.Fatalf("Server failed: %v", err)
logger.LogError(fmt.Sprintf("Server failed: %v", err))
os.Exit(1)
}
},
}
Expand All @@ -69,14 +87,20 @@
Use: "record",
Short: "Start proxy in recording mode",
Run: func(cmd *cobra.Command, args []string) {
logger.PrintBanner(version)

addr := fmt.Sprintf(":%d", port)
fmt.Printf("Starting Mirage recorder on %s, saving to %s...\n", addr, outputFile)

rec := recorder.NewRecorder(outputFile)
p := proxy.NewProxy(nil, rec)

logger.LogSuccess(fmt.Sprintf("Recording started on %s", addr))
logger.LogInfo(fmt.Sprintf("Saving to %s", outputFile))
fmt.Println()

if err := http.ListenAndServe(addr, p); err != nil {
log.Fatalf("Server failed: %v", err)
logger.LogError(fmt.Sprintf("Server failed: %v", err))
os.Exit(1)
}
},
}
Expand All @@ -86,6 +110,7 @@

startCmd.Flags().IntVarP(&port, "port", "p", 8080, "Port to run the proxy on")
startCmd.Flags().StringVarP(&configPath, "config", "c", "", "Path to scenarios config file")
startCmd.Flags().BoolVar(&noBrowser, "no-browser", false, "Don't open browser automatically")

var scenariosCmd = &cobra.Command{
Use: "scenarios",
Expand All @@ -99,11 +124,12 @@
Run: func(cmd *cobra.Command, args []string) {
cfg, err := config.LoadConfig(args[0])
if err != nil {
log.Fatalf("Failed to load config: %v", err)
logger.LogError(fmt.Sprintf("Failed to load config: %v", err))
os.Exit(1)
}
fmt.Printf("Scenarios in %s:\n", args[0])
logger.LogSuccess(fmt.Sprintf("Scenarios in %s:", args[0]))
for _, s := range cfg.Scenarios {
fmt.Printf("- %s (Matches: %s %s)\n", s.Name, s.Match.Method, s.Match.Path)
fmt.Printf("%s (%s %s)\n", s.Name, s.Match.Method, s.Match.Path)
}
},
}
Expand All @@ -116,24 +142,26 @@
Run: func(cmd *cobra.Command, args []string) {
data, err := os.ReadFile(args[0])
if err != nil {
log.Fatalf("Failed to read file: %v", err)
logger.LogError(fmt.Sprintf("Failed to read file: %v", err))
os.Exit(1)
}

var interactions []recorder.Interaction
if err := json.Unmarshal(data, &interactions); err != nil {
log.Fatalf("Failed to parse JSON: %v", err)
logger.LogError(fmt.Sprintf("Failed to parse JSON: %v", err))
os.Exit(1)
}

client := &http.Client{}
fmt.Printf("Replaying %d interactions...\n", len(interactions))
logger.LogInfo(fmt.Sprintf("Replaying %d interactions...", len(interactions)))

for i, interaction := range interactions {
reqData := interaction.Request
fmt.Printf("[%d] %s %s... ", i+1, reqData.Method, reqData.URL)

req, err := http.NewRequest(reqData.Method, reqData.URL, strings.NewReader(reqData.Body))
if err != nil {
fmt.Printf("Failed to create request: %v\n", err)
logger.LogError(fmt.Sprintf("Failed to create request: %v", err))
continue
}

Expand All @@ -145,11 +173,24 @@

resp, err := client.Do(req)
if err != nil {
fmt.Printf("Error: %v\n", err)
logger.LogError(err.Error())
continue
}
resp.Body.Close()
fmt.Printf("Status: %d\n", resp.StatusCode)
logger.LogSuccess(fmt.Sprintf("Status: %d", resp.StatusCode))
}
},
}

var updateCmd = &cobra.Command{
Use: "update",
Short: "Update mirage to the latest version",
Run: func(cmd *cobra.Command, args []string) {
logger.PrintBanner(version)

if err := updater.Update(version); err != nil {
logger.LogError(err.Error())
os.Exit(1)
}
},
}
Expand All @@ -158,9 +199,10 @@
rootCmd.AddCommand(recordCmd)
rootCmd.AddCommand(scenariosCmd)
rootCmd.AddCommand(replayCmd)
rootCmd.AddCommand(updateCmd)

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
logger.LogError(err.Error())
os.Exit(1)
}
}
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,22 @@ go 1.24.2
require github.com/spf13/cobra v1.10.2

require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc // indirect
github.com/charmbracelet/lipgloss v1.1.0 // indirect
github.com/charmbracelet/x/ansi v0.8.0 // indirect
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect
github.com/charmbracelet/x/term v0.2.1 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/muesli/termenv v0.16.0 // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/spf13/pflag v1.0.10 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/sys v0.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
31 changes: 31 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc h1:4pZI35227imm7yK2bGPcfpFEmuY1gc2YSTShr4iJBfs=
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc/go.mod h1:X4/0JoqgTIPSFcRA/P6INZzIuyqdFY5rm8tb41s9okk=
github.com/charmbracelet/lipgloss v1.1.0 h1:vYXsiLHVkK7fp74RkV7b2kq9+zDLoEU4MZoFqR/noCY=
github.com/charmbracelet/lipgloss v1.1.0/go.mod h1:/6Q8FR2o+kj8rz4Dq0zQc3vYf7X+B0binUUBwA0aL30=
github.com/charmbracelet/x/ansi v0.8.0 h1:9GTq3xq9caJW8ZrBTe0LIe2fvfLR/bYXKTx2llXn7xE=
github.com/charmbracelet/x/ansi v0.8.0/go.mod h1:wdYl/ONOLHLIVmQaxbIYEC/cRKOQyjTkowiI4blgS9Q=
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd h1:vy0GVL4jeHEwG5YOXDmi86oYw2yuYUGqz6a8sLwg0X8=
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd/go.mod h1:xe0nKWGd3eJgtqZRaN9RjMtK7xUYchjzPr7q6kcvCCs=
github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ=
github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg=
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading
Loading