Skip to content

Commit

Permalink
use command web and init
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon committed Oct 25, 2015
1 parent 78d1436 commit 79bb106
Show file tree
Hide file tree
Showing 8 changed files with 22,533 additions and 57 deletions.
6 changes: 3 additions & 3 deletions .bra.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[run]
init_cmds = [["./peach"]]
init_cmds = [["./peach", "web"]]
watch_all = true
watch_dirs = [
"$WORKDIR/conf",
"$WORKDIR/cmd",
"$WORKDIR/models",
"$WORKDIR/modules",
"$WORKDIR/routers"
Expand All @@ -12,5 +12,5 @@ build_delay = 1500
cmds = [
["go", "install"],
["go", "build"],
["./peach"]
["./peach", "web"]
]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Peach is a web server for multi-language, real-time synchronization and searchable documentation.

#### Current version: 0.7.3
#### Current version: 0.8.0

## Getting Help

Expand Down
1 change: 1 addition & 0 deletions bindata.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go-bindata -o=modules/bindata/bindata.go -ignore="\\.DS_Store|config.codekit|less" -pkg=bindata templates/... conf/... public/...
105 changes: 105 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package cmd

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/Unknwon/com"
"github.com/codegangsta/cli"
"gopkg.in/ini.v1"

"github.com/peachdocs/peach/modules/bindata"
)

var New = cli.Command{
Name: "new",
Usage: "Initialize a new Peach project",
Action: runNew,
Flags: []cli.Flag{
cli.StringFlag{"target, t", "my.peach", "Directory to save project files", ""},
cli.BoolFlag{"yes, y", "Yes to all confirmations", ""},
},
}

func checkYesNo() bool {
var choice string
fmt.Scan(&choice)
return strings.HasPrefix(strings.ToLower(choice), "y")
}

func toRed(str string) string {
return fmt.Sprintf("\033[31m%s\033[0m", str)
}

func toGreen(str string) string {
return fmt.Sprintf("\033[32m%s\033[0m", str)
}

func toYellow(str string) string {
return fmt.Sprintf("\033[33m%s\033[0m", str)
}

func restoreAssets(target, dir string) {
if err := bindata.RestoreAssets(target, dir); err != nil {
fmt.Printf(toRed("✗ %v\n"), err)
os.Exit(1)
}
}

func runNew(ctx *cli.Context) {
target := ctx.String("target")
if com.IsExist(target) && !ctx.Bool("yes") {
fmt.Printf(toYellow("Directory '%s' already exists, do you want to overwrite?[Y/n] "), target)
if !checkYesNo() {
os.Exit(0)
}
}

fmt.Printf("➜ Creating '%s'...\n", target)
os.MkdirAll(target, os.ModePerm)

// Create default files.
dirs := []string{"templates", "public"}
for _, dir := range dirs {
fmt.Printf("➜ Creating '%s'...\n", dir)
os.RemoveAll(filepath.Join(target, dir))
restoreAssets(target, dir)
}

// Create custom templates.
yes := ctx.Bool("yes")
if !yes {
fmt.Printf(toYellow("Do you want to use custom templates?[Y/n] "))
yes = checkYesNo()
}

if yes {
fmt.Println("➜ Creating 'custom/templates'...")
restoreAssets(filepath.Join(target, "custom"), "templates")

// Update configuration to use custom templates.
fmt.Println("➜ Updating custom configuration...")
var cfg *ini.File
var err error
customPath := filepath.Join(target, "custom/app.ini")
if com.IsExist(customPath) {
cfg, err = ini.Load(customPath)
if err != nil {
fmt.Printf(toRed("✗ %v\n"), err)
os.Exit(1)
}
} else {
cfg = ini.Empty()
}

cfg.Section("page").Key("USE_CUSTOM_TPL").SetValue("true")
if err = cfg.SaveTo(customPath); err != nil {
fmt.Printf(toRed("✗ %v\n"), err)
os.Exit(1)
}
}

fmt.Println(toGreen("✓ Done!"))
}
68 changes: 68 additions & 0 deletions cmd/web.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cmd

import (
"fmt"
"net/http"

"github.com/Unknwon/log"
"github.com/codegangsta/cli"
"github.com/go-macaron/i18n"
"github.com/go-macaron/pongo2"
"gopkg.in/macaron.v1"

"github.com/peachdocs/peach/models"
"github.com/peachdocs/peach/modules/middleware"
"github.com/peachdocs/peach/modules/setting"
"github.com/peachdocs/peach/routers"
)

var Web = cli.Command{
Name: "web",
Usage: "Start Peach web server",
Action: runWeb,
Flags: []cli.Flag{
cli.StringFlag{"config, c", "custom/app.ini", "Custom configuration file path", ""},
},
}

func runWeb(ctx *cli.Context) {
if ctx.IsSet("config") {
setting.CustomConf = ctx.String("config")
}
setting.NewContext()
models.NewContext()

log.Info("Peach %s", setting.AppVer)

m := macaron.New()
m.Use(macaron.Logger())
m.Use(macaron.Recovery())
m.Use(macaron.Statics(macaron.StaticOptions{
SkipLogging: setting.ProdMode,
}, "custom/public", "public", models.HTMLRoot))
m.Use(i18n.I18n(i18n.Options{
Files: setting.Docs.Locales,
}))
tplDir := "templates"
if setting.Page.UseCustomTpl {
tplDir = "custom/templates"
}
m.Use(pongo2.Pongoer(pongo2.Options{
Directory: tplDir,
}))
m.Use(middleware.Contexter())

m.Get("/", routers.Home)
m.Get("/docs", routers.Docs)
m.Get("/docs/images/*", routers.DocsStatic)
m.Get("/docs/*", routers.Docs)
m.Post("/hook", routers.Hook)
m.Get("/search", routers.Search)
m.Get("/*", routers.Pages)

m.NotFound(routers.NotFound)

listenAddr := fmt.Sprintf("0.0.0.0:%d", setting.HTTPPort)
log.Info("%s Listen on %s", setting.Site.Name, listenAddr)
log.Fatal("Fail to start Peach: %v", http.ListenAndServe(listenAddr, m))
}
Loading

0 comments on commit 79bb106

Please sign in to comment.