From c66a8e3def5baf4325dd0b4ebe4f1dbcdc0229ef Mon Sep 17 00:00:00 2001 From: Mitchell Gerber Date: Tue, 25 Jul 2017 18:28:20 -0500 Subject: [PATCH] add tls --- config.template.json | 5 +-- main.go | 46 +++++++++++++++++++++++ package.json | 4 +- server/controller/api/sensor.go | 6 +-- server/model/daily_sensor/daily_sensor.go | 4 +- server/model/raw_sensor/raw_sensor.go | 4 +- server/mywebsite.go | 38 ------------------- server/route/route.go | 28 ++++++++++++-- server/utils/config.go | 33 ++++++++++++---- 9 files changed, 106 insertions(+), 62 deletions(-) create mode 100644 main.go delete mode 100644 server/mywebsite.go diff --git a/config.template.json b/config.template.json index c8e9e20..fac597f 100644 --- a/config.template.json +++ b/config.template.json @@ -8,8 +8,5 @@ "Api": { "key": "" }, - "Port": 8080, - "TLSPort": 443, - "TLSCertFile": "", - "TLSKeyFile": "" + "Address": ":8080" } diff --git a/main.go b/main.go new file mode 100644 index 0000000..5358bc1 --- /dev/null +++ b/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "log" + "net/http" + + "golang.org/x/crypto/acme/autocert" + + "github.com/NYTimes/gziphandler" + "github.com/mgerb/mywebsite/server/controller/api" + "github.com/mgerb/mywebsite/server/db" + "github.com/mgerb/mywebsite/server/route" + "github.com/mgerb/mywebsite/server/utils" +) + +func main() { + configurations := utils.ReadConfig() + + flags := utils.ParseFlags() + + db.Configure(configurations.Database) + api.Configure(configurations.Api) + + db.Mongo.Connect() + + //register middleware + handle := gziphandler.GzipHandler(route.Routes()) + + if flags.TLS { + + // start server on port 80 to redirect + go http.ListenAndServe(":80", route.NonTLSRoutes()) + + log.Println("Starting TLS server...") + + // start TLS server + log.Fatal(http.Serve(autocert.NewListener(), handle)) + + } else { + + log.Println("Starting basic server...") + + // start basic server + http.ListenAndServe(configurations.Address, handle) + } +} diff --git a/package.json b/package.json index 94c3a29..814e7c8 100644 --- a/package.json +++ b/package.json @@ -11,8 +11,8 @@ "deploy": "npm run get_dependencies && npm run prod && ./mywebsite", "dev": "webpack-dev-server --content-base public --inline --hot --history-api-fallback", "generate-tls": "sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout ./tls.key -out ./tls.crt", - "get_dependencies": "go get ./server && npm install", - "prod": "npm run build && go build ./server/mywebsite.go", + "get_dependencies": "go get && npm install", + "prod": "npm run build && go build", "prod-win": "webpack -p --define process.env.NODE_ENV='\"production\"' --progress --colors && babel-node metadata.js && go build ./server/mywebsite.go", "watch": "webpack --watch --colors --progress" }, diff --git a/server/controller/api/sensor.go b/server/controller/api/sensor.go index 9b3253a..1777bff 100644 --- a/server/controller/api/sensor.go +++ b/server/controller/api/sensor.go @@ -3,14 +3,14 @@ package api import ( "encoding/json" "fmt" - "github.com/julienschmidt/httprouter" "log" "net/http" "strconv" "time" - "../../model/daily_sensor" - "../../model/raw_sensor" + "github.com/julienschmidt/httprouter" + "github.com/mgerb/mywebsite/server/model/daily_sensor" + "github.com/mgerb/mywebsite/server/model/raw_sensor" ) // handle http request from sensors diff --git a/server/model/daily_sensor/daily_sensor.go b/server/model/daily_sensor/daily_sensor.go index 9f5bde3..741c96e 100644 --- a/server/model/daily_sensor/daily_sensor.go +++ b/server/model/daily_sensor/daily_sensor.go @@ -3,11 +3,11 @@ package daily_sensor import ( "encoding/json" "errors" - "gopkg.in/mgo.v2/bson" "log" "time" - "../../db" + "github.com/mgerb/mywebsite/server/db" + "gopkg.in/mgo.v2/bson" ) const ( diff --git a/server/model/raw_sensor/raw_sensor.go b/server/model/raw_sensor/raw_sensor.go index 2edde96..fc21557 100644 --- a/server/model/raw_sensor/raw_sensor.go +++ b/server/model/raw_sensor/raw_sensor.go @@ -3,11 +3,11 @@ package raw_sensor import ( "encoding/json" "errors" - "gopkg.in/mgo.v2/bson" "log" "time" - "../../db" + "github.com/mgerb/mywebsite/server/db" + "gopkg.in/mgo.v2/bson" ) const ( diff --git a/server/mywebsite.go b/server/mywebsite.go deleted file mode 100644 index 0109098..0000000 --- a/server/mywebsite.go +++ /dev/null @@ -1,38 +0,0 @@ -package main - -import ( - "github.com/NYTimes/gziphandler" - "log" - "net/http" - "strconv" - - "./controller/api" - "./db" - "./route" - "./utils" -) - -func main() { - configurations := utils.ReadConfig() - - db.Configure(configurations.Database) - api.Configure(configurations.Api) - - db.Mongo.Connect() - - //register middleware - handle := gziphandler.GzipHandler(route.Routes()) - - log.Println("Starting Server...") - log.Println(http.ListenAndServe(":"+strconv.Itoa(configurations.Port), handle)) - - /* enable for TLS support - go func(){ - log.Println(http.ListenAndServe(":"+strconv.Itoa(configurations.Port), handle)) - }() - - if configurations.TLSCertFile != "" && configurations.TLSKeyFile != "" { - log.Println(http.ListenAndServeTLS(":"+strconv.Itoa(configurations.TLSPort), configurations.TLSCertFile, configurations.TLSKeyFile, handle)) - } - */ -} diff --git a/server/route/route.go b/server/route/route.go index b100f56..3e7bc76 100644 --- a/server/route/route.go +++ b/server/route/route.go @@ -1,14 +1,25 @@ package route import ( - "github.com/julienschmidt/httprouter" "log" "net/http" - "../controller" - "../controller/api" + "github.com/julienschmidt/httprouter" + "github.com/mgerb/mywebsite/server/controller" + "github.com/mgerb/mywebsite/server/controller/api" ) +func NonTLSRoutes() *httprouter.Router { + + r := httprouter.New() + r.GET("/api/storedata", api.HandleSensorRequest) + + // redirect to tls on not found + r.NotFound = http.HandlerFunc(tlsRedirect) + + return r +} + func Routes() *httprouter.Router { log.Println("Server Started") @@ -53,3 +64,14 @@ func fileHandler(path string) http.HandlerFunc { } } + +// redirect to tls +func tlsRedirect(w http.ResponseWriter, req *http.Request) { + // remove/add not default ports from req.Host + target := "https://" + req.Host + req.URL.Path + if len(req.URL.RawQuery) > 0 { + target += "?" + req.URL.RawQuery + } + + http.Redirect(w, req, target, http.StatusTemporaryRedirect) +} diff --git a/server/utils/config.go b/server/utils/config.go index aaddfe3..0841311 100644 --- a/server/utils/config.go +++ b/server/utils/config.go @@ -2,22 +2,24 @@ package utils import ( "encoding/json" + "flag" "io/ioutil" "log" "os" - "../controller/api" - "../db" + "github.com/mgerb/mywebsite/server/controller/api" + "github.com/mgerb/mywebsite/server/db" ) //structure for application configurations type Config struct { - Database db.DatabaseInfo `json:"Database"` - Api api.ApiInfo `json:"Api"` - Port int `json:"Port"` - TLSPort int `json:"TLSPort"` - TLSCertFile string `json:"TLSCertFile"` - TLSKeyFile string `json:"TLSKeyFile"` + Database db.DatabaseInfo `json:"Database"` + Api api.ApiInfo `json:"Api"` + Address string `json:"Address"` +} + +type Flags struct { + TLS bool } //read the config file and return JsonObject struct @@ -44,3 +46,18 @@ func ReadConfig() Config { return result } + +func ParseFlags() Flags { + + flags := Flags{ + TLS: false, + } + + tls := flag.Bool("tls", false, "Use TLS") + + flag.Parse() + + flags.TLS = *tls + + return flags +}