Skip to content

Instantly share code, notes, and snippets.

@jaymecd
Forked from d-schmidt/redirectExample.go
Created October 11, 2016 21:20
Show Gist options
  • Save jaymecd/a8474e9d646e3842dc0e1f22db8b5e8e to your computer and use it in GitHub Desktop.
Save jaymecd/a8474e9d646e3842dc0e1f22db8b5e8e to your computer and use it in GitHub Desktop.

Revisions

  1. @d-schmidt d-schmidt revised this gist Oct 5, 2015. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions redirectExample.go
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,14 @@
    package main

    import (
    "net/http"
    )


    func redirect(w http.ResponseWriter, req *http.Request) {
    http.Redirect(w, req,
    "https://" + req.Host + req.URL.String(),
    http.StatusMovedPermanently)
    }


    func index(w http.ResponseWriter, req *http.Request) {
    // all calls to unknown url paths should return 404
    if req.URL.Path != "/" {
    @@ -21,7 +18,6 @@ func index(w http.ResponseWriter, req *http.Request) {
    http.ServeFile(w, req, "index.html")
    }


    func main() {
    // redirect every http request to https
    go http.ListenAndServe(":80", http.HandlerFunc(redirect))
  2. @d-schmidt d-schmidt revised this gist Oct 5, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions redirectExample.go
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,12 @@
    package main

    import (
    "net/http"
    "net/http"
    )


    func redirect(w http.ResponseWriter, req *http.Request) {
    http.Redirect(w, req,
    http.Redirect(w, req,
    "https://" + req.Host + req.URL.String(),
    http.StatusMovedPermanently)
    }
    @@ -25,7 +25,7 @@ func index(w http.ResponseWriter, req *http.Request) {
    func main() {
    // redirect every http request to https
    go http.ListenAndServe(":80", http.HandlerFunc(redirect))
    // serve index (and anything else) using tls
    // serve index (and anything else) as https
    mux := http.NewServeMux()
    mux.HandleFunc("/", index)
    http.ListenAndServeTLS(":443", "cert.pem", "key.pem", mux)
  3. @d-schmidt d-schmidt created this gist Oct 5, 2015.
    32 changes: 32 additions & 0 deletions redirectExample.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    package main

    import (
    "net/http"
    )


    func redirect(w http.ResponseWriter, req *http.Request) {
    http.Redirect(w, req,
    "https://" + req.Host + req.URL.String(),
    http.StatusMovedPermanently)
    }


    func index(w http.ResponseWriter, req *http.Request) {
    // all calls to unknown url paths should return 404
    if req.URL.Path != "/" {
    http.NotFound(w, req)
    return
    }
    http.ServeFile(w, req, "index.html")
    }


    func main() {
    // redirect every http request to https
    go http.ListenAndServe(":80", http.HandlerFunc(redirect))
    // serve index (and anything else) using tls
    mux := http.NewServeMux()
    mux.HandleFunc("/", index)
    http.ListenAndServeTLS(":443", "cert.pem", "key.pem", mux)
    }