Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to heroku #11

Merged
merged 1 commit into from
Oct 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Dockerfile

This file was deleted.

1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: senatus
5 changes: 0 additions & 5 deletions docker-compose.yml

This file was deleted.

12 changes: 12 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package: senatus
import:
- package: github.com/cozmo/senatus
subpackages:
- db
- handler
- package: github.com/gorilla/mux
- package: github.com/gorilla/sessions
- package: github.com/segmentio/go-env
- package: gopkg.in/mgo.v2
subpackages:
- bson
50 changes: 37 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,45 @@ package main

import (
"fmt"
"net/http"
"os"
"runtime"
"strings"

"github.com/cozmo/senatus/db"
"github.com/cozmo/senatus/handler"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
env "github.com/segmentio/go-env"
"net/http"
"os"
"runtime"
)

func isHttps(r *http.Request) bool {
if r.URL.Scheme == "https" {
return true
}
if strings.HasPrefix(r.Proto, "HTTPS") {
return true
}
if r.Header.Get("X-Forwarded-Proto") == "https" {
return true
}
return false
}

func ensureHttpsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if !isHttps(req) {
hostToSend := req.Host
if req.Header.Get("X-Forwarded-Host") != "" {
hostToSend = req.Header.Get("X-Forwarded-Host")
}
http.Redirect(res, req, "https://"+hostToSend+req.URL.String(), 301)
} else {
next.ServeHTTP(res, req)
}
})
}

func sessionMiddleware(h *handler.Handler, store sessions.Store, routeHandler func(http.ResponseWriter, *http.Request, *db.User)) func(http.ResponseWriter, *http.Request) {
return func(res http.ResponseWriter, req *http.Request) {
session, err := store.Get(req, "session")
Expand Down Expand Up @@ -40,15 +69,6 @@ func main() {

runtime.GOMAXPROCS(runtime.NumCPU())

// If we get an INSECURE_PORT env var we forward all requests on that port to HTTPs. This allows us to get
// around routing systems that don't forward the x-forwarded-proto headers correctly (like https://convox.com/)
if os.Getenv("INSECURE_PORT") != "" {
fmt.Println("Serving Senatus HTTPs redirector on port " + os.Getenv("INSECURE_PORT"))
go http.ListenAndServe(":"+os.Getenv("INSECURE_PORT"), http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
http.Redirect(res, req, "https://"+req.Host+req.URL.String(), 301)
}))
}

database, err := db.NewMongoDB(os.Getenv("MONGO_URL"))
if err != nil {
panic("Error connecting to mongo")
Expand Down Expand Up @@ -78,7 +98,11 @@ func main() {
r.PathPrefix("/public/").Handler(http.StripPrefix("/public/", http.FileServer(http.Dir("./public"))))
r.HandleFunc("/{url:.*}", h.NotFoundHandler)

http.Handle("/", r)
if os.Getenv("ENFORCE_HTTPS") != "" {
http.Handle("/", ensureHttpsMiddleware(r))
} else {
http.Handle("/", r)
}
fmt.Println("Serving Senatus on port " + os.Getenv("PORT"))
http.ListenAndServe(":"+os.Getenv("PORT"), nil)
}