diff --git a/.gitignore b/.gitignore index 7fbe486..3808901 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,6 @@ node_modules dist public npm-debug.log -mywebsite \ No newline at end of file +mywebsite +tls.key +tls.crt \ No newline at end of file diff --git a/config.template.json b/config.template.json index a987565..c8e9e20 100644 --- a/config.template.json +++ b/config.template.json @@ -1,12 +1,15 @@ { - "database": { + "Database": { "url": "", "database": "", "username": "", "password": "" }, - "api": { + "Api": { "key": "" }, - "port": 8080 + "Port": 8080, + "TLSPort": 443, + "TLSCertFile": "", + "TLSKeyFile": "" } diff --git a/package.json b/package.json index 6a5ec6b..94c3a29 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "check-gzip-size": "gzip -9 -c ./public/client.min.js | wc -c | numfmt --to=iec-i --suffix=B --padding=10", "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", "prod-win": "webpack -p --define process.env.NODE_ENV='\"production\"' --progress --colors && babel-node metadata.js && go build ./server/mywebsite.go", diff --git a/server/mywebsite.go b/server/mywebsite.go index edb23b2..0e233f4 100644 --- a/server/mywebsite.go +++ b/server/mywebsite.go @@ -24,5 +24,11 @@ func main(){ handle := gziphandler.GzipHandler(route.Routes()) log.Println("Starting Server...") - log.Println(http.ListenAndServe(":"+strconv.Itoa(configurations.Port), handle)) + 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/utils/config.go b/server/utils/config.go index 5085ad6..6bdb181 100644 --- a/server/utils/config.go +++ b/server/utils/config.go @@ -12,9 +12,12 @@ import ( //structure for application configurations type Config struct { - Database db.DatabaseInfo `json:"database"` - Api api.ApiInfo `json:"api"` - Port int `json:"port"` + 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"` } //read the config file and return JsonObject struct