Ejemplo n.º 1
0
// new site
func NewSite(ctx *cli.Context) {
	config := model.NewConfig()

	// encode config
	if err := model.WriteConfig(config, vars.CONFIG_FILE); err != nil {
		log.Error("NewSite | %s", err.Error())
		return
	}

	log.Info("NewSite | %-8s | %s", "Init", vars.CONFIG_FILE)
	log.Info("NewSite | %-8s | %s", "Version", config.Version)
	log.Info("NewSite | %-8s | %s:%s", "Server", config.Server.Host, config.Server.Port)
}
Ejemplo n.º 2
0
// new site data
func NewSiteData(ctx *cli.Context) {
	sqliteVersion, _, _ := sqlite3.Version()
	log.Info("NewSite | %-8s | %s | %s", "SQLite", sqliteVersion, vars.DATA_FILE)

	engine, err := xorm.NewEngine("sqlite3", vars.DATA_FILE)
	if err != nil {
		log.Error("NewSite | %s", err.Error())
		return
	}
	engine.SetLogger(nil) // close logger

	if err = engine.Sync2(new(model.User),
		new(model.Token),
		new(model.Article),
		new(model.Page),
		new(model.Tag),
		new(model.Setting),
		new(model.Media),
		new(model.Comment)); err != nil {
		log.Error("NewSite | %s", err.Error())
		return
	}

	log.Info("NewSite | %-8s | SyncDb | %s,%s,%s,%s,%s,%s,%s,%s", "SQLite",
		reflect.TypeOf(new(model.User)).String(),
		reflect.TypeOf(new(model.Token)).String(),
		reflect.TypeOf(new(model.Article)).String(),
		reflect.TypeOf(new(model.Page)).String(),
		reflect.TypeOf(new(model.Tag)).String(),
		reflect.TypeOf(new(model.Setting)).String(),
		reflect.TypeOf(new(model.Media)).String(),
		reflect.TypeOf(new(model.Comment)).String(),
	)

	// site init data
	NewSiteInitData(engine)

	log.Info("NewSite | %-8s | Success", "SQLite")
	engine.Close()
}
Ejemplo n.º 3
0
// upgrade action
func UpgradeAction(cfg *model.Config) {
	t := time.Now()
	log.Debug("Upgrade | %-8s | %s(%s) -> %s(%s)", "Upgrade", cfg.Version, cfg.Date, vars.VERSION, vars.VERSION_DATE)

	opt := &PrepareOption{true, true, false}
	pre, err := Prepare(opt)
	if err != nil {
		log.Error("Upgrade | %-8s | %s", "Prepare", err.Error())
		return
	}
	log.Info("Upgrade | %-8s | %s", "Prepare", opt.String())

	oldVersion, _ := strconv.Atoi(pre.Config.Date)
	scriptIndex := []int{}
	for vr, _ := range upg.Script {
		if vr > oldVersion {
			scriptIndex = append(scriptIndex, vr)
		}
	}
	sort.Sort(sort.IntSlice(scriptIndex))

	for _, cv := range scriptIndex {
		log.Debug("Upgrade | %-8s | %d ", "Process", cv)
		if err := upg.Script[cv](); err != nil {
			log.Error("Upgrade | %-8s | %s", "Process", err.Error())
			return
		}
	}

	pre.Config.Version = vars.VERSION
	pre.Config.Date = vars.VERSION_DATE
	if err := model.WriteConfig(pre.Config, vars.CONFIG_FILE); err != nil {
		log.Error("Upgrade | %-8s | SyncFail", "Config")
		return
	}

	log.Info("Upgrade | %-8s | Sync | %s", "Config", vars.CONFIG_FILE)
	log.Info("Upgrade | %-8s | %.1fms", "Done", time.Since(t).Seconds()*1000)
}
Ejemplo n.º 4
0
// pack static files to source
func PackSrc(ctx *cli.Context) {
	t := time.Now()
	log.Info("Pack | %-8s", "Source")

	file, err := packSrcZip()
	if err != nil {
		log.Error("Pack | %-8s | %s", "ZipSrc", err.Error())
		return
	}

	bytes, err := ioutil.ReadFile(file)
	if err != nil {
		log.Error("Pack | %-8s | %s", "ZipSrc", err.Error())
		return
	}
	zipWriter, err := os.OpenFile("cmd/asset.go", os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm)
	if err != nil {
		log.Error("Pack | %-8s | %s", "ZipSrc", err.Error())
		return
	}
	header := `package cmd
const zipBytes="`
	zipWriter.Write([]byte(header))
	encoder := base64.NewEncoder(base64.StdEncoding, zipWriter)
	encoder.Write(bytes)
	encoder.Close()
	zipWriter.Write([]byte(`"`))
	zipWriter.Sync()
	zipWriter.Close()
	if err = os.Remove(file); err != nil {
		log.Error("Pack | %-8s | %s", "ZipSrc", err.Error())
		return
	}
	log.Info("Pack | %-8s | %s", "ZipSrc", utils.FriendBytesSize(int64(len(bytes))))
	log.Info("Pack | %-8s | %.1fms", "ZipSrc", time.Since(t).Seconds()*1000)
}
Ejemplo n.º 5
0
// new site assets
func NewSiteAsset(ctx *cli.Context) {
	zip.Verbose = false
	tmpZipFile := "tmp.zip"
	decoder := base64.NewDecoder(base64.StdEncoding, bytes.NewBufferString(zipBytes))
	b, _ := ioutil.ReadAll(decoder)
	if err := ioutil.WriteFile(tmpZipFile, b, os.ModePerm); err != nil {
		log.Fatal("NewSite | %s", err.Error())
	}
	z, err := zip.Open(tmpZipFile)
	if err != nil {
		log.Fatal("NewSite | %s", err.Error())
	}
	z.ExtractTo("")
	z.Close()
	decoder = nil
	os.Remove(tmpZipFile)
	log.Info("NewSite | %-8s | Success", "Asset")
}
Ejemplo n.º 6
0
	"github.com/mattn/go-sqlite3"
	"io/ioutil"
	"os"
	"reflect"
	"strconv"
	"time"
)

var installCmd cli.Command = cli.Command{
	Name:  "install",
	Usage: "install new site for first run",
	Action: func(ctx *cli.Context) {
		t := time.Now()
		// if is not new site,
		if !IsNewSite(ctx) {
			log.Info("NewSite | %-8s", "Done")
			return
		}
		NewSite(ctx)
		NewSiteData(ctx)
		NewSiteAsset(ctx)
		log.Info("NewSite | %-8s | %.1fms", "Finish", time.Since(t).Seconds()*1e3)
	},
}

// check is new site
func IsNewSite(ctx *cli.Context) bool {
	if com.IsFile(vars.CONFIG_FILE) && com.IsFile(vars.DATA_FILE) {
		return false
	}
	return true
Ejemplo n.º 7
0
		}

		// normal pack
		t := time.Now()
		opt := &mapi.PackOption{
			IsStaticAll: true,
			IsData:      true,
		}
		res := mapi.Pack.Pack(opt)
		if !res.Status {
			log.Error("Pack | %-8s | %s", "ZipAll", res.Error)
			return
		}
		file := res.Data["file"].(string)
		if fi, err := os.Stat(file); err == nil {
			log.Info("Pack | %-8s | %s | %s ", "ZipAll", file, utils.FriendBytesSize(fi.Size()))
		} else {
			log.Info("Pack | %-8s | %s", "ZipAll", file)
		}
		log.Info("Pack | %-8s | %.1fms", "ZipAll", time.Since(t).Seconds()*1000)
	},
}

// pack source zip
func packSrcZip() (string, error) {
	opt := &mapi.PackOption{
		IsStaticAll: false,
		IsData:      false,
	}
	res := mapi.Pack.Pack(opt)
	if !res.Status {
Ejemplo n.º 8
0
	"html/template"
	"time"
)

var servCmd cli.Command = cli.Command{
	Name:  "server",
	Usage: "run http server to render and show pages",
	Action: func(ctx *cli.Context) {
		// do prepare
		opt := &PrepareOption{true, true, true}
		pre, err := Prepare(opt)
		if err != nil {
			log.Error("Server | %-8s | %s", "Prepare", err.Error())
			return
		}
		log.Info("Server | %-8s | %s", "Prepare", opt.String())

		if IsNeedUpgrade(pre.Config) {
			log.Info("Server | %-8s | %s(%s) -> %s(%s)", "Upgrade", pre.Config.Version, pre.Config.Date, vars.VERSION, vars.VERSION_DATE)
			log.Info("Please run 'purine.exe upgrade'")
			return
		}

		// start server
		ServeMiddleware(ctx)
		ServeRouting(ctx)
		log.Info("Server | %-8s | %s:%s", "Http", pre.Config.Server.Host, pre.Config.Server.Port)

		vars.Server.Run(fmt.Sprintf("%s:%s", pre.Config.Server.Host, pre.Config.Server.Port))
	},
}