sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
warehost/modul/host/apiweb.go

152 lines
4.0 KiB
Go

package host
import (
"net/http"
"strconv"
"strings"
"goji.io/pat"
"golang.org/x/net/context"
libapi "dev.sum7.eu/sum7/warehost/lib/api"
system "dev.sum7.eu/sum7/warehost/system"
)
func getWeb(ctx context.Context, w http.ResponseWriter) (web Web, returnerr *libapi.ErrorResult) {
login := ctx.Value("login").(*system.Login)
profil := ctx.Value("profil").(*Profil)
id, err := strconv.ParseInt(pat.Param(ctx, "webid"), 10, 64)
if err != nil {
returnerr = &libapi.ErrorResult{
Message: "Internal Request Error",
}
w.WriteHeader(http.StatusBadRequest)
return
}
if login.Superadmin {
dbconnection.Where("id = ?", id).Find(&web)
} else {
dbconnection.Where(map[string]int64{"ID": id, "domain.profil": profil.ID}).Find(&web)
}
if web.ID <= 0 {
returnerr = &libapi.ErrorResult{Fields: []string{"web"}, Message: "not found"}
w.WriteHeader(http.StatusNotFound)
}
return
}
func webList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := log.GetLog(r, "weblist")
var web []*Web
domain, returnerr := getDomain(ctx, w)
if returnerr != nil {
logger.Info("not found")
return
}
dbconnection.Where("domain = ?", domain.ID).Preload("Domain").Find(&web)
logger.Info("done")
returndata = web
return
}
func webAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := log.GetLog(r, "webadd")
var webRequest Web
returnerr = libapi.JSONDecoder(r.Body, &webRequest, w, logger)
if returnerr != nil {
return
}
domain, returnerr := getDomain(ctx, w)
if returnerr != nil {
logger.Info("not found")
return
}
web := &Web{
DomainID: domain.ID,
Subdomain: webRequest.Subdomain,
PHP: webRequest.PHP,
SSL: webRequest.SSL,
SSLRedirect: webRequest.SSLRedirect,
Redirect: webRequest.Redirect,
Proxy: webRequest.Proxy,
//FTP: webRequest.FTP,
//HTTPAccess: webRequest.HTTPAccess,
}
if err := dbconnection.Create(web).Error; err != nil {
if strings.Contains(err.Error(), "duplicate key") {
logger.Warning("exists already")
return
}
logger.Error("database: during create host web: ", err)
w.WriteHeader(http.StatusInternalServerError)
returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"}
return
}
returndata = true
logger.Info("done")
return
}
func webEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := log.GetLog(r, "webedit")
web, returnerr := getWeb(ctx, w)
if returnerr != nil {
logger.Info("not found")
return
}
var webRequest Web
returnerr = libapi.JSONDecoder(r.Body, &webRequest, w, logger)
if returnerr != nil {
return
}
web.Subdomain = webRequest.Subdomain
web.PHP = webRequest.PHP
web.SSL = webRequest.SSL
web.SSLRedirect = webRequest.SSLRedirect
web.Redirect = webRequest.Redirect
web.Proxy = webRequest.Proxy
//web.FTP = webRequest.FTP
//web.HTTPAccess = webRequest.HTTPAccess
if err := dbconnection.Save(web).Error; err != nil {
logger.Error("database: during modify host web: ", err)
w.WriteHeader(http.StatusInternalServerError)
returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"}
return
}
returndata = true
logger.Info("done")
return
}
func webDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := log.GetLog(r, "webdelete")
webRequest, returnerr := getWeb(ctx, w)
if returnerr != nil {
logger.Info("not found")
return
}
if err := dbconnection.Unscoped().Delete(webRequest).Error; err != nil {
logger.Error("database: during create host web: ", err)
w.WriteHeader(http.StatusInternalServerError)
returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"}
return
}
returndata = true
logger.Info("done")
return
}