GithubHelp home page GithubHelp logo

theodesp / http-to-https-redirect Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 24 KB

Simple HTTP to HTTPS redirect Server in Go

License: MIT License

Go 100.00%
http-server redirecting-requests go http-proxy

http-to-https-redirect's Introduction

Simple HTTP to HTTPS redirect Server in Go

Installation Instructions for Gcloud

  1. Make sure you have the default http port open and accessible from the internet. In order to check that login to Gcloud Compute and visit the instance page. Make sure the Firewall rules for http is checked. img
  2. Login to your instance.
  3. Make a folder inside the $GOPATH directory that will host the redirect server
mkdir -p $GOPATH/redirect-server
cd $GOPATH/redirect-server
  1. Create a file named main.go and copy the following code:
package main

import (
	"flag"
	"log"
	"net/http"
	"strings"
	"time"
)

const (
	httpsPort = "443"
	httpPort  = "80"
)

func redirect(w http.ResponseWriter, req *http.Request) {
	// remove/add not default ports from req.Host
	hostNameParts := strings.Split(req.Host, ":")
	target := "https://" + hostNameParts[0] + ":" + httpsPort + req.URL.Path

	if len(req.URL.RawQuery) > 0 {
		target += "?" + req.URL.RawQuery
	}
	log.Printf("redirect to: %s", target)
	http.Redirect(w, req, target, http.StatusTemporaryRedirect)
}

func main() {
	var port string
	flag.StringVar(&port, "port", httpPort, "http port")
	flag.Parse()

	server := &http.Server{
		Addr:           ":" + port,
		ReadTimeout:    10 * time.Second,
		WriteTimeout:   10 * time.Second,
		MaxHeaderBytes: 1 << 16,
		Handler:        http.HandlerFunc(redirect)}

	log.Fatal(server.ListenAndServe())
}

Alternatively just copy the contents of this folder into your $GOPATH.

gcloud compute scp instance:~/redirect-server ~/
gcloud compute ssh instance
mv redirect-server $GOPATH/redirect-server
  1. Run the App as superuser.
sudo go run main.go
  1. If everything is ok the navigate to your server IP address using the http prefix and you should be redirected to the https equivalent.

Code Details

  • The serve just maps any request from port 80 to port 443. If you look at the redirect handler we need to parse the hostname:port string and keep the hostname part but change the port part.

  • Now for the redirect status we use a temporary redirect but you can also use a StatusPermanentRedirect

  • The Server comes with custom timeouts to prevent too many lingering connections.

  • It is recommended though to replace this server with a proper Proxy server like nginx because:

    1. Its more performant.
    2. Its more declarative.
    3. Its more scalable.
  • Its recommended to start the server using supervisor or some other production ready process manager.

Tests

To run the tests just enter the following command:

go test

http-to-https-redirect's People

Contributors

theodesp avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.