Skip to content

Commit

Permalink
feat: Add --preserve-path arg
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejsika committed Apr 22, 2024
1 parent 58afa39 commit da90b4e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
10 changes: 9 additions & 1 deletion cmd/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
)

var FlagTarget string
var PreservePath bool

var Cmd = &cobra.Command{
Use: "all",
Short: "Redirect all to ...",
Aliases: []string{"s"},
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
err := server.Server(FlagTarget)
err := server.Server(FlagTarget, PreservePath)
if err != nil {
log.Fatal(err)
}
Expand All @@ -33,4 +34,11 @@ func init() {
"Redirect target (eg.: https://google.com)",
)
Cmd.MarkPersistentFlagRequired("target")
Cmd.PersistentFlags().BoolVarP(
&PreservePath,
"preserve-path",
"p",
false,
"Preserve path",
)
}
5 changes: 4 additions & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"net/http"
)

func Server(target string) error {
func Server(target string, preservePath bool) error {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if preservePath {
target = fmt.Sprintf("%s%s", target, r.URL.Path)
}
http.Redirect(w, r, target, http.StatusTemporaryRedirect)
})

Expand Down

0 comments on commit da90b4e

Please sign in to comment.