Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[add] load application from the package file #370

Merged
merged 1 commit into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

var appPath string
var envFile string
var yazFile string

var lang = os.Getenv("YAO_LANG")
var langs = map[string]string{
Expand Down Expand Up @@ -128,6 +129,7 @@ func init() {
)
// rootCmd.SetHelpCommand(helpCmd)
rootCmd.PersistentFlags().StringVarP(&appPath, "app", "a", "", L("Application directory"))
rootCmd.PersistentFlags().StringVarP(&yazFile, "file", "f", "", L("Application package file"))
rootCmd.PersistentFlags().StringVarP(&envFile, "env", "e", "", L("Environment file"))
}

Expand All @@ -142,19 +144,25 @@ func Execute() {
// Boot 设定配置
func Boot() {
root := config.Conf.Root

if appPath != "" {
r, err := filepath.Abs(appPath)
if err != nil {
exception.New("Root error %s", 500, err.Error()).Throw()
}
root = r
}

if envFile != "" {
config.Conf = config.LoadFrom(envFile)
} else {
config.Conf = config.LoadFrom(filepath.Join(root, ".env"))
}

if yazFile != "" {
config.Conf.AppSource = yazFile
}

if config.Conf.Mode == "production" {
config.Production()
} else if config.Conf.Mode == "development" {
Expand Down
21 changes: 10 additions & 11 deletions engine/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,29 +225,28 @@ func loadApp(root string) error {
var err error
var app application.Application

if root == "bin:application.pkg" {
app, err = application.OpenFromBin(root, &share.Pack{}) // Load app from Bin
if root == "bin:application.yaz" {
app, err = application.OpenFromYaz(root, &share.Pack{}) // Load app from Bin
if err != nil {
return err
}
application.Load(app)

} else if strings.HasSuffix(root, ".pkg") {

app, err = application.OpenFromPkg(root, &share.Pack{}) // Load app from .pkg file
} else if strings.HasSuffix(root, ".yaz") {
app, err = application.OpenFromYaz(root, &share.Pack{}) // Load app from .yaz file
if err != nil {
return err
}
application.Load(app)
}

app, err = application.OpenFromDisk(root) // Load app from Disk
if err != nil {
return err
} else {
app, err = application.OpenFromDisk(root) // Load app from Disk
if err != nil {
return err
}
application.Load(app)
}

application.Load(app)

var info []byte

// Read app setting
Expand Down
48 changes: 48 additions & 0 deletions engine/load_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package engine

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/yaoapp/gou/api"
"github.com/yaoapp/gou/application/yaz"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/share"
)

func TestLoad(t *testing.T) {
Expand All @@ -24,3 +27,48 @@ func TestReload(t *testing.T) {
assert.Nil(t, err)
assert.Greater(t, len(api.APIs), 0)
}

func TestLoadYaz(t *testing.T) {

defer Unload()

// package yaz
file, err := yaz.Pack(config.Conf.Root, &share.Pack{})
if err != nil {
t.Fatal(err)
}
defer os.Remove(file)

cfg := config.Conf
cfg.AppSource = file
err = Load(cfg)
if err != nil {
t.Fatal(err)
}
assert.Greater(t, len(api.APIs), 0)

}

func TestReoadYaz(t *testing.T) {

defer Unload()

// package yaz
file, err := yaz.Pack(config.Conf.Root, &share.Pack{})
if err != nil {
t.Fatal(err)
}
defer os.Remove(file)

cfg := config.Conf
cfg.AppSource = file
err = Load(cfg)
if err != nil {
t.Fatal(err)
}
assert.Greater(t, len(api.APIs), 0)

Reload(cfg)
assert.Nil(t, err)
assert.Greater(t, len(api.APIs), 0)
}
16 changes: 10 additions & 6 deletions share/pack.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package share

import "io"

// ********************************************************************************
// WARNING: DO NOT MODIFY THIS FILE. IT WILL BE REPLACED BY THE APPLICATION CODE.
// *********************************************************************************

// Pack the yao app package
type Pack struct{}

// Decode the package decode method
func (pkg *Pack) Decode(data []byte) ([]byte, error) {
return data, nil
// Encrypt encrypt
func (pkg *Pack) Encrypt(reader io.Reader, writer io.Writer) error {
_, err := io.Copy(writer, reader)
return err
}

// Encode the package encode method
func (pkg *Pack) Encode(data []byte) ([]byte, error) {
return data, nil
// Decrypt decrypt
func (pkg *Pack) Decrypt(reader io.Reader, writer io.Writer) error {
_, err := io.Copy(writer, reader)
return err
}