Verified Commit cb882db2 authored by Caleb Woodbine's avatar Caleb Woodbine 🛳

Add: root and wildcard directs, tests for new additions; Update: documentation

parent 4e8694f2
Pipeline #105502628 passed with stages
in 2 minutes and 34 seconds
......@@ -10,9 +10,11 @@ routes:
duck: https://duckduckgo.com
gitlab: https://gitlab.com
github: https://github.com
root: https://gitlab.com
wildcard: https://github.com
```
Given the config above, if [`https://localhost:8080/duck`](https://localhost:8080/duck) is visited, it will redirect to [`https://duckduckgo.com`](https://duckduckgo.com).
Given the config above, if [`https://localhost:8080/duck`](https://localhost:8080/duck) is visited, the request will redirect to [`https://duckduckgo.com`](https://duckduckgo.com). If [`https://localhost:8080`](https://localhost:8080) is visited, the request will be redirected to [`https://gitlab.com`](https://gitlab.com). If the path that doesn't exist is visited, the request will be redirected to [`https://github.com`](https://github.com).
## Local usage
```bash
......
duck: https://duckduckgo.com
gitlab: https://gitlab.com
\ No newline at end of file
routes:
duck: https://duckduckgo.com
github: https://github.com
gitlab: https://about.gitlab.com
root: https://about.gitlab.com
wildcard: https://github.com
......@@ -86,10 +86,26 @@ func APIshortLink(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
redirectURL := configYAML.Routes[vars["link"]]
if redirectURL == "" {
if configYAML.Wildcard == "" {
w.WriteHeader(404)
w.Write([]byte(`404 page not found`))
return
} else {
http.Redirect(w, r, configYAML.Wildcard, 302)
return
}
}
http.Redirect(w, r, redirectURL, 302)
}
func APIroot(w http.ResponseWriter, r *http.Request) {
configYAML := ReadConfigYAML()
if configYAML.Root == "" {
w.WriteHeader(404)
w.Write([]byte(`404 page not found`))
return
}
http.Redirect(w, r, redirectURL, 302)
http.Redirect(w, r, configYAML.Root, 302)
}
// print a table of the environment variables
......@@ -104,8 +120,17 @@ func PrintEnvConfig() {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Value"})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetTablePadding("\t")
table.SetNoWhiteSpace(true)
table.AppendBulk(data)
table.Render()
fmt.Println()
......@@ -130,6 +155,7 @@ func HandleWebserver() {
http.ServeFile(w, r, "./robots.txt")
})
router.HandleFunc("/{link:[a-zA-Z0-9]+}", APIshortLink)
router.HandleFunc("/", APIroot)
router.Use(RequestLogger)
srv := &http.Server{
Handler: router,
......
package types
type ConfigYAML struct {
Routes Routes `yaml:"routes"`
Routes Routes `yaml:"routes"`
Root string `yaml:"root"`
Wildcard string `yaml:"wildcard"`
}
type Routes map[string]string
......@@ -20,6 +20,8 @@ var _ = Describe("API redirect tests", func() {
"gitlab": "https://about.gitlab.com",
"github": "https://github.com",
},
Root: "https://about.gitlab.com",
Wildcard: "https://github.com",
}
BeforeEach(func() {
......@@ -32,7 +34,6 @@ var _ = Describe("API redirect tests", func() {
It("should redirect from a page to an url", func() {
By("visiting written redirect")
//pageRef := "duck"
for key, _ := range configYAML.Routes {
resp, err := http.Get(fmt.Sprintf("http://localhost:8080/%v", key))
Expect(err).To(BeNil(), "Request should not return errors")
......@@ -40,11 +41,51 @@ var _ = Describe("API redirect tests", func() {
}
})
It("should respond with 404 for non-existent url", func() {
It("should respond with 404 for non-existent url if a wildcard doesn't exist", func() {
By("visiting an non-existent url")
// remove wildcard in config
file, err := os.Create(configStoreLocation)
Expect(err).To(BeNil())
configYAMLnew := configYAML
configYAMLnew.Wildcard = ""
configYAMLFmt, _ := yaml.Marshal(configYAMLnew)
_, err = file.Write(configYAMLFmt)
Expect(err).To(BeNil())
pageRef := "aaaaa"
resp, err := http.Get(fmt.Sprintf("http://localhost:8080/%v", pageRef))
Expect(err).To(BeNil(), "Request should not return errors")
Expect(resp.StatusCode).To(Equal(404))
Expect(resp.Request.URL.Host).To(Equal("localhost:8080"))
})
It("should redirect to wildcard if is defined", func() {
pageRef := "aaaaa"
resp, err := http.Get(fmt.Sprintf("http://localhost:8080/%v", pageRef))
Expect(err).To(BeNil(), "Request should not return errors")
Expect(resp.StatusCode).To(Equal(200))
Expect(fmt.Sprintf("%v://%v", resp.Request.URL.Scheme, resp.Request.URL.Host)).To(Equal(configYAML.Wildcard))
})
It("should redirect from root to a page if is defined", func() {
resp, err := http.Get(fmt.Sprintf("http://localhost:8080/"))
Expect(err).To(BeNil(), "Request should not return errors")
Expect(resp.StatusCode).To(Equal(200))
Expect(fmt.Sprintf("%v://%v", resp.Request.URL.Scheme, resp.Request.URL.Host)).To(Equal(configYAML.Root))
})
It("should respond with 404 from root if it is not defined", func() {
// remove root in config
file, err := os.Create(configStoreLocation)
Expect(err).To(BeNil())
configYAMLnew := configYAML
configYAMLnew.Root = ""
configYAMLFmt, _ := yaml.Marshal(configYAMLnew)
_, err = file.Write(configYAMLFmt)
Expect(err).To(BeNil())
resp, err := http.Get(fmt.Sprintf("http://localhost:8080/"))
Expect(err).To(BeNil(), "Request should not return errors")
Expect(resp.StatusCode).To(Equal(404))
Expect(resp.Request.URL.Host).To(Equal("localhost:8080"))
})
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment