{"id":15487211,"url":"https://github.com/theodesp/http-to-https-redirect","last_synced_at":"2025-03-28T16:14:58.216Z","repository":{"id":97066552,"uuid":"126340517","full_name":"theodesp/http-to-https-redirect","owner":"theodesp","description":"Simple HTTP to HTTPS redirect Server in Go","archived":false,"fork":false,"pushed_at":"2018-03-22T13:40:41.000Z","size":24,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-02T16:24:33.840Z","etag":null,"topics":["go","http-proxy","http-server","redirecting-requests"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/theodesp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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-03-22T13:36:42.000Z","updated_at":"2018-03-22T13:41:45.000Z","dependencies_parsed_at":"2023-03-13T16:19:35.977Z","dependency_job_id":null,"html_url":"https://github.com/theodesp/http-to-https-redirect","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/theodesp%2Fhttp-to-https-redirect","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theodesp%2Fhttp-to-https-redirect/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theodesp%2Fhttp-to-https-redirect/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theodesp%2Fhttp-to-https-redirect/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/theodesp","download_url":"https://codeload.github.com/theodesp/http-to-https-redirect/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246059336,"owners_count":20717085,"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":["go","http-proxy","http-server","redirecting-requests"],"created_at":"2024-10-02T06:21:42.848Z","updated_at":"2025-03-28T16:14:58.196Z","avatar_url":"https://github.com/theodesp.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"Simple HTTP to HTTPS redirect Server in Go\n---\n\n## Installation Instructions for Gcloud\n1. Make sure you have the default http port open and accessible from the internet.\nIn order to check that login to Gcloud Compute and visit the instance page. Make sure the \n**Firewall** rules for http is checked. ![img](./http-open.png)\n2. Login to your instance.\n3. Make a folder inside the **$GOPATH** directory that will host the redirect server\n```bash\nmkdir -p $GOPATH/redirect-server\ncd $GOPATH/redirect-server\n```\n\n4. Create a file named `main.go` and copy the following code:\n```\npackage main\n\nimport (\n\t\"flag\"\n\t\"log\"\n\t\"net/http\"\n\t\"strings\"\n\t\"time\"\n)\n\nconst (\n\thttpsPort = \"443\"\n\thttpPort  = \"80\"\n)\n\nfunc redirect(w http.ResponseWriter, req *http.Request) {\n\t// remove/add not default ports from req.Host\n\thostNameParts := strings.Split(req.Host, \":\")\n\ttarget := \"https://\" + hostNameParts[0] + \":\" + httpsPort + req.URL.Path\n\n\tif len(req.URL.RawQuery) \u003e 0 {\n\t\ttarget += \"?\" + req.URL.RawQuery\n\t}\n\tlog.Printf(\"redirect to: %s\", target)\n\thttp.Redirect(w, req, target, http.StatusTemporaryRedirect)\n}\n\nfunc main() {\n\tvar port string\n\tflag.StringVar(\u0026port, \"port\", httpPort, \"http port\")\n\tflag.Parse()\n\n\tserver := \u0026http.Server{\n\t\tAddr:           \":\" + port,\n\t\tReadTimeout:    10 * time.Second,\n\t\tWriteTimeout:   10 * time.Second,\n\t\tMaxHeaderBytes: 1 \u003c\u003c 16,\n\t\tHandler:        http.HandlerFunc(redirect)}\n\n\tlog.Fatal(server.ListenAndServe())\n}\n```\n \nAlternatively just copy the contents of this folder into your $GOPATH.\n```bash\ngcloud compute scp instance:~/redirect-server ~/\ngcloud compute ssh instance\nmv redirect-server $GOPATH/redirect-server\n```\n\n5. Run the App as superuser.\n```bash\nsudo go run main.go\n```\n\n6. If everything is ok the navigate to your server IP address using the http prefix and you \nshould be redirected to the https equivalent.\n\n## Code Details\n* The serve just maps any request from port 80 to port 443.\nIf you look at the `redirect` handler we need to parse the `hostname:port`\nstring and keep the hostname part but change the port part.\n\n* Now for the redirect status we use a temporary redirect but you can also use\na `StatusPermanentRedirect`\n\n* The Server comes with custom timeouts to prevent too many lingering connections.\n\n* It is recommended though to replace this server with a proper Proxy server like nginx because:\n    1. Its more performant.\n    2. Its more declarative.\n    3. Its more scalable.\n    \n* Its recommended to start the server using supervisor or some other production ready process manager.\n\n## Tests\nTo run the tests just enter the following command:\n```\ngo test\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftheodesp%2Fhttp-to-https-redirect","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftheodesp%2Fhttp-to-https-redirect","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftheodesp%2Fhttp-to-https-redirect/lists"}