[add] initial commit
This commit is contained in:
32
README.md
Normal file
32
README.md
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
<h1 align="center">[WIP] Gelvin</h1>
|
||||||
|
|
||||||
|
<h4 align="center">
|
||||||
|
REST API for IoT Sensors.
|
||||||
|
</h4>
|
||||||
|
|
||||||
|
`Gelvin` is a simple REST-API for IoT Sensors. Currently work in progess. API is subject to change.
|
||||||
|
|
||||||
|
<p align="center">
|
||||||
|
<strong>
|
||||||
|
<a href="https://api.pdev.dev/gelvin/measurements/2">My Office Temp (°C)</a>
|
||||||
|
• <a href="#rest-api">Rest API</a>
|
||||||
|
</strong>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
## REST API
|
||||||
|
|
||||||
|
The following is a quick overview of the available commands.
|
||||||
|
|
||||||
|
### Sensors
|
||||||
|
|
||||||
|
* [`GET /sensors`]() - Retrieve all sensors information
|
||||||
|
* [`GET /sensors/{SensorID}`]() - Retrieve sensor information
|
||||||
|
* [`POST /sensors`]() - Create or update a sensor and retrieve `SensorID`
|
||||||
|
|
||||||
|
|
||||||
|
### Measurements
|
||||||
|
|
||||||
|
* [`GET /measurements`]() - Retrieve all sensors measurements
|
||||||
|
* [`GET /measurements/{SensorID}`]() - Retrieve sensor measurements
|
||||||
|
* [`POST /measurements`]() - Add measurement (payload needs to include `SensorID`)
|
||||||
|
* [`POST /measurements/{SensorID}`]() - Add measurement to sensor
|
||||||
119
app/application/application.go
Normal file
119
app/application/application.go
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
package application
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Mode uint
|
||||||
|
|
||||||
|
const (
|
||||||
|
Debug Mode = iota
|
||||||
|
Test
|
||||||
|
Production
|
||||||
|
)
|
||||||
|
|
||||||
|
type Version struct {
|
||||||
|
Major uint8
|
||||||
|
Minor uint8
|
||||||
|
Patch uint8
|
||||||
|
}
|
||||||
|
|
||||||
|
type AppError struct {
|
||||||
|
Error string `json:"error"`
|
||||||
|
Debug string `json:"debug"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Application struct {
|
||||||
|
version Version
|
||||||
|
urlRoot string
|
||||||
|
database *Database
|
||||||
|
engine *gin.Engine
|
||||||
|
mode Mode
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Application) SqliteDB(path string) {
|
||||||
|
a.database = &Database{
|
||||||
|
databaseType: SQLite3,
|
||||||
|
path: path,
|
||||||
|
mode: a.mode,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Application) OpenDB() error {
|
||||||
|
return a.database.open()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Application) CloseDB() error {
|
||||||
|
return a.database.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a Application) Db() *gorm.DB {
|
||||||
|
return a.database.db
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a Application) Version() string {
|
||||||
|
return fmt.Sprintf("%d.%d.%d", a.version.Major, a.version.Minor, a.version.Patch)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a Application) VersionNamespace() string {
|
||||||
|
return fmt.Sprintf("%d", a.version.Major)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a Application) UrlRoot() string {
|
||||||
|
return a.urlRoot
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a Application) Engine() *gin.Engine {
|
||||||
|
return a.engine
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Application) Run(address string) error {
|
||||||
|
return a.engine.Run(address)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a Application) Database() *Database {
|
||||||
|
return a.database
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a Application) Mode() Mode {
|
||||||
|
return a.mode
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a Application) NewError(error string, debug string) interface{} {
|
||||||
|
if a.mode == Debug {
|
||||||
|
return AppError{
|
||||||
|
Error: error,
|
||||||
|
Debug: debug,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return struct {
|
||||||
|
Error string `json:"error"`
|
||||||
|
Debug string `json:"-"`
|
||||||
|
}{error, debug}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewApplication(version Version, urlRoot string, mode Mode) *Application {
|
||||||
|
|
||||||
|
switch mode {
|
||||||
|
case Production:
|
||||||
|
gin.SetMode(gin.ReleaseMode)
|
||||||
|
case Test:
|
||||||
|
gin.SetMode(gin.TestMode)
|
||||||
|
}
|
||||||
|
|
||||||
|
app := Application{
|
||||||
|
version: Version{
|
||||||
|
Major: version.Major,
|
||||||
|
Minor: version.Minor,
|
||||||
|
Patch: version.Patch,
|
||||||
|
},
|
||||||
|
urlRoot: urlRoot,
|
||||||
|
engine: gin.Default(),
|
||||||
|
mode: mode,
|
||||||
|
}
|
||||||
|
|
||||||
|
return &app
|
||||||
|
}
|
||||||
52
app/application/database.go
Normal file
52
app/application/database.go
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package application
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"gorm.io/driver/sqlite"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"gorm.io/gorm/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DatabaseType uint8
|
||||||
|
|
||||||
|
const (
|
||||||
|
SQL DatabaseType = iota
|
||||||
|
SQLite3
|
||||||
|
)
|
||||||
|
|
||||||
|
type Database struct {
|
||||||
|
databaseType DatabaseType
|
||||||
|
path string
|
||||||
|
db *gorm.DB
|
||||||
|
mode Mode
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Database) open() error {
|
||||||
|
var err error
|
||||||
|
switch d.databaseType {
|
||||||
|
case SQL:
|
||||||
|
err = errors.New("not yet implemented")
|
||||||
|
case SQLite3:
|
||||||
|
var config gorm.Config
|
||||||
|
if d.mode == Debug {
|
||||||
|
config = gorm.Config{}
|
||||||
|
} else {
|
||||||
|
config = gorm.Config{Logger: logger.Default.LogMode(logger.Silent)}
|
||||||
|
}
|
||||||
|
db, err := gorm.Open(sqlite.Open(d.path), &config)
|
||||||
|
if err == nil {
|
||||||
|
d.db = db
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
err = errors.New("unknown database type")
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Database) close() error {
|
||||||
|
db, err := d.db.DB()
|
||||||
|
if err == nil {
|
||||||
|
err = db.Close()
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
22
app/controllers/controller.go
Normal file
22
app/controllers/controller.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gelvin/app/application"
|
||||||
|
"gelvin/app/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Controller struct {
|
||||||
|
app *application.Application
|
||||||
|
model *models.Model
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Controller) Model() *models.Model {
|
||||||
|
return c.model
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewController(app *application.Application) *Controller {
|
||||||
|
return &Controller{
|
||||||
|
app: app,
|
||||||
|
model: models.NewModel(app),
|
||||||
|
}
|
||||||
|
}
|
||||||
134
app/controllers/sensor.go
Normal file
134
app/controllers/sensor.go
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"gelvin/app/models"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c Controller) GetSensors(ctx *gin.Context) {
|
||||||
|
var sensors []models.Sensor
|
||||||
|
result := c.app.Db().Find(&sensors)
|
||||||
|
if result.Error != nil {
|
||||||
|
ctx.IndentedJSON(http.StatusNotFound, c.app.NewError("sensor not found", result.Error.Error()))
|
||||||
|
} else {
|
||||||
|
ctx.IndentedJSON(http.StatusOK, sensors)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Controller) GetSensor(ctx *gin.Context) {
|
||||||
|
var sensor models.Sensor
|
||||||
|
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
ctx.IndentedJSON(http.StatusBadRequest, c.app.NewError("invalid input", err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
result := c.app.Db().First(&sensor, id)
|
||||||
|
if result.Error != nil {
|
||||||
|
ctx.IndentedJSON(http.StatusNotFound, c.app.NewError("sensor not found", result.Error.Error()))
|
||||||
|
} else {
|
||||||
|
ctx.IndentedJSON(http.StatusOK, sensor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Controller) AddSensor(ctx *gin.Context) {
|
||||||
|
var sensor models.Sensor
|
||||||
|
var err = ctx.BindJSON(&sensor)
|
||||||
|
if err == nil && !sensor.IsValid() {
|
||||||
|
err = errors.New("invalid sensor")
|
||||||
|
}
|
||||||
|
var result uint
|
||||||
|
httpCode := http.StatusCreated
|
||||||
|
if err == nil {
|
||||||
|
var txSensor models.Sensor
|
||||||
|
tx := c.app.Db().Where(&models.Sensor{Name: sensor.Name}).First(&txSensor)
|
||||||
|
if tx.Error == nil {
|
||||||
|
result, err = c.model.UpdateSensor(sensor, txSensor, tx)
|
||||||
|
httpCode = http.StatusOK
|
||||||
|
} else {
|
||||||
|
result, err = c.model.AddSensor(sensor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
ctx.IndentedJSON(http.StatusBadRequest, c.app.NewError("invalid input", err.Error()))
|
||||||
|
} else {
|
||||||
|
id := struct {
|
||||||
|
Id uint `json:"id"`
|
||||||
|
}{result}
|
||||||
|
ctx.IndentedJSON(httpCode, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Controller) GetMeasurements(ctx *gin.Context) {
|
||||||
|
var measurements []models.Measurement
|
||||||
|
//result := c.app.Db().Find(&measurements)
|
||||||
|
// MAX(created_at) converts time.Time to string, `as created_at` or datetime() is not sufficient
|
||||||
|
result := c.app.Db().Select("MAX(created_at)",
|
||||||
|
"id", "created_at", "deleted_at", "sensor_id", "value").Group("sensor_id").Find(&measurements)
|
||||||
|
if result.Error != nil {
|
||||||
|
ctx.IndentedJSON(http.StatusOK, struct{}{})
|
||||||
|
} else {
|
||||||
|
ctx.IndentedJSON(http.StatusOK, measurements)
|
||||||
|
} //result := c.app.Db().Select("id sensor_id MAX(created_at) value").Group("sensor_id").Find(&measurements)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Controller) GetMeasurement(ctx *gin.Context) {
|
||||||
|
var measurements []models.Measurement
|
||||||
|
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
ctx.IndentedJSON(http.StatusBadRequest, c.app.NewError("invalid input", err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
result := c.app.Db().Where(&models.Measurement{SensorID: uint(id)}).Find(&measurements)
|
||||||
|
if result.Error != nil {
|
||||||
|
ctx.IndentedJSON(http.StatusOK, struct{}{})
|
||||||
|
} else {
|
||||||
|
ctx.IndentedJSON(http.StatusOK, measurements)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Controller) AddMeasurement(ctx *gin.Context) {
|
||||||
|
var measurement models.Measurement
|
||||||
|
err := ctx.BindJSON(&measurement)
|
||||||
|
if err != nil {
|
||||||
|
ctx.IndentedJSON(http.StatusBadRequest, c.app.NewError("invalid input", err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.addMeasurement(ctx, measurement)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Controller) AddMeasurementID(ctx *gin.Context) {
|
||||||
|
var measurement models.Measurement
|
||||||
|
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
ctx.IndentedJSON(http.StatusBadRequest, c.app.NewError("invalid input", err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = ctx.BindJSON(&measurement)
|
||||||
|
if err != nil {
|
||||||
|
ctx.IndentedJSON(http.StatusBadRequest, c.app.NewError("invalid input", err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
measurement.SensorID = uint(id)
|
||||||
|
c.addMeasurement(ctx, measurement)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Controller) addMeasurement(ctx *gin.Context, measurement models.Measurement) {
|
||||||
|
result, err := c.model.AddMeasurement(measurement)
|
||||||
|
if err != nil {
|
||||||
|
if err == gorm.ErrRecordNotFound {
|
||||||
|
ctx.IndentedJSON(http.StatusNotFound, c.app.NewError("sensor not found", err.Error()))
|
||||||
|
} else {
|
||||||
|
ctx.IndentedJSON(http.StatusBadRequest, c.app.NewError("invalid input", err.Error()))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
id := struct {
|
||||||
|
Id uint `json:"id"`
|
||||||
|
}{result}
|
||||||
|
ctx.IndentedJSON(http.StatusOK, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
19
app/models/model.go
Normal file
19
app/models/model.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gelvin/app/application"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Model struct {
|
||||||
|
app *application.Application
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewModel(app *application.Application) *Model {
|
||||||
|
return &Model{
|
||||||
|
app: app,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Migrate(app *application.Application) error {
|
||||||
|
return app.Db().AutoMigrate(&Sensor{}, &Measurement{})
|
||||||
|
}
|
||||||
124
app/models/sensor.go
Normal file
124
app/models/sensor.go
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql/driver"
|
||||||
|
"errors"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SensorType uint
|
||||||
|
|
||||||
|
const (
|
||||||
|
Temperature SensorType = iota + 1 // to allow gorm: not null
|
||||||
|
Humidity
|
||||||
|
Pressure
|
||||||
|
Gas
|
||||||
|
CO2
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s SensorType) IsValid() bool {
|
||||||
|
switch s {
|
||||||
|
case Temperature, Humidity, Pressure, Gas, CO2:
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Position struct {
|
||||||
|
X int `json:"x"`
|
||||||
|
Y int `json:"y"`
|
||||||
|
NotNull bool `json:"-"` // to allow gorm: not null with x/y = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
type Sensor struct {
|
||||||
|
ID uint `json:"id" gorm:"primaryKey"`
|
||||||
|
CreatedAt time.Time `json:"-"`
|
||||||
|
UpdatedAt time.Time `json:"-"`
|
||||||
|
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
|
||||||
|
Name string `json:"name" gorm:"uniqueIndex;not null;default:null"`
|
||||||
|
SensorType SensorType `json:"sensor_type" gorm:"not null;default:null"`
|
||||||
|
Position Position `json:"position" gorm:"not null;default:null"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Measurement struct {
|
||||||
|
ID uint `json:"id" gorm:"primaryKey"`
|
||||||
|
CreatedAt time.Time `json:"time"` // TODO: change to datetime2
|
||||||
|
UpdatedAt time.Time `json:"-"` // TODO: remove
|
||||||
|
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` // TODO: remove
|
||||||
|
SensorID uint `json:"sensor_id" gorm:"<-:create;not null;default:null"`
|
||||||
|
Sensor Sensor `json:"-"`
|
||||||
|
Value float64 `json:"value" gorm:"<-:create;default:null"` // TODO on migrate set column NOT NULL
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s Sensor) IsValid() bool {
|
||||||
|
return s.SensorType.IsValid()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Position) Value() (driver.Value, error) {
|
||||||
|
return strconv.FormatInt(int64(p.X), 10) + "." + strconv.FormatInt(int64(p.Y), 10), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Position) Scan(value interface{}) error {
|
||||||
|
p.X = 0
|
||||||
|
p.Y = 0
|
||||||
|
p.NotNull = true
|
||||||
|
positionString, isString := value.(string)
|
||||||
|
if !isString {
|
||||||
|
return errors.New("invalid scan value")
|
||||||
|
}
|
||||||
|
positionArray := strings.Split(positionString, ".")
|
||||||
|
if len(positionArray) != 2 {
|
||||||
|
return errors.New("invalid string value")
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
var position int64
|
||||||
|
position, err = strconv.ParseInt(positionArray[0], 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("invalid x value")
|
||||||
|
}
|
||||||
|
p.X = int(position)
|
||||||
|
position, err = strconv.ParseInt(positionArray[1], 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("invalid y value")
|
||||||
|
}
|
||||||
|
p.Y = int(position)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// http -v POST localhost:8080/sensors name="sensor_$RANDOM" sensor_type:=1 position:='{"x":1, "y":2}'
|
||||||
|
// http -v POST localhost:8080/sensors name="sensor_$RANDOM" sensor_type:=2
|
||||||
|
func (m Model) AddSensor(sensor Sensor) (uint, error) {
|
||||||
|
sensor.ID = 0
|
||||||
|
sensor.Position.NotNull = true
|
||||||
|
if !sensor.SensorType.IsValid() {
|
||||||
|
return 0, errors.New("invalid sensor type")
|
||||||
|
}
|
||||||
|
result := m.app.Db().Create(&sensor)
|
||||||
|
return sensor.ID, result.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m Model) UpdateSensor(sensor Sensor, txSensor Sensor, tx *gorm.DB) (uint, error) {
|
||||||
|
sensor.ID = txSensor.ID
|
||||||
|
sensor.Position.NotNull = true
|
||||||
|
if !sensor.SensorType.IsValid() {
|
||||||
|
return 0, errors.New("invalid sensor type")
|
||||||
|
}
|
||||||
|
result := tx.Updates(&sensor)
|
||||||
|
return sensor.ID, result.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
// http -v POST localhost:8080/measurements sensor_id:=3 value:=32
|
||||||
|
// http -v GET localhost:8080/measurements/3
|
||||||
|
func (m Model) AddMeasurement(measurement Measurement) (uint, error) {
|
||||||
|
measurement.ID = 0
|
||||||
|
err := m.app.Db().First(&Sensor{}, measurement.SensorID).Error
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
result := m.app.Db().Create(&measurement)
|
||||||
|
return measurement.ID, result.Error
|
||||||
|
}
|
||||||
8
config/app.go
Normal file
8
config/app.go
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import "gelvin/app/application"
|
||||||
|
|
||||||
|
func configureApp() *application.Application {
|
||||||
|
app := application.NewApplication(Environment().Version, Environment().UrlRoot, Environment().Mode)
|
||||||
|
return app
|
||||||
|
}
|
||||||
33
config/config.go
Normal file
33
config/config.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gelvin/app/application"
|
||||||
|
"gelvin/app/controllers"
|
||||||
|
"gelvin/app/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
app *application.Application
|
||||||
|
controller *controllers.Controller
|
||||||
|
model *models.Model
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) App() *application.Application {
|
||||||
|
return c.app
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) Controller() *controllers.Controller {
|
||||||
|
return c.controller
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) Model() *models.Model {
|
||||||
|
return c.model
|
||||||
|
}
|
||||||
|
|
||||||
|
func Configure() *Config {
|
||||||
|
app := configureApp()
|
||||||
|
controller := controllers.NewController(app)
|
||||||
|
configureDb(app)
|
||||||
|
configureRoutes(app, controller)
|
||||||
|
return &Config{app, controller, controller.Model()}
|
||||||
|
}
|
||||||
9
config/database.go
Normal file
9
config/database.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gelvin/app/application"
|
||||||
|
)
|
||||||
|
|
||||||
|
func configureDb(app *application.Application) {
|
||||||
|
app.SqliteDB("./db")
|
||||||
|
}
|
||||||
51
config/environment.go
Normal file
51
config/environment.go
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gelvin/app/application"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
const EnvPrefix = "GELVIN_"
|
||||||
|
|
||||||
|
type environment struct {
|
||||||
|
Mode application.Mode
|
||||||
|
UrlRoot string
|
||||||
|
Version application.Version
|
||||||
|
}
|
||||||
|
|
||||||
|
func Environment() environment {
|
||||||
|
|
||||||
|
var mode application.Mode
|
||||||
|
switch getEnv("MODE") {
|
||||||
|
case "TEST":
|
||||||
|
mode = application.Test
|
||||||
|
case "PRODUCTION":
|
||||||
|
mode = application.Production
|
||||||
|
default:
|
||||||
|
mode = application.Debug
|
||||||
|
}
|
||||||
|
|
||||||
|
var urlRoot string
|
||||||
|
if urlRoot = getEnv("URL_ROOT"); urlRoot == "" {
|
||||||
|
urlRoot = "/"
|
||||||
|
}
|
||||||
|
|
||||||
|
version := parseVersion(getEnv("API_VERSION"))
|
||||||
|
return environment{
|
||||||
|
Mode: mode,
|
||||||
|
UrlRoot: urlRoot,
|
||||||
|
Version: version,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getEnv(key string) string {
|
||||||
|
return os.Getenv(EnvPrefix + key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseVersion(s string) application.Version {
|
||||||
|
version := application.Version{Major: 1}
|
||||||
|
if s != "" {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
return version
|
||||||
|
}
|
||||||
30
config/routes.go
Normal file
30
config/routes.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gelvin/app/application"
|
||||||
|
"gelvin/app/controllers"
|
||||||
|
"github.com/gin-contrib/cors"
|
||||||
|
)
|
||||||
|
|
||||||
|
func configureRoutes(app *application.Application, controller *controllers.Controller) {
|
||||||
|
root := app.UrlRoot()
|
||||||
|
router := app.Engine()
|
||||||
|
corsConfig := cors.DefaultConfig()
|
||||||
|
|
||||||
|
// cors
|
||||||
|
corsConfig.AllowOrigins = []string{
|
||||||
|
"http://localhost:8080",
|
||||||
|
}
|
||||||
|
router.Use(cors.New(corsConfig))
|
||||||
|
|
||||||
|
// sensors
|
||||||
|
router.GET(root+"sensors", controller.GetSensors)
|
||||||
|
router.GET(root+"sensors/:id", controller.GetSensor)
|
||||||
|
router.POST(root+"sensors", controller.AddSensor)
|
||||||
|
|
||||||
|
// measurements
|
||||||
|
router.GET(root+"measurements", controller.GetMeasurements)
|
||||||
|
router.GET(root+"measurements/:id", controller.GetMeasurement)
|
||||||
|
router.POST(root+"measurements", controller.AddMeasurement)
|
||||||
|
router.POST(root+"measurements/:id", controller.AddMeasurementID)
|
||||||
|
}
|
||||||
30
go.mod
Normal file
30
go.mod
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
module gelvin
|
||||||
|
|
||||||
|
go 1.17
|
||||||
|
|
||||||
|
require github.com/gin-gonic/gin v1.7.4
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/gin-contrib/cors v1.3.1 // indirect
|
||||||
|
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||||
|
github.com/go-playground/locales v0.14.0 // indirect
|
||||||
|
github.com/go-playground/universal-translator v0.18.0 // indirect
|
||||||
|
github.com/go-playground/validator/v10 v10.9.0 // indirect
|
||||||
|
github.com/golang/protobuf v1.5.2 // indirect
|
||||||
|
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||||
|
github.com/jinzhu/now v1.1.2 // indirect
|
||||||
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
|
github.com/leodido/go-urn v1.2.1 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.9 // indirect
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
|
github.com/ugorji/go/codec v1.2.6 // indirect
|
||||||
|
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa // indirect
|
||||||
|
golang.org/x/sys v0.0.0-20211113001501-0c823b97ae02 // indirect
|
||||||
|
golang.org/x/text v0.3.7 // indirect
|
||||||
|
google.golang.org/protobuf v1.26.0 // indirect
|
||||||
|
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
|
gorm.io/driver/sqlite v1.2.4 // indirect
|
||||||
|
gorm.io/gorm v1.22.3 // indirect
|
||||||
|
)
|
||||||
131
go.sum
Normal file
131
go.sum
Normal file
@@ -0,0 +1,131 @@
|
|||||||
|
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||||
|
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/gin-contrib/cors v1.3.1 h1:doAsuITavI4IOcd0Y19U4B+O0dNWihRyX//nn4sEmgA=
|
||||||
|
github.com/gin-contrib/cors v1.3.1/go.mod h1:jjEJ4268OPZUcU7k9Pm653S7lXUGcqMADzFA61xsmDk=
|
||||||
|
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.5.0/go.mod h1:Nd6IXA8m5kNZdNEHMBd93KT+mdY3+bewLgRvmCsR2Do=
|
||||||
|
github.com/gin-gonic/gin v1.7.4 h1:QmUZXrvJ9qZ3GfWvQ+2wnW/1ePrTEJqPKMYEU3lD/DM=
|
||||||
|
github.com/gin-gonic/gin v1.7.4/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
|
||||||
|
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
|
||||||
|
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||||
|
github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3ygWanZIBtBW0W2TM=
|
||||||
|
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
|
||||||
|
github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU=
|
||||||
|
github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs=
|
||||||
|
github.com/go-playground/universal-translator v0.16.0/go.mod h1:1AnU7NaIRDWWzGEKwgtJRd2xk99HeFyHw3yid4rvQIY=
|
||||||
|
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
|
||||||
|
github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho=
|
||||||
|
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
|
||||||
|
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
|
||||||
|
github.com/go-playground/validator/v10 v10.9.0 h1:NgTtmN58D0m8+UuxtYmGztBJB7VnPgjj221I1QHci2A=
|
||||||
|
github.com/go-playground/validator/v10 v10.9.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos=
|
||||||
|
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
|
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
|
||||||
|
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||||
|
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
|
||||||
|
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
|
||||||
|
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||||
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
|
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||||
|
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||||
|
github.com/jinzhu/now v1.1.2 h1:eVKgfIdy9b6zbWBMgFpfDPoAMifwSZagU9HmEU6zgiI=
|
||||||
|
github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||||
|
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||||
|
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||||
|
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||||
|
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||||
|
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||||
|
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||||
|
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
|
||||||
|
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
|
||||||
|
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
|
github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw=
|
||||||
|
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
|
||||||
|
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
|
||||||
|
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
|
||||||
|
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
|
||||||
|
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||||
|
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
|
||||||
|
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.9 h1:10HX2Td0ocZpYEjhilsuo6WWtUqttj2Kb0KtD86/KYA=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
|
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||||
|
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||||
|
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||||
|
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
||||||
|
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/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
|
||||||
|
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
|
||||||
|
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
|
||||||
|
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/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||||
|
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
|
||||||
|
github.com/ugorji/go v1.2.6 h1:tGiWC9HENWE2tqYycIqFTNorMmFRVhNwCpDOpWqnk8E=
|
||||||
|
github.com/ugorji/go v1.2.6/go.mod h1:anCg0y61KIhDlPZmnH+so+RQbysYVyDko0IMgJv0Nn0=
|
||||||
|
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
|
||||||
|
github.com/ugorji/go/codec v1.2.6 h1:7kbGefxLoDBuYXOms4yD7223OpNMMPNPZxXk5TvFcyQ=
|
||||||
|
github.com/ugorji/go/codec v1.2.6/go.mod h1:V6TCNZ4PHqoHGFZuSG1W8nrCzzdgA2DozYxWFFpvxTw=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
|
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
|
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa h1:idItI2DDfCokpg0N51B2VtiLdJ4vAuXC9fnCb2gACo4=
|
||||||
|
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
|
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-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
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/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20211113001501-0c823b97ae02 h1:7NCfEGl0sfUojmX78nK9pBJuUlSZWEJA/TwASvfiPLo=
|
||||||
|
golang.org/x/sys v0.0.0-20211113001501-0c823b97ae02/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
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/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
||||||
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||||
|
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
|
||||||
|
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||||
|
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||||
|
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
|
||||||
|
gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
|
||||||
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||||
|
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gorm.io/driver/sqlite v1.2.4 h1:jx16ESo1WzNjgBJNSbhEDoMKJnlhkU8BuBR2C0GC7D8=
|
||||||
|
gorm.io/driver/sqlite v1.2.4/go.mod h1:n8/CTEIEmo7lKrehQI4pd+rz6O514tMkBeCAR5UTXLs=
|
||||||
|
gorm.io/gorm v1.22.2/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
|
||||||
|
gorm.io/gorm v1.22.3 h1:/JS6z+GStEQvJNW3t1FTwJwG/gZ+A7crFdRqtvG5ehA=
|
||||||
|
gorm.io/gorm v1.22.3/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
|
||||||
31
main.go
Normal file
31
main.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"gelvin/app/models"
|
||||||
|
"gelvin/config"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
address := "0.0.0.0:8081"
|
||||||
|
if len(os.Args) > 1 {
|
||||||
|
address = os.Args[1]
|
||||||
|
}
|
||||||
|
config := config.Configure()
|
||||||
|
config.App().OpenDB()
|
||||||
|
defer config.App().CloseDB()
|
||||||
|
fmt.Println("Migration:", models.Migrate(config.App()))
|
||||||
|
config.Model().AddSensor(models.Sensor{
|
||||||
|
ID: 0,
|
||||||
|
CreatedAt: time.Time{},
|
||||||
|
UpdatedAt: time.Time{},
|
||||||
|
DeletedAt: gorm.DeletedAt{},
|
||||||
|
Name: "asd",
|
||||||
|
SensorType: 4,
|
||||||
|
Position: models.Position{},
|
||||||
|
})
|
||||||
|
fmt.Println(config.App().Run(address))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user