update schema
This commit is contained in:
parent
f3305a6054
commit
8c3b114428
|
@ -16,11 +16,11 @@ type ErrorResult struct {
|
||||||
|
|
||||||
type JsonResult struct {
|
type JsonResult struct {
|
||||||
Data interface{} `json:"data"`
|
Data interface{} `json:"data"`
|
||||||
Error *ErrorResult `json:"error,omitemty"`
|
Error *ErrorResult `json:"error,omitempty"`
|
||||||
Session struct {
|
Session struct {
|
||||||
Login interface{} `json:"login"`
|
Login interface{} `json:"login,omitempty"`
|
||||||
Profil map[string]interface{} `json:"profil"`
|
Profil map[string]interface{} `json:"profil,omitempty"`
|
||||||
} `json:"session"`
|
} `json:"session,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func JsonOutput(w http.ResponseWriter, r *http.Request, sess session.Session, data interface{}, errorresult *ErrorResult) {
|
func JsonOutput(w http.ResponseWriter, r *http.Request, sess session.Session, data interface{}, errorresult *ErrorResult) {
|
||||||
|
|
11
main.go
11
main.go
|
@ -8,9 +8,9 @@ import (
|
||||||
"github.com/NYTimes/gziphandler"
|
"github.com/NYTimes/gziphandler"
|
||||||
"github.com/astaxie/session"
|
"github.com/astaxie/session"
|
||||||
_ "github.com/astaxie/session/providers/memory"
|
_ "github.com/astaxie/session/providers/memory"
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/jinzhu/gorm"
|
||||||
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
_ "github.com/lib/pq"
|
|
||||||
"github.com/rs/cors"
|
"github.com/rs/cors"
|
||||||
|
|
||||||
libconfig "dev.sum7.de/sum7/warehost/config"
|
libconfig "dev.sum7.de/sum7/warehost/config"
|
||||||
|
@ -21,7 +21,7 @@ import (
|
||||||
var (
|
var (
|
||||||
configFile string
|
configFile string
|
||||||
config *libconfig.Config
|
config *libconfig.Config
|
||||||
dbconnection *xorm.Engine
|
dbconnection *gorm.DB
|
||||||
sessions *session.Manager
|
sessions *session.Manager
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -37,12 +37,13 @@ func main() {
|
||||||
go sessions.GC()
|
go sessions.GC()
|
||||||
|
|
||||||
// Main Databaseconnection
|
// Main Databaseconnection
|
||||||
dbconnection, err = xorm.NewEngine("postgres", config.Database)
|
dbconnection, err = gorm.Open("postgres", config.Database)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Log.Fatal("database connection: ", err)
|
log.Log.Fatal("database connection: ", err)
|
||||||
}
|
}
|
||||||
defer dbconnection.Close()
|
defer dbconnection.Close()
|
||||||
dbconnection.ShowSQL(config.DatabaseDebug)
|
dbconnection.SingularTable(true)
|
||||||
|
dbconnection.LogMode(config.DatabaseDebug)
|
||||||
//load system Models to database
|
//load system Models to database
|
||||||
system.SyncModels(dbconnection)
|
system.SyncModels(dbconnection)
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/astaxie/session"
|
"github.com/astaxie/session"
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/jinzhu/gorm"
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
|
|
||||||
libconfig "dev.sum7.de/sum7/warehost/config"
|
libconfig "dev.sum7.de/sum7/warehost/config"
|
||||||
|
@ -21,12 +21,12 @@ const MODULNAME = "system"
|
||||||
type API struct {
|
type API struct {
|
||||||
config *libconfig.Config
|
config *libconfig.Config
|
||||||
sessions *session.Manager
|
sessions *session.Manager
|
||||||
dbconnection *xorm.Engine
|
dbconnection *gorm.DB
|
||||||
log *log.ModulLog
|
log *log.ModulLog
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewAPI sets the routes to the api functions
|
// NewAPI sets the routes to the api functions
|
||||||
func NewAPI(config *libconfig.Config, sessions *session.Manager, dbconnection *xorm.Engine, router *httprouter.Router, prefix string) {
|
func NewAPI(config *libconfig.Config, sessions *session.Manager, dbconnection *gorm.DB, router *httprouter.Router, prefix string) {
|
||||||
api := &API{
|
api := &API{
|
||||||
config: config,
|
config: config,
|
||||||
sessions: sessions,
|
sessions: sessions,
|
||||||
|
@ -38,21 +38,21 @@ func NewAPI(config *libconfig.Config, sessions *session.Manager, dbconnection *x
|
||||||
router.GET(prefix+"/logout", LoginHandler(api.Logout, sessions))
|
router.GET(prefix+"/logout", LoginHandler(api.Logout, sessions))
|
||||||
router.POST(prefix+"/password", LoginHandler(api.Password, sessions))
|
router.POST(prefix+"/password", LoginHandler(api.Password, sessions))
|
||||||
router.GET(prefix+"/delete", LoginHandler(api.Delete, sessions))
|
router.GET(prefix+"/delete", LoginHandler(api.Delete, sessions))
|
||||||
|
router.GET(prefix+"/invite", LoginHandler(api.InviteList, sessions))
|
||||||
|
router.POST(prefix+"/invite", LoginHandler(api.InviteAdd, sessions))
|
||||||
|
router.PUT(prefix+"/invite", LoginHandler(api.InviteEdit, sessions))
|
||||||
|
router.DELETE(prefix+"/invite", LoginHandler(api.InviteDelete, sessions))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Status to get Login and Server status
|
// Status to get Login and Server status
|
||||||
func (api *API) Status(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func (api *API) Status(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "status")
|
logger := api.log.GetLog(r, "status")
|
||||||
result, err := api.dbconnection.Count(new(Login))
|
var result int64
|
||||||
if err != nil {
|
api.dbconnection.Model(&Login{}).Count(&result)
|
||||||
returnerr = &libapi.ErrorResult{Message: "Affe"}
|
|
||||||
logger.Error("get login count: ", err)
|
|
||||||
} else {
|
|
||||||
if result > 0 {
|
if result > 0 {
|
||||||
returndata = true
|
returndata = true
|
||||||
}
|
}
|
||||||
}
|
|
||||||
logger.Info("status")
|
logger.Info("status")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -87,15 +87,7 @@ func (api *API) Login(w http.ResponseWriter, r *http.Request, _ httprouter.Param
|
||||||
}
|
}
|
||||||
logger = logger.WithField("user", requestlogin.Username)
|
logger = logger.WithField("user", requestlogin.Username)
|
||||||
var login = Login{Username: requestlogin.Username}
|
var login = Login{Username: requestlogin.Username}
|
||||||
_, err = api.dbconnection.Get(&login)
|
api.dbconnection.Where("mail = ?", requestlogin.Username).First(&login)
|
||||||
if err != nil {
|
|
||||||
logger.Error("fetch database")
|
|
||||||
returnerr = &libapi.ErrorResult{
|
|
||||||
Message: "Internal Request Error",
|
|
||||||
}
|
|
||||||
returndata = false
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if login.ID <= 0 {
|
if login.ID <= 0 {
|
||||||
logger.Warn("user not found")
|
logger.Warn("user not found")
|
||||||
returnerr = &libapi.ErrorResult{Fields: []string{"username"}, Message: "User not Found"}
|
returnerr = &libapi.ErrorResult{Fields: []string{"username"}, Message: "User not Found"}
|
||||||
|
@ -147,7 +139,7 @@ func (api *API) Password(w http.ResponseWriter, r *http.Request, _ httprouter.Pa
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
login.Password = libpassword.NewHesh(changePasswordRequest.NewPassword)
|
login.Password = libpassword.NewHesh(changePasswordRequest.NewPassword)
|
||||||
api.dbconnection.Update(login)
|
api.dbconnection.Save(login)
|
||||||
sess.Set("login", *login)
|
sess.Set("login", *login)
|
||||||
returndata = true
|
returndata = true
|
||||||
logger.Info("works")
|
logger.Info("works")
|
||||||
|
@ -163,3 +155,32 @@ func (api *API) Delete(w http.ResponseWriter, r *http.Request, _ httprouter.Para
|
||||||
returndata = true
|
returndata = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (api *API) InviteList(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
logger := api.log.GetLog(r, "invitelist")
|
||||||
|
logger.Warn("not implemented")
|
||||||
|
api.dbconnection.Model(login).Preload("Invites.Invited").First(login)
|
||||||
|
returndata = login.Invites
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (api *API) InviteAdd(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
logger := api.log.GetLog(r, "invitelist")
|
||||||
|
logger.Warn("not implemented")
|
||||||
|
returndata = false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (api *API) InviteEdit(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
logger := api.log.GetLog(r, "invitelist")
|
||||||
|
logger.Warn("not implemented")
|
||||||
|
returndata = false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (api *API) InviteDelete(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
logger := api.log.GetLog(r, "invitelist")
|
||||||
|
logger.Warn("not implemented")
|
||||||
|
returndata = false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
log "dev.sum7.de/sum7/warehost/lib/log"
|
log "dev.sum7.de/sum7/warehost/lib/log"
|
||||||
libpassword "dev.sum7.de/sum7/warehost/lib/password"
|
libpassword "dev.sum7.de/sum7/warehost/lib/password"
|
||||||
|
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/jinzhu/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MINPASSWORDLENTH to validate password
|
// MINPASSWORDLENTH to validate password
|
||||||
|
@ -34,38 +34,45 @@ type ChangePasswordRequest struct {
|
||||||
|
|
||||||
// Login found
|
// Login found
|
||||||
type Login struct {
|
type Login struct {
|
||||||
ID int64 `xorm:"'id'" json:"id"`
|
gorm.Model
|
||||||
Username string `xorm:"varchar(255) not null unique 'mail'" json:"username"`
|
Username string `gorm:"type:varchar(255);unique;column:mail" json:"username"`
|
||||||
Password string `xorm:"varchar(255) not null 'password'" json:"-"`
|
Password string `gorm:"type:varchar(255);column:password" json:"-"`
|
||||||
Active bool `xorm:"boolean default false 'active'" json:"active"`
|
Active bool `gorm:"column:active" json:"active"`
|
||||||
Code string `xorm:"varchar(255) 'code'" json:"-"`
|
Code string `gorm:"type:varchar(255);column:code" json:"-"`
|
||||||
Superadmin bool `xorm:"boolean default false 'superadmin'" json:"superadmin"`
|
Superadmin bool `gorm:"column:superadmin" json:"superadmin"`
|
||||||
CreateAt time.Time `xorm:"timestampz 'createat'" json:"createat"`
|
CreateAt time.Time `gorm:"column:createat" json:"createat"`
|
||||||
LastLoginAt time.Time `xorm:"timestampz 'lastloginat'" json:"lastloginat"`
|
LastLoginAt time.Time `gorm:"column:lastloginat" json:"lastloginat"`
|
||||||
|
Invites []Invite `gorm:"foreignkey:Login"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Login found
|
||||||
|
type Invite struct {
|
||||||
|
gorm.Model
|
||||||
|
LoginID uint `sql:"type:bigint REFERENCES login(id);column:login"`
|
||||||
|
Login Login `gorm:"column:login" json:"login"`
|
||||||
|
InvitedID uint `sql:"type:bigint REFERENCES login(id);column:invited"`
|
||||||
|
Invited Login `gorm:"column:invited" json:"invited"`
|
||||||
|
Admin bool `json:"admin"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// SyncModels to verify the database schema
|
// SyncModels to verify the database schema
|
||||||
func SyncModels(dbconnection *xorm.Engine) {
|
func SyncModels(dbconnection *gorm.DB) {
|
||||||
err := dbconnection.Sync(new(Login))
|
dbconnection.AutoMigrate(&Login{}, &Invite{})
|
||||||
if err != nil {
|
var result int64
|
||||||
log.Log.Fatal("create table \"login\" ", err)
|
dbconnection.Model(&Login{}).Count(&result)
|
||||||
}
|
|
||||||
result, err := dbconnection.Count(new(Login))
|
|
||||||
if err != nil {
|
|
||||||
log.Log.Error("get \"login\" count ", err)
|
|
||||||
}
|
|
||||||
if result <= 0 {
|
if result <= 0 {
|
||||||
login := new(Login)
|
login := &Login{
|
||||||
login.Username = "root"
|
Username: "root",
|
||||||
login.Password = libpassword.NewHesh("root")
|
Active: true,
|
||||||
login.CreateAt = time.Now()
|
Superadmin: true,
|
||||||
login.Active = true
|
Password: libpassword.NewHesh("root"),
|
||||||
login.Superadmin = true
|
CreateAt: time.Now(),
|
||||||
_, err := dbconnection.Insert(login)
|
}
|
||||||
if err == nil {
|
|
||||||
log.Log.Warn("Create user \"root\"")
|
dbconnection.Create(login)
|
||||||
|
log.Log.Error("have to create \"login\"")
|
||||||
} else {
|
} else {
|
||||||
log.Log.Fatal("cound not first user \"root\" ", err)
|
log.Log.Info("Conection to \"login\" works")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue