1
0
mirror of https://github.com/mgerb/mywebsite synced 2026-01-08 09:02:47 +00:00
This commit is contained in:
2017-07-25 18:28:20 -05:00
parent 081334ee20
commit c66a8e3def
9 changed files with 106 additions and 62 deletions

View File

@@ -8,8 +8,5 @@
"Api": {
"key": ""
},
"Port": 8080,
"TLSPort": 443,
"TLSCertFile": "",
"TLSKeyFile": ""
"Address": ":8080"
}

46
main.go Normal file
View 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)
}
}

View File

@@ -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"
},

View File

@@ -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

View File

@@ -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 (

View File

@@ -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 (

View File

@@ -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))
}
*/
}

View File

@@ -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)
}

View File

@@ -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
}