1
0
mirror of https://github.com/mgerb/mywebsite synced 2026-01-11 02:12:53 +00:00

convert server to use relative imports

This commit is contained in:
2017-01-12 14:51:25 +01:00
parent a68c97fa3c
commit 148721e071
6 changed files with 44 additions and 45 deletions

View File

@@ -9,8 +9,8 @@ import (
"strconv" "strconv"
"time" "time"
"mywebsite/server/model/daily_sensor" "../../model/daily_sensor"
"mywebsite/server/model/raw_sensor" "../../model/raw_sensor"
) )
// handle http request from sensors // handle http request from sensors
@@ -175,13 +175,13 @@ func HandleSensorByLocationMonth(w http.ResponseWriter, r *http.Request, ps http
} }
func HandleUniqueDates(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { func HandleUniqueDates(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
location := ps.ByName("location") location := ps.ByName("location")
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
s, err := daily_sensor.GetUniqueSensorDates(location) s, err := daily_sensor.GetUniqueSensorDates(location)
var response string var response string
if err != nil { if err != nil {
@@ -201,7 +201,7 @@ func HandleUniqueDates(w http.ResponseWriter, r *http.Request, ps httprouter.Par
fmt.Fprint(w, response) fmt.Fprint(w, response)
} }
func createResponse(s []daily_sensor.Data, err error) string{ func createResponse(s []daily_sensor.Data, err error) string {
var response string var response string
if err != nil { if err != nil {
@@ -217,6 +217,6 @@ func createResponse(s []daily_sensor.Data, err error) string{
response = string(js) response = string(js)
} }
} }
return response return response
} }

View File

@@ -7,7 +7,7 @@ import (
"log" "log"
"time" "time"
"mywebsite/server/db" "../../db"
) )
const ( const (
@@ -23,7 +23,7 @@ type Data struct {
MonthName string `json:"monthname,omitempty" bson:"monthname"` MonthName string `json:"monthname,omitempty" bson:"monthname"`
Day int `json:"day,omitempty" bson:"day"` Day int `json:"day,omitempty" bson:"day"`
Year int `json:"year,omitempty" bson:"year"` Year int `json:"year,omitempty" bson:"year"`
Updated time.Time `json:"updated,omitempty" bson:"updated"` Updated time.Time `json:"updated,omitempty" bson:"updated"`
} }
//convert struct to json string //convert struct to json string
@@ -211,35 +211,34 @@ type UniqueDates struct{
*/ */
type Years struct { type Years struct {
Year int `json:"year", bson:"year"` Year int `json:"year", bson:"year"`
Months []Month `json:"months", bson:"months"` Months []Month `json:"months", bson:"months"`
} }
type Month struct { type Month struct {
Month int `json:"month", bson:"month"` Month int `json:"month", bson:"month"`
MonthName string `json:"monthname", bson:"monthname"` MonthName string `json:"monthname", bson:"monthname"`
} }
func GetUniqueSensorDates(sensor_location string) ([]Years, error){ func GetUniqueSensorDates(sensor_location string) ([]Years, error) {
d := []Years{} d := []Years{}
if db.Mongo.Connected() == true { if db.Mongo.Connected() == true {
session := db.Mongo.Session.Copy() session := db.Mongo.Session.Copy()
defer session.Close() defer session.Close()
c := session.DB(db.Mongo.Info.Database).C(collection) c := session.DB(db.Mongo.Info.Database).C(collection)
err := c.Pipe([]bson.M{bson.M{"$match": bson.M{"location": sensor_location}}, err := c.Pipe([]bson.M{bson.M{"$match": bson.M{"location": sensor_location}},
bson.M{"$group": bson.M{"_id": "$year", "months": bson.M{"$addToSet": bson.M{"month": "$month", "monthname": "$monthname"}}}}, bson.M{"$group": bson.M{"_id": "$year", "months": bson.M{"$addToSet": bson.M{"month": "$month", "monthname": "$monthname"}}}},
bson.M{"$project": bson.M{"year": "$_id", "months": "$months"}}, bson.M{"$project": bson.M{"year": "$_id", "months": "$months"}},
bson.M{"$unwind": "$months"}, bson.M{"$unwind": "$months"},
bson.M{"$sort": bson.M{"months.month": -1}}, bson.M{"$sort": bson.M{"months.month": -1}},
bson.M{"$group": bson.M{"_id": "$year", "months": bson.M{"$push": "$months"}}}, bson.M{"$group": bson.M{"_id": "$year", "months": bson.M{"$push": "$months"}}},
bson.M{"$project": bson.M{"year": "$_id", "months": "$months"}}, bson.M{"$project": bson.M{"year": "$_id", "months": "$months"}},
}).All(&d) }).All(&d)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return d, nil return d, nil
@@ -250,4 +249,4 @@ func GetUniqueSensorDates(sensor_location string) ([]Years, error){
} else { } else {
return d, errors.New("Query failed") return d, errors.New("Query failed")
} }
} }

View File

@@ -7,7 +7,7 @@ import (
"log" "log"
"time" "time"
"mywebsite/server/db" "../../db"
) )
const ( const (
@@ -60,8 +60,8 @@ func (s *Data) StoreData() error {
//handle queries for all sensors page //handle queries for all sensors page
type DataStore_AllSensors struct { type DataStore_AllSensors struct {
ID bson.ObjectId `bson:"_id,omitempty"` ID bson.ObjectId `bson:"_id,omitempty"`
Location string `json:"location", bson:"location"` Location string `json:"location", bson:"location"`
Temperature float64 `json:"temperature" bson:"temperature"` Temperature float64 `json:"temperature" bson:"temperature"`
Updated time.Time `json:"updated" bson:"updated"` Updated time.Time `json:"updated" bson:"updated"`
} }
@@ -92,4 +92,4 @@ func GetAllSensors() ([]DataStore_AllSensors, error) {
} else { } else {
return s, errors.New("Query failed") return s, errors.New("Query failed")
} }
} }

View File

@@ -6,10 +6,10 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"mywebsite/server/controller/api" "./controller/api"
"mywebsite/server/db" "./db"
"mywebsite/server/route" "./route"
"mywebsite/server/utils" "./utils"
) )
func main() { func main() {

View File

@@ -5,8 +5,8 @@ import (
"log" "log"
"net/http" "net/http"
"mywebsite/server/controller" "../controller"
"mywebsite/server/controller/api" "../controller/api"
) )
func Routes() *httprouter.Router { func Routes() *httprouter.Router {
@@ -21,7 +21,7 @@ func Routes() *httprouter.Router {
r.GET("/api/sensor/:location/:year", api.HandleSensorByLocationYear) r.GET("/api/sensor/:location/:year", api.HandleSensorByLocationYear)
r.GET("/api/sensor/:location/:year/:monthname", api.HandleSensorByLocationMonth) r.GET("/api/sensor/:location/:year/:monthname", api.HandleSensorByLocationMonth)
r.GET("/api/uniquedates/:location", api.HandleUniqueDates) r.GET("/api/uniquedates/:location", api.HandleUniqueDates)
r.GET("/discord", controller.DiscordRedirect) r.GET("/discord", controller.DiscordRedirect)
r.GET("/vpn", controller.VPNRedirect) r.GET("/vpn", controller.VPNRedirect)
r.GET("/camera", controller.CameraRedirect) r.GET("/camera", controller.CameraRedirect)

View File

@@ -6,18 +6,18 @@ import (
"log" "log"
"os" "os"
"mywebsite/server/controller/api" "../controller/api"
"mywebsite/server/db" "../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"` Port int `json:"Port"`
TLSPort int `json:"TLSPort"` TLSPort int `json:"TLSPort"`
TLSCertFile string `json:"TLSCertFile"` TLSCertFile string `json:"TLSCertFile"`
TLSKeyFile string `json:"TLSKeyFile"` TLSKeyFile string `json:"TLSKeyFile"`
} }
//read the config file and return JsonObject struct //read the config file and return JsonObject struct