sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0

update schema

This commit is contained in:
Martin Geno 2016-08-20 01:17:08 +02:00
parent f3305a6054
commit 8c3b114428
4 changed files with 88 additions and 59 deletions

View File

@ -16,11 +16,11 @@ type ErrorResult struct {
type JsonResult struct {
Data interface{} `json:"data"`
Error *ErrorResult `json:"error,omitemty"`
Error *ErrorResult `json:"error,omitempty"`
Session struct {
Login interface{} `json:"login"`
Profil map[string]interface{} `json:"profil"`
} `json:"session"`
Login interface{} `json:"login,omitempty"`
Profil map[string]interface{} `json:"profil,omitempty"`
} `json:"session,omitempty"`
}
func JsonOutput(w http.ResponseWriter, r *http.Request, sess session.Session, data interface{}, errorresult *ErrorResult) {

11
main.go
View File

@ -8,9 +8,9 @@ import (
"github.com/NYTimes/gziphandler"
"github.com/astaxie/session"
_ "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/lib/pq"
"github.com/rs/cors"
libconfig "dev.sum7.de/sum7/warehost/config"
@ -21,7 +21,7 @@ import (
var (
configFile string
config *libconfig.Config
dbconnection *xorm.Engine
dbconnection *gorm.DB
sessions *session.Manager
)
@ -37,12 +37,13 @@ func main() {
go sessions.GC()
// Main Databaseconnection
dbconnection, err = xorm.NewEngine("postgres", config.Database)
dbconnection, err = gorm.Open("postgres", config.Database)
if err != nil {
log.Log.Fatal("database connection: ", err)
}
defer dbconnection.Close()
dbconnection.ShowSQL(config.DatabaseDebug)
dbconnection.SingularTable(true)
dbconnection.LogMode(config.DatabaseDebug)
//load system Models to database
system.SyncModels(dbconnection)

View File

@ -5,7 +5,7 @@ import (
"net/http"
"github.com/astaxie/session"
"github.com/go-xorm/xorm"
"github.com/jinzhu/gorm"
"github.com/julienschmidt/httprouter"
libconfig "dev.sum7.de/sum7/warehost/config"
@ -21,12 +21,12 @@ const MODULNAME = "system"
type API struct {
config *libconfig.Config
sessions *session.Manager
dbconnection *xorm.Engine
dbconnection *gorm.DB
log *log.ModulLog
}
// 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{
config: config,
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.POST(prefix+"/password", LoginHandler(api.Password, 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
func (api *API) Status(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := api.log.GetLog(r, "status")
result, err := api.dbconnection.Count(new(Login))
if err != nil {
returnerr = &libapi.ErrorResult{Message: "Affe"}
logger.Error("get login count: ", err)
} else {
var result int64
api.dbconnection.Model(&Login{}).Count(&result)
if result > 0 {
returndata = true
}
}
logger.Info("status")
return
}
@ -87,15 +87,7 @@ func (api *API) Login(w http.ResponseWriter, r *http.Request, _ httprouter.Param
}
logger = logger.WithField("user", requestlogin.Username)
var login = Login{Username: requestlogin.Username}
_, err = api.dbconnection.Get(&login)
if err != nil {
logger.Error("fetch database")
returnerr = &libapi.ErrorResult{
Message: "Internal Request Error",
}
returndata = false
return
}
api.dbconnection.Where("mail = ?", requestlogin.Username).First(&login)
if login.ID <= 0 {
logger.Warn("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
}
login.Password = libpassword.NewHesh(changePasswordRequest.NewPassword)
api.dbconnection.Update(login)
api.dbconnection.Save(login)
sess.Set("login", *login)
returndata = true
logger.Info("works")
@ -163,3 +155,32 @@ func (api *API) Delete(w http.ResponseWriter, r *http.Request, _ httprouter.Para
returndata = true
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
}

View File

@ -6,7 +6,7 @@ import (
log "dev.sum7.de/sum7/warehost/lib/log"
libpassword "dev.sum7.de/sum7/warehost/lib/password"
"github.com/go-xorm/xorm"
"github.com/jinzhu/gorm"
)
// MINPASSWORDLENTH to validate password
@ -34,38 +34,45 @@ type ChangePasswordRequest struct {
// Login found
type Login struct {
ID int64 `xorm:"'id'" json:"id"`
Username string `xorm:"varchar(255) not null unique 'mail'" json:"username"`
Password string `xorm:"varchar(255) not null 'password'" json:"-"`
Active bool `xorm:"boolean default false 'active'" json:"active"`
Code string `xorm:"varchar(255) 'code'" json:"-"`
Superadmin bool `xorm:"boolean default false 'superadmin'" json:"superadmin"`
CreateAt time.Time `xorm:"timestampz 'createat'" json:"createat"`
LastLoginAt time.Time `xorm:"timestampz 'lastloginat'" json:"lastloginat"`
gorm.Model
Username string `gorm:"type:varchar(255);unique;column:mail" json:"username"`
Password string `gorm:"type:varchar(255);column:password" json:"-"`
Active bool `gorm:"column:active" json:"active"`
Code string `gorm:"type:varchar(255);column:code" json:"-"`
Superadmin bool `gorm:"column:superadmin" json:"superadmin"`
CreateAt time.Time `gorm:"column:createat" json:"createat"`
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
func SyncModels(dbconnection *xorm.Engine) {
err := dbconnection.Sync(new(Login))
if err != nil {
log.Log.Fatal("create table \"login\" ", err)
}
result, err := dbconnection.Count(new(Login))
if err != nil {
log.Log.Error("get \"login\" count ", err)
}
func SyncModels(dbconnection *gorm.DB) {
dbconnection.AutoMigrate(&Login{}, &Invite{})
var result int64
dbconnection.Model(&Login{}).Count(&result)
if result <= 0 {
login := new(Login)
login.Username = "root"
login.Password = libpassword.NewHesh("root")
login.CreateAt = time.Now()
login.Active = true
login.Superadmin = true
_, err := dbconnection.Insert(login)
if err == nil {
log.Log.Warn("Create user \"root\"")
} else {
log.Log.Fatal("cound not first user \"root\" ", err)
login := &Login{
Username: "root",
Active: true,
Superadmin: true,
Password: libpassword.NewHesh("root"),
CreateAt: time.Now(),
}
dbconnection.Create(login)
log.Log.Error("have to create \"login\"")
} else {
log.Log.Info("Conection to \"login\" works")
}
}