1
0
mirror of https://github.com/mgerb/mywebsite synced 2026-03-05 07:55:23 +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

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