entity.go
сущностей пакета
тип Структура устройства {
Id int
Name string
}
модели.go
package models
import (
"log"
"net/http"
entities "../entities"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
var devices [] entity. Deevice
тип Модель struct {}
func HttpInfo (r * http.Request) {
fmt.Printf("%s\t %s\t %s%s\r\n", r.Method, r.Proto, r.Host, r.URL)
}
func (c Model) getDevices (db * gorm.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
setJsonHeader(w)
HttpInfo(r)
var devices entities.Device
if err := db.Find(&devices).Error; err != nil {
fmt.Println(err)
} else {
json.NewEncoder(w).Encode(devices)
}
}
}
main.go
package main
импорт (
"log"
"net/http"
entities "./src/entities"
models "./src/models"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
var devices [] entity.evice
var db * gorm.DB
ошибка var var
func main () {
// Handle Subsequent requests
fmt.Println("Api running on port 4000...")
r := mux.NewRouter().StrictSlash(true)
r.HandleFunc("/devices", model.getDevices(db)).Methods("GET")
r.HandleFunc("/devices/{id}", model.getDevice).Methods("GET")
log.Fatal(http.ListenAndServe(":4000", handlers.CORS(headers, methods, origins)(r)))
}