mirror of
https://github.com/documize/community.git
synced 2025-07-18 20:59:43 +02:00
Move over to Go embed directive
This commit is contained in:
parent
cddba799f8
commit
470e2d3ecf
18 changed files with 148 additions and 25205 deletions
62
build.sh
62
build.sh
|
@ -12,44 +12,36 @@ ember build ---environment=production --output-path dist-prod --suppress-sizes t
|
|||
cd ..
|
||||
|
||||
echo "Copying Ember assets..."
|
||||
rm -rf embed/bindata/public
|
||||
mkdir -p embed/bindata/public
|
||||
cp -r gui/dist-prod/assets embed/bindata/public
|
||||
cp -r gui/dist-prod/codemirror embed/bindata/public/codemirror
|
||||
cp -r gui/dist-prod/prism embed/bindata/public/prism
|
||||
cp -r gui/dist-prod/sections embed/bindata/public/sections
|
||||
cp -r gui/dist-prod/tinymce embed/bindata/public/tinymce
|
||||
cp -r gui/dist-prod/pdfjs embed/bindata/public/pdfjs
|
||||
cp gui/dist-prod/*.* embed/bindata
|
||||
cp gui/dist-prod/favicon.ico embed/bindata/public
|
||||
cp gui/dist-prod/manifest.json embed/bindata/public
|
||||
rm -rf edition/static/public
|
||||
mkdir -p edition/static/public
|
||||
cp -r gui/dist-prod/assets edition/static/public
|
||||
cp -r gui/dist-prod/codemirror edition/static/public/codemirror
|
||||
cp -r gui/dist-prod/prism edition/static/public/prism
|
||||
cp -r gui/dist-prod/sections edition/static/public/sections
|
||||
cp -r gui/dist-prod/tinymce edition/static/public/tinymce
|
||||
cp -r gui/dist-prod/pdfjs edition/static/public/pdfjs
|
||||
cp gui/dist-prod/*.* edition/static
|
||||
cp gui/dist-prod/favicon.ico edition/static/public
|
||||
cp gui/dist-prod/manifest.json edition/static/public
|
||||
|
||||
rm -rf embed/bindata/mail
|
||||
mkdir -p embed/bindata/mail
|
||||
cp domain/mail/*.html embed/bindata/mail
|
||||
cp core/database/templates/*.html embed/bindata
|
||||
rm -rf edition/static/mail
|
||||
mkdir -p edition/static/mail
|
||||
cp domain/mail/*.html edition/static/mail
|
||||
cp core/database/templates/*.html edition/static
|
||||
|
||||
rm -rf embed/bindata/scripts
|
||||
mkdir -p embed/bindata/scripts
|
||||
mkdir -p embed/bindata/scripts/mysql
|
||||
mkdir -p embed/bindata/scripts/postgresql
|
||||
mkdir -p embed/bindata/scripts/sqlserver
|
||||
cp -r core/database/scripts/mysql/*.sql embed/bindata/scripts/mysql
|
||||
cp -r core/database/scripts/postgresql/*.sql embed/bindata/scripts/postgresql
|
||||
cp -r core/database/scripts/sqlserver/*.sql embed/bindata/scripts/sqlserver
|
||||
rm -rf edition/static/scripts
|
||||
mkdir -p edition/static/scripts
|
||||
mkdir -p edition/static/scripts/mysql
|
||||
mkdir -p edition/static/scripts/postgresql
|
||||
mkdir -p edition/static/scripts/sqlserver
|
||||
cp -r core/database/scripts/mysql/*.sql edition/static/scripts/mysql
|
||||
cp -r core/database/scripts/postgresql/*.sql edition/static/scripts/postgresql
|
||||
cp -r core/database/scripts/sqlserver/*.sql edition/static/scripts/sqlserver
|
||||
|
||||
rm -rf embed/bindata/onboard
|
||||
mkdir -p embed/bindata/onboard
|
||||
cp -r domain/onboard/*.json embed/bindata/onboard
|
||||
rm -rf edition/static/onboard
|
||||
mkdir -p edition/static/onboard
|
||||
cp -r domain/onboard/*.json edition/static/onboard
|
||||
|
||||
echo "Generating in-memory static assets..."
|
||||
export PATH=$PATH:~/go/bin
|
||||
# go get -u github.com/jteeuwen/go-bindata/...
|
||||
# go get -u github.com/elazarl/go-bindata-assetfs/...
|
||||
cd embed
|
||||
go generate
|
||||
|
||||
cd ..
|
||||
echo "Compiling for Linux..."
|
||||
env GOOS=linux GOARCH=amd64 go build -mod=vendor -trimpath -o bin/documize-community-linux-amd64 ./edition/community.go
|
||||
echo "Compiling for macOS Intel..."
|
||||
|
@ -68,5 +60,3 @@ echo "Finished."
|
|||
# CGO_ENABLED=0 GOOS=linux go build -a -ldflags="-s -w" -installsuffix cgo
|
||||
# go build -ldflags '-d -s -w' -a -tags netgo -installsuffix netgo test.go
|
||||
# ldd test
|
||||
|
||||
# https://stackoverflow.com/questions/55664630/how-do-i-migrate-from-dep-to-go-modules
|
||||
|
|
70
core/asset/assets.go
Normal file
70
core/asset/assets.go
Normal file
|
@ -0,0 +1,70 @@
|
|||
package asset
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"errors"
|
||||
"io"
|
||||
"io/fs"
|
||||
"mime"
|
||||
"net/http"
|
||||
"path"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// GetPublicFileSystem
|
||||
func GetPublicFileSystem(e embed.FS) (hfs http.FileSystem, err error) {
|
||||
fsys, err := fs.Sub(e, "static/public")
|
||||
if err != nil {
|
||||
return nil, errors.New("failed GetPublicFileSystem")
|
||||
}
|
||||
|
||||
return http.FS(fsys), nil
|
||||
}
|
||||
|
||||
// FetchStatic loads static asset from embed file system.
|
||||
func FetchStatic(e embed.FS, filename string) (content, contentType string, err error) {
|
||||
data, err := e.ReadFile("static/" + filename)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
contentType = mime.TypeByExtension(filepath.Ext(filename))
|
||||
content = string(data)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// FetchStaticDir returns filenames within specified directory
|
||||
func FetchStaticDir(fs embed.FS, directory string) (files []string, err error) {
|
||||
entries, err := fs.ReadDir("static/" + directory)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for i := range entries {
|
||||
if !entries[i].Type().IsDir() {
|
||||
files = append(files, entries[i].Name())
|
||||
}
|
||||
}
|
||||
|
||||
return files, nil
|
||||
}
|
||||
|
||||
// WriteStatic loads static asset from embed file system and writes to HTTP.
|
||||
func WriteStatic(fs embed.FS, prefix, requestedPath string, w http.ResponseWriter) error {
|
||||
f, err := fs.Open(path.Join(prefix, requestedPath))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
stat, _ := f.Stat()
|
||||
if stat.IsDir() {
|
||||
return errors.New("cannot write static file")
|
||||
}
|
||||
|
||||
contentType := mime.TypeByExtension(filepath.Ext(requestedPath))
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
_, err = io.Copy(w, f)
|
||||
return err
|
||||
}
|
|
@ -26,7 +26,7 @@ func InstallUpgrade(runtime *env.Runtime, existingDB bool) (err error) {
|
|||
// amLeader := false
|
||||
|
||||
// Get all SQL scripts.
|
||||
scripts, err := LoadScripts()
|
||||
scripts, err := LoadScripts(runtime)
|
||||
if err != nil {
|
||||
runtime.Log.Error("Database: unable to load scripts", err)
|
||||
return
|
||||
|
|
|
@ -12,11 +12,12 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/documize/community/core/asset"
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/server/web"
|
||||
)
|
||||
|
||||
// Scripts holds all .SQL files for all supported database providers.
|
||||
|
@ -33,21 +34,19 @@ type Script struct {
|
|||
}
|
||||
|
||||
// LoadScripts returns .SQL scripts for supported database providers.
|
||||
func LoadScripts() (s Scripts, err error) {
|
||||
assetDir := "bindata/scripts"
|
||||
|
||||
func LoadScripts(runtime *env.Runtime) (s Scripts, err error) {
|
||||
// MySQL
|
||||
s.MySQL, err = loadFiles(fmt.Sprintf("%s/mysql", assetDir))
|
||||
s.MySQL, err = loadFiles(runtime.Assets, "scripts/mysql")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// PostgreSQL
|
||||
s.PostgreSQL, err = loadFiles(fmt.Sprintf("%s/postgresql", assetDir))
|
||||
s.PostgreSQL, err = loadFiles(runtime.Assets, "scripts/postgresql")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// PostgreSQL
|
||||
s.SQLServer, err = loadFiles(fmt.Sprintf("%s/sqlserver", assetDir))
|
||||
s.SQLServer, err = loadFiles(runtime.Assets, "scripts/sqlserver")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -70,20 +69,22 @@ func SpecificScripts(runtime *env.Runtime, all Scripts) (s []Script) {
|
|||
}
|
||||
|
||||
// loadFiles returns all SQL scripts in specified folder as [][]byte.
|
||||
func loadFiles(path string) (b []Script, err error) {
|
||||
buf := []byte{}
|
||||
scripts, err := web.AssetDir(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
sort.Strings(scripts)
|
||||
for _, file := range scripts {
|
||||
buf, err = web.Asset(fmt.Sprintf("%s/%s", path, file))
|
||||
func loadFiles(fs embed.FS, path string) (b []Script, err error) {
|
||||
scripts, err := asset.FetchStaticDir(fs, path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
b = append(b, Script{Version: extractVersionNumber(file), Script: buf})
|
||||
sort.Strings(scripts)
|
||||
|
||||
for i := range scripts {
|
||||
filename := scripts[i]
|
||||
sqlfile, _, err := asset.FetchStatic(fs, fmt.Sprintf("%s/%s", path, filename))
|
||||
if err != nil {
|
||||
return b, err
|
||||
}
|
||||
|
||||
b = append(b, Script{Version: extractVersionNumber(filename), Script: []byte(sqlfile)})
|
||||
}
|
||||
|
||||
return b, nil
|
||||
|
|
2
core/env/runtime.go
vendored
2
core/env/runtime.go
vendored
|
@ -15,6 +15,7 @@ package env
|
|||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"embed"
|
||||
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
@ -42,6 +43,7 @@ type Runtime struct {
|
|||
StoreProvider StoreProvider
|
||||
Log Logger
|
||||
Product domain.Product
|
||||
Assets embed.FS
|
||||
}
|
||||
|
||||
// StartTx begins database transaction with given transaction isolation level.
|
||||
|
|
|
@ -13,15 +13,17 @@ package mail
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
|
||||
"github.com/documize/community/core/asset"
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/core/mail"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/setting"
|
||||
ds "github.com/documize/community/domain/smtp"
|
||||
"github.com/documize/community/domain/store"
|
||||
"github.com/documize/community/server/web"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Mailer provides emailing facilities
|
||||
|
@ -43,15 +45,15 @@ func (m *Mailer) Initialize() {
|
|||
func (m *Mailer) ParseTemplate(filename string, params interface{}) (html string, err error) {
|
||||
html = ""
|
||||
|
||||
file, err := web.ReadFile(filename)
|
||||
content, _, err := asset.FetchStatic(m.Runtime.Assets, filename)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, fmt.Sprintf("missing %s", filename))
|
||||
m.Runtime.Log.Error("failed to load mail template", err)
|
||||
return
|
||||
}
|
||||
|
||||
emailTemplate := string(file)
|
||||
buffer := new(bytes.Buffer)
|
||||
|
||||
t := template.Must(template.New("emailTemplate").Parse(emailTemplate))
|
||||
t := template.Must(template.New("emailTemplate").Parse(content))
|
||||
t.Execute(buffer, ¶ms)
|
||||
|
||||
html = buffer.String()
|
||||
|
|
|
@ -20,6 +20,7 @@ import (
|
|||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/documize/community/core/asset"
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/core/response"
|
||||
"github.com/documize/community/core/uniqueid"
|
||||
|
@ -28,7 +29,6 @@ import (
|
|||
"github.com/documize/community/domain/store"
|
||||
om "github.com/documize/community/model/onboard"
|
||||
"github.com/documize/community/model/permission"
|
||||
"github.com/documize/community/server/web"
|
||||
)
|
||||
|
||||
// Handler contains the runtime information such as logging and database.
|
||||
|
@ -112,14 +112,14 @@ func (h *Handler) loadFile(data om.SampleData, filename string, v interface{}) {
|
|||
|
||||
// Reads file and unmarshals content as JSON.
|
||||
func (h *Handler) unpackFile(filename string, v interface{}) (err error) {
|
||||
data, err := web.Embed.Asset("bindata/onboard/" + filename)
|
||||
content, _, err := asset.FetchStatic(h.Runtime.Assets, "onboard/"+filename)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, fmt.Sprintf("missing %s", filename))
|
||||
h.Runtime.Log.Error("failed to load file", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &v)
|
||||
err = json.Unmarshal([]byte(content), &v)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, fmt.Sprintf("failed to read %s as JSON", filename))
|
||||
h.Runtime.Log.Error("failed to load file", err)
|
||||
|
|
|
@ -1,142 +0,0 @@
|
|||
package organization
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/documize/community/core/uniqueid"
|
||||
"github.com/documize/community/model/org"
|
||||
|
||||
"github.com/documize/community/domain/test"
|
||||
)
|
||||
|
||||
// TestSpace tests all space database operations.
|
||||
|
||||
func TestOrganization(t *testing.T) {
|
||||
rt, s, ctx := test.SetupTest()
|
||||
//Create a new organization
|
||||
var err error
|
||||
org := org.Organization{}
|
||||
orgID := uniqueid.Generate()
|
||||
|
||||
t.Run("AddOrginization", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
org.RefID = orgID
|
||||
org.Company = "test"
|
||||
org.Title = "test"
|
||||
org.Message = "test"
|
||||
org.Domain = "testDomain"
|
||||
org.Active = true
|
||||
|
||||
err = s.Organization.AddOrganization(ctx, org)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to add org organization")
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
orgGot, err := s.Organization.GetOrganization(ctx, org.RefID)
|
||||
if err != nil || org.Title != orgGot.Title {
|
||||
t.Error("failed to get org organization")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("GetOrganizationByDomain", func(t *testing.T) {
|
||||
orgGot, err := s.Organization.GetOrganizationByDomain("testDomain")
|
||||
if err != nil || org.Title != orgGot.Title {
|
||||
t.Error("failed to get org organization by domain")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("UpdateOrginization", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
org.Title = "testUpdate"
|
||||
|
||||
err = s.Organization.UpdateOrganization(ctx, org)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to update org organization")
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
orgGot, err := s.Organization.GetOrganization(ctx, org.RefID)
|
||||
if err != nil || org.Title != orgGot.Title {
|
||||
t.Error("failed to get updated org organization")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("CheckDomain", func(t *testing.T) {
|
||||
Domain := s.Organization.CheckDomain(ctx, "")
|
||||
if Domain != Domain {
|
||||
t.Error("failed to CheckDomain")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("UpdateAuthConfig", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = s.Organization.UpdateAuthConfig(ctx, org)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to update organization AuthConfig")
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
})
|
||||
|
||||
//
|
||||
//Run after everything except delete as this makes an org inactive
|
||||
//
|
||||
|
||||
t.Run("RemoveOrganization", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = s.Organization.RemoveOrganization(ctx, org.RefID)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to remove organization")
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
orgGot, err := s.Organization.GetOrganization(ctx, org.RefID)
|
||||
if err != nil || orgGot.Active != false {
|
||||
t.Error("failed to get removed organization activity")
|
||||
}
|
||||
})
|
||||
|
||||
//
|
||||
// teardown code goes here
|
||||
//
|
||||
|
||||
t.Run("DeleteOrganization", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_,
|
||||
err = s.Organization.DeleteOrganization(ctx, orgID)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to delete org organization")
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
})
|
||||
}
|
|
@ -1,234 +0,0 @@
|
|||
package space
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/documize/community/core/uniqueid"
|
||||
"github.com/documize/community/domain/test"
|
||||
"github.com/documize/community/model/space"
|
||||
)
|
||||
|
||||
// TestSpace tests all space database operations.
|
||||
func TestSpace(t *testing.T) {
|
||||
rt, s, ctx := test.SetupTest()
|
||||
spaceID := uniqueid.Generate()
|
||||
spaceID2 := uniqueid.Generate()
|
||||
sp := space.Space{}
|
||||
sp2 := space.Space{}
|
||||
r := space.Role{}
|
||||
r2 := space.Role{}
|
||||
r3 := space.Role{}
|
||||
var err error
|
||||
|
||||
t.Run("Add Space", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
sp.RefID = spaceID
|
||||
sp.OrgID = ctx.OrgID
|
||||
sp.UserID = ctx.UserID
|
||||
sp.Type = space.ScopePublic
|
||||
sp.Name = "PublicTestSpace"
|
||||
sp.UserID = ctx.UserID
|
||||
sp.Created = time.Now().UTC()
|
||||
sp.Revised = time.Now().UTC()
|
||||
|
||||
err = s.Space.Add(ctx, sp)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to add sp space")
|
||||
}
|
||||
|
||||
perm := space.Permission{}
|
||||
perm.OrgID = ctx.OrgID
|
||||
perm.Who = "user"
|
||||
perm.WhoID = ctx.UserID
|
||||
perm.Scope = permission.ScopeRow
|
||||
perm.Location = permission.LocationSpace
|
||||
perm.RefID = spaceID
|
||||
perm.Action = "" // we send array for actions below
|
||||
|
||||
err = s.Space.AddPermissions(ctx, perm, space.SpaceOwner, space.SpaceManage, space.SpaceView)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to add permission")
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
spGet, err := s.Space.Get(ctx, sp.RefID)
|
||||
if err != nil || sp.Name != spGet.Name {
|
||||
t.Error("failed to get sp space")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Update Space", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
|
||||
sp, err := s.Space.Get(ctx, spaceID)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to get space prior to update")
|
||||
return
|
||||
}
|
||||
|
||||
sp.Name = "test update"
|
||||
err = s.Space.Update(ctx, sp)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to update space")
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
sp, err = s.Space.Get(ctx, spaceID)
|
||||
if err != nil || sp.Name != "test update" {
|
||||
t.Error("failed to get the space after update")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Get All", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
sp2.UserID = ctx.UserID
|
||||
sp2.RefID = spaceID2
|
||||
sp2.OrgID = ctx.OrgID
|
||||
sp2.Type = space.ScopePrivate
|
||||
sp2.Name = "PrivateTestSpace"
|
||||
sp.UserID = ctx.UserID
|
||||
sp.Created = time.Now().UTC()
|
||||
sp.Revised = time.Now().UTC()
|
||||
|
||||
err = s.Space.Add(ctx, sp2)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to add sp2")
|
||||
}
|
||||
|
||||
perm := space.Permission{}
|
||||
perm.OrgID = ctx.OrgID
|
||||
perm.Who = "user"
|
||||
perm.WhoID = ctx.UserID
|
||||
perm.Scope = permission.ScopeRow
|
||||
perm.Location = permission.LocationSpace
|
||||
perm.RefID = spaceID2
|
||||
perm.Action = "" // we send array for actions below
|
||||
|
||||
err = s.Space.AddPermissions(ctx, perm, space.SpaceOwner, space.SpaceManage, space.SpaceView)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to add permission")
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
spSlice, err := s.Space.GetAll(ctx)
|
||||
if err != nil || spSlice == nil {
|
||||
t.Error("failed to get all spaces")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("PublicSpaces", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
spSlice, err := s.Space.PublicSpaces(ctx, sp.OrgID)
|
||||
if err != nil || spSlice == nil {
|
||||
t.Error("failed to get public spaces")
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
})
|
||||
|
||||
t.Run("Change Owner", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
newUserID := "Updated Owner"
|
||||
|
||||
err := s.Space.ChangeOwner(ctx, sp.UserID, newUserID)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to change Owner")
|
||||
return
|
||||
}
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
sp, err = s.Space.Get(ctx, spaceID)
|
||||
if err != nil || sp.UserID != newUserID {
|
||||
t.Error("failed to get space w/ new owner")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Add Role", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
|
||||
perm := space.Permission{}
|
||||
perm.OrgID = ctx.OrgID
|
||||
perm.Who = "user"
|
||||
perm.WhoID = ctx.UserID
|
||||
perm.Scope = permission.ScopeRow
|
||||
perm.Location = permission.LocationSpace
|
||||
perm.RefID = spaceID
|
||||
perm.Action = "" // we send array for actions below
|
||||
|
||||
err = s.Space.AddPermissions(ctx, perm, space.DocumentAdd, space.DocumentDelete, space.DocumentMove)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to add permission")
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
roles, err := s.Space.GetUserPermissions(ctx, spaceID)
|
||||
if err != nil || roles == nil {
|
||||
t.Error("Could not get any roles")
|
||||
return
|
||||
}
|
||||
// TODO: could we Verify the role was added with the if r3.UserID == Returned.UserID?
|
||||
})
|
||||
|
||||
t.Run("Get User Permissions", func(t *testing.T) {
|
||||
userRoles, err := s.Space.GetUserPermissions(ctx, spaceID)
|
||||
if err != nil || userRoles == nil {
|
||||
t.Error("failed to get user roles")
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
// teardown
|
||||
t.Run("Delete space", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
|
||||
_, err = s.Space.Delete(ctx, spaceID)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to delete space")
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
})
|
||||
|
||||
t.Run("Delete space 2", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
|
||||
_, err = s.Space.Delete(ctx, spaceID2)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to delete space in teardown")
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
})
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/documize/community/domain/store"
|
||||
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/edition/boot"
|
||||
"github.com/documize/community/edition/logging"
|
||||
"github.com/documize/community/embed"
|
||||
"github.com/documize/community/server/web"
|
||||
_ "github.com/go-sql-driver/mysql" // testing
|
||||
)
|
||||
|
||||
// SetupTest prepares test environment
|
||||
func SetupTest() (rt *env.Runtime, s *store
|
||||
.Store, ctx domain.RequestContext) {
|
||||
rt, s = startRuntime()
|
||||
ctx = setupContext()
|
||||
return rt, s, ctx
|
||||
}
|
||||
|
||||
func startRuntime() (rt *env.Runtime, s *store.Store) {
|
||||
rt = new(env.Runtime)
|
||||
s = new(store.Store)
|
||||
|
||||
rt.Log = logging.NewLogger(false)
|
||||
web.Embed = embed.NewEmbedder()
|
||||
|
||||
rt.Product = env.Product{}
|
||||
rt.Product.Major = "0"
|
||||
rt.Product.Minor = "0"
|
||||
rt.Product.Patch = "0"
|
||||
rt.Product.Version = fmt.Sprintf("%s.%s.%s", rt.Product.Major, rt.Product.Minor, rt.Product.Patch)
|
||||
rt.Product.Edition = "Test"
|
||||
rt.Product.Title = fmt.Sprintf("%s Edition", rt.Product.Edition)
|
||||
|
||||
// parse settings from command line and environment
|
||||
rt.Flags = env.ParseFlags()
|
||||
boot.InitRuntime(rt, s)
|
||||
|
||||
// section.Register(rt, s)
|
||||
|
||||
return rt, s
|
||||
}
|
||||
|
||||
// setup testing context
|
||||
func setupContext() domain.RequestContext {
|
||||
ctx := domain.RequestContext{}
|
||||
ctx.AllowAnonymousAccess = true
|
||||
ctx.Authenticated = true
|
||||
ctx.Administrator = true
|
||||
ctx.Guest = false
|
||||
ctx.Editor = true
|
||||
ctx.GlobalAdmin = true
|
||||
ctx.UserID = "test"
|
||||
ctx.OrgID = "test"
|
||||
return ctx
|
||||
}
|
||||
|
||||
// For dummy user data https://www.mockaroo.com
|
|
@ -26,9 +26,6 @@ import (
|
|||
|
||||
// InitRuntime prepares runtime using command line and environment variables.
|
||||
func InitRuntime(r *env.Runtime, s *store.Store) bool {
|
||||
// TODO: remove this line when Go1.13 is released.
|
||||
os.Setenv("GODEBUG", os.Getenv("GODEBUG")+",tls13=1")
|
||||
|
||||
// We need SALT to hash auth JWT tokens.
|
||||
if r.Flags.Salt == "" {
|
||||
r.Flags.Salt = secrets.RandSalt()
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
|
@ -22,11 +23,12 @@ import (
|
|||
"github.com/documize/community/domain/store"
|
||||
"github.com/documize/community/edition/boot"
|
||||
"github.com/documize/community/edition/logging"
|
||||
"github.com/documize/community/embed"
|
||||
"github.com/documize/community/server"
|
||||
"github.com/documize/community/server/web"
|
||||
)
|
||||
|
||||
//go:embed static/*
|
||||
var embeddedFiles embed.FS
|
||||
|
||||
func main() {
|
||||
// Runtime stores server/application information.
|
||||
rt := env.Runtime{}
|
||||
|
@ -34,21 +36,21 @@ func main() {
|
|||
// Wire up logging implementation.
|
||||
rt.Log = logging.NewLogger(false)
|
||||
|
||||
// Wire up embedded web assets handler.
|
||||
web.Embed = embed.NewEmbedder()
|
||||
|
||||
// Specify the product edition.
|
||||
rt.Product = domain.Product{}
|
||||
rt.Product.Major = "3"
|
||||
rt.Product.Minor = "9"
|
||||
rt.Product.Major = "4"
|
||||
rt.Product.Minor = "0"
|
||||
rt.Product.Patch = "0"
|
||||
rt.Product.Revision = "210328153633"
|
||||
rt.Product.Revision = "210817183831"
|
||||
rt.Product.Version = fmt.Sprintf("%s.%s.%s", rt.Product.Major, rt.Product.Minor, rt.Product.Patch)
|
||||
rt.Product.Edition = domain.CommunityEdition
|
||||
rt.Product.Title = fmt.Sprintf("%s Edition", rt.Product.Edition)
|
||||
|
||||
rt.Log.Info(fmt.Sprintf("Product: %s version %s (build %s)", rt.Product.Title, rt.Product.Version, rt.Product.Revision))
|
||||
|
||||
// Locate static assets.
|
||||
rt.Assets = embeddedFiles
|
||||
|
||||
// Setup data store.
|
||||
s := store.Store{}
|
||||
|
||||
|
@ -61,10 +63,7 @@ func main() {
|
|||
rt.Log.Info("Configuration: " + rt.Flags.ConfigSource)
|
||||
|
||||
// Start database init.
|
||||
bootOK := boot.InitRuntime(&rt, &s)
|
||||
if bootOK {
|
||||
// runtime.Log = runtime.Log.SetDB(runtime.Db)
|
||||
}
|
||||
boot.InitRuntime(&rt, &s)
|
||||
|
||||
// Register document sections.
|
||||
section.Register(&rt, &s)
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
|
||||
Uses github.com/elazarl/go-bindata-assetfs, which must be installed to 'go generate' to create bindata_assetfs.go
|
24590
embed/bindata.go
24590
embed/bindata.go
File diff suppressed because one or more lines are too long
|
@ -1,48 +0,0 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
// Package embed contains the Documize static web data.
|
||||
package embed
|
||||
|
||||
//go:generate go-bindata-assetfs -pkg embed bindata/...
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
assetfs "github.com/elazarl/go-bindata-assetfs"
|
||||
)
|
||||
|
||||
type embedderT struct{}
|
||||
|
||||
func (embedderT) Asset(name string) ([]byte, error) {
|
||||
return Asset(name)
|
||||
}
|
||||
|
||||
func (embedderT) AssetDir(dir string) ([]string, error) {
|
||||
return AssetDir(dir)
|
||||
}
|
||||
|
||||
// StaticAssetsFileSystem data encoded in the go:generate above.
|
||||
func (embedderT) StaticAssetsFileSystem() http.FileSystem {
|
||||
return &assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: AssetInfo, Prefix: "bindata/public"}
|
||||
}
|
||||
|
||||
var embedder embedderT
|
||||
|
||||
// NewEmbedder returns embed assets handler instance
|
||||
func NewEmbedder() embedderT {
|
||||
return embedder
|
||||
}
|
||||
|
||||
// func init() {
|
||||
// fmt.Println("firing embed init()")
|
||||
// web.Embed = embedder
|
||||
// }
|
|
@ -19,11 +19,11 @@ import (
|
|||
|
||||
"github.com/codegangsta/negroni"
|
||||
"github.com/documize/community/core/api/plugins"
|
||||
"github.com/documize/community/core/asset"
|
||||
"github.com/documize/community/core/database"
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/domain/store"
|
||||
"github.com/documize/community/server/routing"
|
||||
"github.com/documize/community/server/web"
|
||||
"github.com/gorilla/handlers"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
@ -81,7 +81,13 @@ func Start(rt *env.Runtime, s *store.Store, ready chan struct{}) {
|
|||
router.Use(handlers.ProxyHeaders)
|
||||
|
||||
n := negroni.New()
|
||||
n.Use(negroni.NewStatic(web.StaticAssetsFileSystem()))
|
||||
|
||||
sfs, err := asset.GetPublicFileSystem(rt.Assets)
|
||||
if err != nil {
|
||||
rt.Log.Error("!!!!!!!!!! Cannot load public file system", err)
|
||||
}
|
||||
n.Use(negroni.NewStatic(sfs))
|
||||
|
||||
n.Use(negroni.HandlerFunc(cm.cors))
|
||||
n.UseHandler(router)
|
||||
|
||||
|
|
|
@ -1,48 +0,0 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
// Package web contains the Documize static web data.
|
||||
package web
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// EmbedHandler is defined in each embed directory
|
||||
type EmbedHandler interface {
|
||||
Asset(string) ([]byte, error)
|
||||
AssetDir(string) ([]string, error)
|
||||
StaticAssetsFileSystem() http.FileSystem
|
||||
}
|
||||
|
||||
// Embed allows access to the embedded data
|
||||
var Embed EmbedHandler
|
||||
|
||||
// StaticAssetsFileSystem data encoded in the go:generate above.
|
||||
func StaticAssetsFileSystem() http.FileSystem {
|
||||
return Embed.StaticAssetsFileSystem()
|
||||
//return &assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: AssetInfo, Prefix: "bindata/public"}
|
||||
}
|
||||
|
||||
// ReadFile is intended to substitute for ioutil.ReadFile().
|
||||
func ReadFile(filename string) ([]byte, error) {
|
||||
return Embed.Asset("bindata/" + filename)
|
||||
}
|
||||
|
||||
// Asset fetch.
|
||||
func Asset(location string) ([]byte, error) {
|
||||
return Embed.Asset(location)
|
||||
}
|
||||
|
||||
// AssetDir returns web app "assets" folder.
|
||||
func AssetDir(dir string) ([]string, error) {
|
||||
return Embed.AssetDir(dir)
|
||||
}
|
|
@ -16,6 +16,7 @@ import (
|
|||
"html/template"
|
||||
"net/http"
|
||||
|
||||
"github.com/documize/community/core/asset"
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/core/secrets"
|
||||
"github.com/documize/community/domain/store"
|
||||
|
@ -39,11 +40,12 @@ type Handler struct {
|
|||
// EmberHandler serves HTML web pages
|
||||
func (h *Handler) EmberHandler(w http.ResponseWriter, r *http.Request) {
|
||||
filename := "index.html"
|
||||
|
||||
switch h.Runtime.Flags.SiteMode {
|
||||
case env.SiteModeOffline:
|
||||
filename = "offline.html"
|
||||
case env.SiteModeSetup:
|
||||
// NoOp
|
||||
// noop
|
||||
case env.SiteModeBadDB:
|
||||
filename = "db-error.html"
|
||||
default:
|
||||
|
@ -52,13 +54,13 @@ func (h *Handler) EmberHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
SiteInfo.Edition = string(h.Runtime.Product.Edition)
|
||||
|
||||
data, err := Embed.Asset("bindata/" + filename)
|
||||
content, _, err := asset.FetchStatic(h.Runtime.Assets, filename)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
emberView := template.Must(template.New(filename).Parse(string(data)))
|
||||
emberView := template.Must(template.New(filename).Parse(string(content)))
|
||||
|
||||
if err := emberView.Execute(w, SiteInfo); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue