<commit_before>package cmd

import (
	"fmt"
	"github.com/BurntSushi/toml"
	"github.com/codegangsta/cli"
	"github.com/fuxiaohei/purine/log"
	"github.com/fuxiaohei/purine/model"
	"github.com/fuxiaohei/purine/route/admin"
	"github.com/fuxiaohei/purine/route/base"
	"github.com/fuxiaohei/purine/utils"
	"github.com/fuxiaohei/purine/vars"
	"github.com/go-xorm/xorm"
	"github.com/lunny/tango"
	"github.com/mattn/go-sqlite3"
	"github.com/tango-contrib/binding"
	"github.com/tango-contrib/renders"
	"html/template"
)

var servCmd cli.Command = cli.Command{
	Name:  "serv",
	Usage: "run http server to render and show pages",
	Action: func(ctx *cli.Context) {
		// read config file
		cfg := ServeConfig(ctx)
		if cfg == nil {
			log.Error("Serv|%-8s|ReadFail", "Config")
			return
		}
		log.Info("Serv|%-8s|Read|%s", "Config", configTomlFile)

		// start Db
		ServeDb(ctx)

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

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

func ServeConfig(ctx *cli.Context) *model.Config {
	cfg := new(model.Config)
	if _, err := toml.DecodeFile(configTomlFile, cfg); err != nil {
		log.Error("Serv|%-8s|%s", "Config", err.Error())
		return nil
	}
	return cfg
}

func ServeDb(ctx *cli.Context) {
	sqliteVersion, _, _ := sqlite3.Version()
	log.Info("Serv|%-8s|%s|%s", "SQLite", sqliteVersion, databaseFile)

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

func ServeMiddleware(ctx *cli.Context) {
	vars.Server.Use(binding.Bind())
	vars.Server.Use(base.AuthHandler())
	vars.Server.Use(tango.Static(tango.StaticOptions{
		RootPath: "static",<commit_msg>server command<commit_after>package cmd

import (
	"fmt"
	"github.com/BurntSushi/toml"
	"github.com/codegangsta/cli"
	"github.com/fuxiaohei/purine/log"
	"github.com/fuxiaohei/purine/model"
	"github.com/fuxiaohei/purine/route/admin"
	"github.com/fuxiaohei/purine/route/base"
	"github.com/fuxiaohei/purine/utils"
	"github.com/fuxiaohei/purine/vars"
	"github.com/go-xorm/xorm"
	"github.com/lunny/tango"
	"github.com/mattn/go-sqlite3"
	"github.com/tango-contrib/binding"
	"github.com/tango-contrib/renders"
	"html/template"
)

var servCmd cli.Command = cli.Command{
	Name:  "server",
	Usage: "run http server to render and show pages",
	Action: func(ctx *cli.Context) {
		// read config file
		cfg := ServeConfig(ctx)
		if cfg == nil {
			log.Error("Server|%-8s|ReadFail", "Config")
			return
		}
		log.Info("Server|%-8s|Read|%s", "Config", configTomlFile)

		// start Db
		ServeDb(ctx)

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

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

func ServeConfig(ctx *cli.Context) *model.Config {
	cfg := new(model.Config)
	if _, err := toml.DecodeFile(configTomlFile, cfg); err != nil {
		log.Error("Server|%-8s|%s", "Config", err.Error())
		return nil
	}
	return cfg
}

func ServeDb(ctx *cli.Context) {
	sqliteVersion, _, _ := sqlite3.Version()
	log.Info("Server|%-8s|%s|%s", "SQLite", sqliteVersion, databaseFile)

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

func ServeMiddleware(ctx *cli.Context) {
	vars.Server.Use(binding.Bind())
	vars.Server.Use(base.AuthHandler())
	vars.Server.Use(tango.Static(tango.StaticOptions{
		RootPath: "static",
{'fraction_non_alphanumeric': 0.17854217854217855, 'fraction_numerical': 0.004368004368004368, 'mean_word_length': 4.420118343195266, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '284916', 'n_tokens_mistral': 1552, 'n_tokens_neox': 1465, 'n_words': 230}