{"id":17362159,"url":"https://github.com/calebfroese/urlshort","last_synced_at":"2025-03-27T16:46:30.381Z","repository":{"id":100593000,"uuid":"141545723","full_name":"calebfroese/urlshort","owner":"calebfroese","description":"Gophercises Exercise 2","archived":false,"fork":false,"pushed_at":"2018-07-19T08:07:59.000Z","size":20,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-01T20:29:31.470Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/calebfroese.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-07-19T08:07:08.000Z","updated_at":"2018-07-19T08:08:01.000Z","dependencies_parsed_at":"2023-05-16T04:15:25.999Z","dependency_job_id":null,"html_url":"https://github.com/calebfroese/urlshort","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calebfroese%2Furlshort","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calebfroese%2Furlshort/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calebfroese%2Furlshort/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calebfroese%2Furlshort/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/calebfroese","download_url":"https://codeload.github.com/calebfroese/urlshort/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245887907,"owners_count":20688893,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T19:37:12.939Z","updated_at":"2025-03-27T16:46:30.376Z","avatar_url":"https://github.com/calebfroese.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Exercise #2: URL Shortener\n\n[![exercise status: released](https://img.shields.io/badge/exercise%20status-released-green.svg?style=for-the-badge)](https://gophercises.com/exercises/urlshort)\n\n\n\n## Exercise details\n\nThe goal of this exercise is to create an [http.Handler](https://golang.org/pkg/net/http/#Handler) that will look at the path of any incoming web request and determine if it should redirect the user to a new page, much like URL shortener would.\n\nFor instance, if we have a redirect setup for `/dogs` to `https://www.somesite.com/a-story-about-dogs` we would look for any incoming web requests with the path `/dogs` and redirect them.\n\nTo complete this exercises you will need to implement the stubbed out methods in [handler.go](https://github.com/gophercises/urlshort/blob/master/handler.go). There are a good bit of comments explaining what each method should do, and there is also a [main/main.go](https://github.com/gophercises/urlshort/blob/master/main/main.go) source file that uses the package to help you test your code and get an idea of what your program should be doing.\n\nI suggest first commenting out all of the code in main.go related to the `YAMLHandler` function and focusing on implementing the `MapHandler` function first.\n\nOnce you have that working, focus on parsing the YAML using the [gopkg.in/yaml.v2](https://godoc.org/gopkg.in/yaml.v2) package. *Note: You will need to `go get` this package if you don't have it already.*\n\nAfter you get the YAML parsing down, try to convert the data into a map and then use the MapHandler to finish the YAMLHandler implementation. Eg you might end up with some code like this:\n\n```go\nfunc YAMLHandler(yaml []byte, fallback http.Handler) (http.HandlerFunc, error) {\n  parsedYaml, err := parseYAML(yaml)\n  if err != nil {\n    return nil, err\n  }\n  pathMap := buildMap(parsedYaml)\n  return MapHandler(pathMap, fallback), nil\n}\n```\n\nBut in order for this to work you will need to create functions like `parseYAML` and `buildMap` on your own. This should give you ample experience working with YAML data.\n\n\n## Bonus\n\nAs a bonus exercises you can also...\n\n1. Update the [main/main.go](https://github.com/gophercises/urlshort/blob/master/main/main.go) source file to accept a YAML file as a flag and then load the YAML from a file rather than from a string.\n2. Build a JSONHandler that serves the same purpose, but reads from JSON data.\n3. Build a Handler that doesn't read from a map but instead reads from a database. Whether you use BoltDB, SQL, or something else is entirely up to you.0\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcalebfroese%2Furlshort","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcalebfroese%2Furlshort","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcalebfroese%2Furlshort/lists"}