From 49430cf5b3057a7b8ce90699467a3cfa411e8161 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 20 Apr 2023 19:39:52 +0800 Subject: [PATCH] [add] load application from the package file --- cmd/root.go | 8 ++++++++ engine/load.go | 21 ++++++++++---------- engine/load_test.go | 48 +++++++++++++++++++++++++++++++++++++++++++++ share/pack.go | 16 +++++++++------ 4 files changed, 76 insertions(+), 17 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 5cfa7e3b42..8ba4fb93f9 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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{ @@ -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")) } @@ -142,6 +144,7 @@ func Execute() { // Boot 设定配置 func Boot() { root := config.Conf.Root + if appPath != "" { r, err := filepath.Abs(appPath) if err != nil { @@ -149,12 +152,17 @@ func Boot() { } 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" { diff --git a/engine/load.go b/engine/load.go index 9255118d47..12e66a86be 100644 --- a/engine/load.go +++ b/engine/load.go @@ -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 diff --git a/engine/load_test.go b/engine/load_test.go index 7f9b770500..d9c71f229a 100644 --- a/engine/load_test.go +++ b/engine/load_test.go @@ -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) { @@ -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) +} diff --git a/share/pack.go b/share/pack.go index 5ee5b6f0a2..b1bc5f67ea 100644 --- a/share/pack.go +++ b/share/pack.go @@ -1,5 +1,7 @@ package share +import "io" + // ******************************************************************************** // WARNING: DO NOT MODIFY THIS FILE. IT WILL BE REPLACED BY THE APPLICATION CODE. // ********************************************************************************* @@ -7,12 +9,14 @@ package share // 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 }