GetLocalIP function

Returns:

  • string
  • error
Show/Hide Function Body
{
	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")
}

OpenBrowser function

Parameters:

  • url string

Returns:

  • error
Show/Hide Function Body
{
	return browser.OpenURL(url)
}

TestOpenBrowserError function

Parameters:

  • t *testing.T
Show/Hide Function Body
{
	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")
	}
}

ClearScreen function

Show/Hide Function Body
{
	fmt.Print("\033[H\033[2J")
	fmt.Println()
}

PrintStartupInfo function

Parameters:

  • port string
  • httpsPort string
  • localIP string
  • host bool
Show/Hide Function Body
{
	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()
}

LogServeRequest function

Parameters:

  • r *http.Request
Show/Hide Function Body
{
	fmt.Printf("%s %s %s\n", faint(time.Now().Format("15:04:05")), boldYellow("serving"), faint(r.URL.Path))
}

Info function

Parameters:

  • message string
Show/Hide Function Body
{
	fmt.Println(boldRed("[rfw]"), message)
}

Fatal function

Parameters:

  • message string
  • err error
Show/Hide Function Body
{
	log.Fatalf(boldRed("[rfw] "), message, err)
}

EnableDebug function

Parameters:

  • d bool
Show/Hide Function Body
{ dbg = d }

IsDebug function

IsDebug reports whether debug mode is enabled.

Returns:

  • bool
Show/Hide Function Body
{ return dbg }

Debug function

Parameters:

  • message string
Show/Hide Function Body
{
	if dbg {
		fmt.Println(boldRed("[rfw][debug]"), faint(message))
	}
}

PrintHelp function

Show/Hide Function Body
{
	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 function

captureOutput redirects stdout for the duration of f and returns what was

written to it.

Parameters:

  • f func()

Returns:

  • string
Show/Hide Function Body
{
	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()
}

TestDebug function

Parameters:

  • t *testing.T
Show/Hide Function Body
{
	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)
	}
}

TestIsDebug function

Parameters:

  • t *testing.T
Show/Hide Function Body
{
	EnableDebug(true)
	if !IsDebug() {
		t.Fatalf("expected true in debug mode")
	}
	EnableDebug(false)
	if IsDebug() {
		t.Fatalf("expected false when debug disabled")
	}
}

TestPrintStartupInfo function

Parameters:

  • t *testing.T
Show/Hide Function Body
{
	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)
	}
}

TestPrintHelp function

Parameters:

  • t *testing.T
Show/Hide Function Body
{
	out := captureOutput(PrintHelp)
	if !strings.Contains(out, "Shortcuts") || !strings.Contains(out, "Flags") {
		t.Fatalf("missing help sections, got %q", out)
	}
}

TestLogServeRequest function

Parameters:

  • t *testing.T
Show/Hide Function Body
{
	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)
	}
}

fmt import

Import example:

import "fmt"

net import

Import example:

import "net"

github.com/pkg/browser import

Import example:

import "github.com/pkg/browser"

os import

Import example:

import "os"

testing import

Import example:

import "testing"

fmt import

Import example:

import "fmt"

log import

Import example:

import "log"

net/http import

Import example:

import "net/http"

time import

Import example:

import "time"

github.com/fatih/color import

Import example:

import "github.com/fatih/color"

github.com/rfwlab/rfw/v1/core import

Import example:

import "github.com/rfwlab/rfw/v1/core"

bytes import

Import example:

import "bytes"

io import

Import example:

import "io"

net/http/httptest import

Import example:

import "net/http/httptest"

os import

Import example:

import "os"

strings import

Import example:

import "strings"

testing import

Import example:

import "testing"