mirror of
https://github.com/mgerb/mywebsite
synced 2026-01-09 01:12:52 +00:00
add tls
This commit is contained in:
@@ -8,8 +8,5 @@
|
|||||||
"Api": {
|
"Api": {
|
||||||
"key": ""
|
"key": ""
|
||||||
},
|
},
|
||||||
"Port": 8080,
|
"Address": ":8080"
|
||||||
"TLSPort": 443,
|
|
||||||
"TLSCertFile": "",
|
|
||||||
"TLSKeyFile": ""
|
|
||||||
}
|
}
|
||||||
|
|||||||
46
main.go
Normal file
46
main.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,8 +11,8 @@
|
|||||||
"deploy": "npm run get_dependencies && npm run prod && ./mywebsite",
|
"deploy": "npm run get_dependencies && npm run prod && ./mywebsite",
|
||||||
"dev": "webpack-dev-server --content-base public --inline --hot --history-api-fallback",
|
"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",
|
"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",
|
"get_dependencies": "go get && npm install",
|
||||||
"prod": "npm run build && go build ./server/mywebsite.go",
|
"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",
|
"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"
|
"watch": "webpack --watch --colors --progress"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -3,14 +3,14 @@ package api
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/julienschmidt/httprouter"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"../../model/daily_sensor"
|
"github.com/julienschmidt/httprouter"
|
||||||
"../../model/raw_sensor"
|
"github.com/mgerb/mywebsite/server/model/daily_sensor"
|
||||||
|
"github.com/mgerb/mywebsite/server/model/raw_sensor"
|
||||||
)
|
)
|
||||||
|
|
||||||
// handle http request from sensors
|
// handle http request from sensors
|
||||||
|
|||||||
@@ -3,11 +3,11 @@ package daily_sensor
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"gopkg.in/mgo.v2/bson"
|
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"../../db"
|
"github.com/mgerb/mywebsite/server/db"
|
||||||
|
"gopkg.in/mgo.v2/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|||||||
@@ -3,11 +3,11 @@ package raw_sensor
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"gopkg.in/mgo.v2/bson"
|
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"../../db"
|
"github.com/mgerb/mywebsite/server/db"
|
||||||
|
"gopkg.in/mgo.v2/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|||||||
@@ -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))
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
@@ -1,14 +1,25 @@
|
|||||||
package route
|
package route
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/julienschmidt/httprouter"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"../controller"
|
"github.com/julienschmidt/httprouter"
|
||||||
"../controller/api"
|
"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 {
|
func Routes() *httprouter.Router {
|
||||||
|
|
||||||
log.Println("Server Started")
|
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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,22 +2,24 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"../controller/api"
|
"github.com/mgerb/mywebsite/server/controller/api"
|
||||||
"../db"
|
"github.com/mgerb/mywebsite/server/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
//structure for application configurations
|
//structure for application configurations
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Database db.DatabaseInfo `json:"Database"`
|
Database db.DatabaseInfo `json:"Database"`
|
||||||
Api api.ApiInfo `json:"Api"`
|
Api api.ApiInfo `json:"Api"`
|
||||||
Port int `json:"Port"`
|
Address string `json:"Address"`
|
||||||
TLSPort int `json:"TLSPort"`
|
}
|
||||||
TLSCertFile string `json:"TLSCertFile"`
|
|
||||||
TLSKeyFile string `json:"TLSKeyFile"`
|
type Flags struct {
|
||||||
|
TLS bool
|
||||||
}
|
}
|
||||||
|
|
||||||
//read the config file and return JsonObject struct
|
//read the config file and return JsonObject struct
|
||||||
@@ -44,3 +46,18 @@ func ReadConfig() Config {
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ParseFlags() Flags {
|
||||||
|
|
||||||
|
flags := Flags{
|
||||||
|
TLS: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
tls := flag.Bool("tls", false, "Use TLS")
|
||||||
|
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
flags.TLS = *tls
|
||||||
|
|
||||||
|
return flags
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user