{
addrs, err := net.InterfaceAddrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
if ipNet, ok := addr.(*net.IPNet); ok && !ipNet.IP.IsLoopback() && ipNet.IP.To4() != nil {
return ipNet.IP.String(), nil
}
}
return "", fmt.Errorf("no local IP address found")
}
{
return browser.OpenURL(url)
}
{
orig := os.Getenv("BROWSER")
_ = os.Setenv("BROWSER", "nonexistent-browser")
defer os.Setenv("BROWSER", orig)
if err := OpenBrowser("http://example.com"); err == nil {
t.Fatalf("expected error when browser command is missing")
}
}
{
fmt.Print("\033[H\033[2J")
fmt.Println()
}
{
fmt.Println(indent, boldRed("rfw"), faint(core.Version))
fmt.Println()
fmt.Println(indent, red("➜ "), bold("Local:"), red(fmt.Sprintf("http://localhost:%s/ - https://localhost:%s/", port, httpsPort)))
if host {
fmt.Println(indent, red("➜ "), faint(bold("Network:")), white(fmt.Sprintf("http://%s:%s/ (https://%s:%s/)", localIP, port, localIP, httpsPort)))
} else {
fmt.Println(indent, red("➜ "), faint(bold("Network:")), white("--host"), faint("to expose"))
}
fmt.Println(indent, faintRed("➜ "), faint("Press"), bold("h + enter"), faint("to show help"))
fmt.Println()
}
{
fmt.Printf("%s %s %s\n", faint(time.Now().Format("15:04:05")), boldYellow("serving"), faint(r.URL.Path))
}
{
fmt.Println(boldRed("[rfw]"), message)
}
{
log.Fatalf(boldRed("[rfw] "), message, err)
}
{ dbg = d }
IsDebug reports whether debug mode is enabled.
{ return dbg }
{
if dbg {
fmt.Println(boldRed("[rfw][debug]"), faint(message))
}
}
{
ClearScreen()
fmt.Println()
fmt.Println(indent, red("➜ "), bold("Help"))
fmt.Println(indent, indent, yellow("➜ "), bold("Shortcuts"))
fmt.Println(indent, indent, indent, faint("Press"), bold("c + enter"), faint("to stop the server"))
fmt.Println(indent, indent, indent, faint("Press"), bold("o + enter"), faint("to open the browser"))
fmt.Println(indent, indent, indent, faint("Press"), bold("u + enter"), faint("to show the startup info and clear logs"))
fmt.Println(indent, indent, indent, faint("Press"), bold("h + enter"), faint("to show this help"))
fmt.Println(indent, indent, yellow("➜ "), bold("Flags"))
fmt.Println(indent, indent, indent, faint("Use"), bold("--host"), faint("to expose the server to the network"))
fmt.Println(indent, indent, indent, faint("Use"), bold("--port=XXXX"), faint("to specify a port"))
fmt.Println()
}
captureOutput redirects stdout for the duration of f and returns what was
written to it.
{
orig := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
f()
w.Close()
os.Stdout = orig
var buf bytes.Buffer
_, _ = io.Copy(&buf, r)
return buf.String()
}
{
EnableDebug(true)
out := captureOutput(func() { Debug("hello") })
if !strings.Contains(out, "[rfw][debug]") {
t.Fatalf("expected debug output, got %q", out)
}
EnableDebug(false)
out = captureOutput(func() { Debug("no output") })
if out != "" {
t.Fatalf("expected no output, got %q", out)
}
}
{
EnableDebug(true)
if !IsDebug() {
t.Fatalf("expected true in debug mode")
}
EnableDebug(false)
if IsDebug() {
t.Fatalf("expected false when debug disabled")
}
}
{
out := captureOutput(func() { PrintStartupInfo("8080", "8443", "192.168.0.1", true) })
if !strings.Contains(out, "http://localhost:8080/") {
t.Fatalf("expected local URL in output, got %q", out)
}
if !strings.Contains(out, "http://192.168.0.1:8080/") {
t.Fatalf("expected network URL, got %q", out)
}
out = captureOutput(func() { PrintStartupInfo("8080", "8443", "", false) })
if !strings.Contains(out, "--host") {
t.Fatalf("expected hint about --host, got %q", out)
}
}
{
out := captureOutput(PrintHelp)
if !strings.Contains(out, "Shortcuts") || !strings.Contains(out, "Flags") {
t.Fatalf("missing help sections, got %q", out)
}
}
{
req := httptest.NewRequest("GET", "/foo", nil)
out := captureOutput(func() { LogServeRequest(req) })
if !strings.Contains(out, "/foo") {
t.Fatalf("expected path in output, got %q", out)
}
}
import "fmt"
import "net"
import "github.com/pkg/browser"
import "os"
import "testing"
import "fmt"
import "log"
import "net/http"
import "time"
import "github.com/fatih/color"
import "github.com/rfwlab/rfw/v1/core"
import "bytes"
import "io"
import "net/http/httptest"
import "os"
import "strings"
import "testing"