Skip to content

Commit

Permalink
add an access log option
Browse files Browse the repository at this point in the history
  • Loading branch information
Curtis Ruck committed Nov 20, 2024
1 parent 906242e commit 5afffe1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 11 additions & 1 deletion cmd/goproxygo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ func main() {
router := Router{}
host := flag.String("host", "127.0.0.1", "which interface to bind to")
port := flag.Int("port", 8080, "which port to bind to")
accesslog := flag.Bool("accesslog", false, "enable access logging to stdout")

router.AccessLog = accesslog

flag.Parse()
for _, arg := range flag.Args() {
Expand All @@ -33,7 +36,8 @@ type ProxyRoute struct {
}

type Router struct {
routes []*ProxyRoute
routes []*ProxyRoute
AccessLog *bool
}

func (router *Router) Handle(path string, destination string) {
Expand All @@ -48,6 +52,12 @@ func (router *Router) Handle(path string, destination string) {
}

func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// do access log if enabled
if *router.AccessLog {
// write out access log
log.Printf("%s %s %s %s\n", r.RemoteAddr, r.Method, r.URL.Path, r.Proto)
}

for _, route := range router.routes {
matched := strings.HasPrefix(r.URL.Path, *route.path)
//log.Printf("Checking if %s matches %s and the result is %t", r.URL.Path, route.path, matched)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/ruckc/goproxygo

go 1.22
go 1.23

0 comments on commit 5afffe1

Please sign in to comment.