Browse Source

Location, location, location.

feature-colorvalue2 3.5.0
Stian Fredrik Aune 3 years ago
parent
commit
a08685c3cd
  1. 19
      app/config/env.go
  2. 5
      app/server.go
  3. 8
      models/timeofday.go

19
app/config/env.go

@ -1,17 +1,34 @@
package config package config
import ( import (
"log"
"os" "os"
"strconv" "strconv"
"time"
) )
func Location() *time.Location {
env, ok := os.LookupEnv("LUCIFER_LOCATION")
if !ok {
return time.UTC
}
loc, err := time.LoadLocation(env)
if err != nil {
log.Printf("Location lookup failed: %s (%s)", env, err.Error())
return time.UTC
}
return loc
}
func MySqlHost() string { return strEnv("LUCIFER_MYSQL_HOST") } func MySqlHost() string { return strEnv("LUCIFER_MYSQL_HOST") }
func MySqlPort() int { return intEnv("LUCIFER_MYSQL_PORT") } func MySqlPort() int { return intEnv("LUCIFER_MYSQL_PORT") }
func MySqlSchema() string { return strEnv("LUCIFER_MYSQL_SCHEMA") } func MySqlSchema() string { return strEnv("LUCIFER_MYSQL_SCHEMA") }
func MySqlUsername() string { return strEnv("LUCIFER_MYSQL_USERNAME") } func MySqlUsername() string { return strEnv("LUCIFER_MYSQL_USERNAME") }
func MySQlPassword() string { return strEnv("LUCIFER_MYSQL_PASSWORD") } func MySQlPassword() string { return strEnv("LUCIFER_MYSQL_PASSWORD") }
func ServerPort() int { return intEnv("LUCIFER_SERVER_PORT") }
func ServerPort() int { return intEnv("LUCIFER_SERVER_PORT") }
func strEnv(key string) string { func strEnv(key string) string {
env, ok := os.LookupEnv(key) env, ok := os.LookupEnv(key)

5
app/server.go

@ -7,13 +7,14 @@ import (
"git.aiterp.net/lucifer/new-server/app/config" "git.aiterp.net/lucifer/new-server/app/config"
"git.aiterp.net/lucifer/new-server/app/services" "git.aiterp.net/lucifer/new-server/app/services"
"git.aiterp.net/lucifer/new-server/app/services/publisher" "git.aiterp.net/lucifer/new-server/app/services/publisher"
"git.aiterp.net/lucifer/new-server/models"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"log" "log"
"time" "time"
) )
func StartServer() { func StartServer() {
setupCtx, cancel := context.WithTimeout(context.Background(), time.Second * 10)
setupCtx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel() defer cancel()
err := publisher.Initialize(setupCtx) err := publisher.Initialize(setupCtx)
@ -38,5 +39,7 @@ func StartServer() {
api.EventHandlers(apiGin.Group("/event-handlers")) api.EventHandlers(apiGin.Group("/event-handlers"))
api.Scenes(apiGin.Group("/scenes")) api.Scenes(apiGin.Group("/scenes"))
models.TimeOfDayTimeZone = config.Location()
log.Fatal(ginny.Run(fmt.Sprintf("0.0.0.0:%d", config.ServerPort()))) log.Fatal(ginny.Run(fmt.Sprintf("0.0.0.0:%d", config.ServerPort())))
} }

8
models/timeofday.go

@ -9,6 +9,8 @@ import (
"time" "time"
) )
var TimeOfDayTimeZone = time.UTC
type TimeOfDay int type TimeOfDay int
func (t *TimeOfDay) UnmarshalJSON(v []byte) error { func (t *TimeOfDay) UnmarshalJSON(v []byte) error {
@ -29,7 +31,6 @@ func (t *TimeOfDay) UnmarshalJSON(v []byte) error {
} }
} }
t2, err := ParseTimeOfDay(str) t2, err := ParseTimeOfDay(str)
if err != nil { if err != nil {
return err return err
@ -82,11 +83,12 @@ func (t TimeOfDay) IsBetween(from TimeOfDay, to TimeOfDay) bool {
} }
func CurrentTimeOfDay() TimeOfDay { func CurrentTimeOfDay() TimeOfDay {
return TimeOfDayFromDate(time.Now().Local())
return TimeOfDayFromDate(time.Now().In(TimeOfDayTimeZone))
} }
func TimeOfDayFromDate(date time.Time) TimeOfDay { func TimeOfDayFromDate(date time.Time) TimeOfDay {
return NewTimeOfDay(date.Hour(), date.Minute(), date.Second())
localDate := date.In(TimeOfDayTimeZone)
return NewTimeOfDay(localDate.Hour(), localDate.Minute(), localDate.Second())
} }
func NewTimeOfDay(hours, minutes, seconds int) TimeOfDay { func NewTimeOfDay(hours, minutes, seconds int) TimeOfDay {

Loading…
Cancel
Save