Browse Source

second commit

pull/1/head
Stian Fredrik Aune 4 years ago
parent
commit
dda774f22b
  1. 12
      app/api/drivers.go
  2. 4
      app/api/events.go
  3. 2
      app/api/util.go
  4. 2
      app/config/channels.go
  5. 29
      app/config/db.go
  6. 10
      app/config/driver.go
  7. 11
      app/config/env.go
  8. 2
      app/config/repo.go
  9. 11
      app/server.go
  10. 12
      app/services/events.go
  11. 24
      cmd/goose/main.go
  12. 2
      cmd/server/main.go
  13. 16
      go.mod
  14. 41
      go.sum
  15. 21
      models/bridge.go
  16. 4
      models/driver.go
  17. 18
      scripts/20210522103453_bridge.sql

12
app/api/drivers.go

@ -0,0 +1,12 @@
package api
import (
"git.aiterp.net/lucifer/new-server/models"
"github.com/gin-gonic/gin"
)
func DriverKinds(r gin.IRoutes) {
r.GET("", handler(func(c *gin.Context) (interface{}, error) {
return models.ValidDriverKinds, nil
}))
}

4
app/api/events.go

@ -1,8 +1,8 @@
package api
import (
"git.aiterp.net/lucifer/server3/app/config"
"git.aiterp.net/lucifer/server3/models"
"git.aiterp.net/lucifer/new-server/app/config"
"git.aiterp.net/lucifer/new-server/models"
"github.com/gin-gonic/gin"
)

2
app/api/util.go

@ -2,7 +2,7 @@ package api
import (
"encoding/json"
"git.aiterp.net/lucifer/server3/models"
"git.aiterp.net/lucifer/new-server/models"
"github.com/gin-gonic/gin"
)

2
app/config/channels.go

@ -1,6 +1,6 @@
package config
import "git.aiterp.net/lucifer/server3/models"
import "git.aiterp.net/lucifer/new-server/models"
var EventChannel = make(chan models.Event, 8)

29
app/config/db.go

@ -0,0 +1,29 @@
package config
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"sync"
)
var dbx *sqlx.DB
var dbLock sync.Mutex
func DBX() *sqlx.DB {
dbLock.Lock()
defer dbLock.Unlock()
if dbx == nil {
dbx = sqlx.MustConnect("mysql", fmt.Sprintf(
"%s:%s@(%s:%d)/%s?parseTime=true",
MySqlUsername(),
MySQlPassword(),
MySqlHost(),
MySqlPort(),
MySqlSchema(),
))
}
return dbx
}

10
app/config/driver.go

@ -1,13 +1,13 @@
package config
import "git.aiterp.net/lucifer/server3/models"
import "git.aiterp.net/lucifer/new-server/models"
var dr models.DriverResolver
var dp models.DriverProvider
func DriverResolver() models.DriverResolver {
if dr == nil {
func DriverProvider() models.DriverProvider {
if dp == nil {
panic("not implemented yet")
}
return dr
return dp
}

11
app/config/env.go

@ -5,12 +5,13 @@ import (
"strconv"
)
var MySqlHost = strEnv("LUCIFER_MYSQL_HOST")
var MySqlPort = intEnv("LUCIFER_MYSQL_PORT")
var MySqlUsername = strEnv("LUCIFER_MYSQL_USERNAME")
var MySQlPassword = strEnv("LUCIFER_MYSQL_PASSWORD")
func MySqlHost() string { return strEnv("LUCIFER_MYSQL_HOST") }
func MySqlPort() int { return intEnv("LUCIFER_MYSQL_PORT") }
func MySqlSchema() string { return strEnv("LUCIFER_MYSQL_SCHEMA") }
func MySqlUsername() string { return strEnv("LUCIFER_MYSQL_USERNAME") }
func MySQlPassword() string { return strEnv("LUCIFER_MYSQL_PASSWORD") }
var ServerPort = intEnv("LUCIFER_SERVER_PORT")
func ServerPort() int { return intEnv("LUCIFER_SERVER_PORT") }
func strEnv(key string) string {
env, ok := os.LookupEnv(key)

2
app/config/repo.go

@ -1,6 +1,6 @@
package config
import "git.aiterp.net/lucifer/server3/models"
import "git.aiterp.net/lucifer/new-server/models"
var (
dRepo models.DeviceRepository

11
app/server.go

@ -2,9 +2,9 @@ package app
import (
"fmt"
"git.aiterp.net/lucifer/server3/app/api"
"git.aiterp.net/lucifer/server3/app/config"
"git.aiterp.net/lucifer/server3/app/services"
"git.aiterp.net/lucifer/new-server/app/api"
"git.aiterp.net/lucifer/new-server/app/config"
"git.aiterp.net/lucifer/new-server/app/services"
"github.com/gin-gonic/gin"
"log"
)
@ -14,7 +14,10 @@ func StartServer() {
gin.SetMode(gin.ReleaseMode)
ginny := gin.New()
api.Events(ginny.Group("/api/events"))
apiGin := ginny.Group("/api")
api.DriverKinds(apiGin.Group("/driver-kinds"))
api.Events(apiGin.Group("/events"))
log.Fatal(ginny.Run(fmt.Sprintf("0.0.0.0:%d", config.ServerPort)))
}

12
app/services/events.go

@ -2,8 +2,8 @@ package services
import (
"context"
"git.aiterp.net/lucifer/server3/app/config"
"git.aiterp.net/lucifer/server3/models"
"git.aiterp.net/lucifer/new-server/app/config"
"git.aiterp.net/lucifer/new-server/models"
"log"
"time"
)
@ -30,6 +30,7 @@ func StartEventHandler() {
var loc, _ = time.LoadLocation("Europe/Oslo")
var ctx = context.Background()
var X = false
func handleEvent(event models.Event) {
if !event.HasPayload("hour") {
@ -39,6 +40,10 @@ func handleEvent(event models.Event) {
event.AddPayload("minute", time.Now().In(loc).Format("04"))
}
if !X {
return
}
handlers, err := config.EventHandlerRepository().FetchAll(ctx)
if err != nil {
log.Printf("Error fetchin event halders: %d", err)
@ -56,6 +61,7 @@ func handleEvent(event models.Event) {
continue
}
panic("panik")
}
log.Println("Unhandled event: " + event.Name)
}

24
cmd/goose/main.go

@ -0,0 +1,24 @@
package main
import (
"git.aiterp.net/lucifer/new-server/app/config"
"github.com/pressly/goose"
"log"
)
func main() {
db := config.DBX().DB
log.Println("Host: " + config.MySqlHost())
log.Println("Schema: " + config.MySqlUsername())
err := goose.SetDialect("mysql")
if err != nil {
log.Fatal(err)
}
err = goose.Run("up", db, "./scripts")
if err != nil {
log.Fatal(err)
}
}

2
main.go → cmd/server/main.go

@ -1,6 +1,6 @@
package main
import "git.aiterp.net/lucifer/server3/app"
import "git.aiterp.net/lucifer/new-server/app"
func main() {
app.StartServer()

16
go.mod

@ -1,5 +1,17 @@
module git.aiterp.net/lucifer/server3
module git.aiterp.net/lucifer/new-server
go 1.16
require github.com/gin-gonic/gin v1.7.1
require (
github.com/ClickHouse/clickhouse-go v1.4.5 // indirect
github.com/denisenkom/go-mssqldb v0.10.0 // indirect
github.com/gin-gonic/gin v1.7.1
github.com/go-sql-driver/mysql v1.6.0
github.com/jmoiron/sqlx v1.3.4
github.com/lib/pq v1.10.2 // indirect
github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pressly/goose v2.7.0+incompatible
github.com/ziutek/mymysql v1.5.4 // indirect
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
)

41
go.sum

@ -1,6 +1,13 @@
github.com/ClickHouse/clickhouse-go v1.4.5 h1:FfhyEnv6/BaWldyjgT2k4gDDmeNwJ9C4NbY/MXxJlXk=
github.com/ClickHouse/clickhouse-go v1.4.5/go.mod h1:EaI/sW7Azgz9UATzd5ZdZHRUhHgv5+JMS9NSr2smCJI=
github.com/bkaradzic/go-lz4 v1.0.0/go.mod h1:0YdlkowM3VswSROI7qDxhRvJ3sLhlFrRRwjwegp5jy4=
github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58 h1:F1EaeKL/ta07PY/k9Os/UFtwERei2/XzGemhpGnBKNg=
github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58/go.mod h1:EOBUe0h4xcZ5GoxqC5SDxFQ8gwyZPKQoEzownBlhI80=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/denisenkom/go-mssqldb v0.10.0 h1:QykgLZBorFE95+gO3u9esLd0BmbvpWp0/waNNZfHBM8=
github.com/denisenkom/go-mssqldb v0.10.0/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.7.1 h1:qC89GU3p8TvKWMAVhEpmpB2CIb1hnqt2UdKZaP93mS8=
@ -13,21 +20,46 @@ github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD87
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/jmoiron/sqlx v1.3.4 h1:wv+0IJZfL5z0uZoUjlpKgHkgaFSYD+r9CfrXjEXsO7w=
github.com/jmoiron/sqlx v1.3.4/go.mod h1:2BljVx/86SuTyjE+aPYlHCTNvZrnJXghYGpNiXLBMCQ=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8=
github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v2.0.3+incompatible h1:gXHsfypPkaMZrKbD5209QV9jbUTJKjyR5WD3HYQSd+U=
github.com/mattn/go-sqlite3 v2.0.3+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pressly/goose v2.7.0+incompatible h1:PWejVEv07LCerQEzMMeAtjuyCKbyprZ/LBa6K5P0OCQ=
github.com/pressly/goose v2.7.0+incompatible/go.mod h1:m+QHWCqxR3k8D9l7qfzuC/djtlfzxr34mozWDYEu1z8=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
@ -36,16 +68,25 @@ github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/ziutek/mymysql v1.5.4 h1:GB0qdRGsTwQSBVYuVShFBKaXSnSnYYC2d9knnE1LHFs=
github.com/ziutek/mymysql v1.5.4/go.mod h1:LMSpPZ6DbqWFxNCHW77HeMg9I646SAhApZ/wKdgO/C0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a h1:kr2P4QFmQr29mSLA43kwrOcgcReGTfbE9N577tCTuBc=
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

21
models/bridge.go

@ -1,16 +1,21 @@
package models
type Bridge struct {
ID int
Name string
Driver DriverType
Address string
Token string
ID int `json:"id"`
Name string `json:"name"`
Driver DriverKind `json:"driver"`
Address string `json:"address"`
Token string `json:"token"`
}
type DriverType string
type DriverKind string
var (
DTHue DriverType = "Hue"
DTNanoLeaf DriverType = "NanoLeaf"
DTHue DriverKind = "Hue"
DTNanoLeaf DriverKind = "NanoLeaf"
)
var ValidDriverKinds = []DriverKind{
DTHue,
DTNanoLeaf,
}

4
models/driver.go

@ -5,8 +5,8 @@ import (
"time"
)
type DriverResolver interface {
ResolveFor(bridge Bridge) (Driver, error)
type DriverProvider interface {
Provide(kind DriverKind) (Driver, error)
}
type Driver interface {

18
scripts/20210522103453_bridge.sql

@ -0,0 +1,18 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE bridge
(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
driver VARCHAR(255) NOT NULL,
address VARCHAR(255) NOT NULL,
token VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE bridge;
-- +goose StatementEnd
Loading…
Cancel
Save