gophertype/cmd/gophertype/main.go

82 行
1.9 KiB
Go

//go:generate go-bindata -pkg bundled -o pkg/bundled/bindata.go pvt-www/...
//go:generate echo hello world
package main
import (
"flag"
"fmt"
"os"
"git.kirsle.net/apps/gophertype/pkg"
_ "git.kirsle.net/apps/gophertype/pkg/controllers"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
// Build parameters.
var (
Build string
BuildDate string
)
// Command-line flags.
var (
optDebug bool
optBind string
// Database option flags.
optSQLite string
optPostgres string
optMySQL string
// Chosen DB options.
dbDriver string
dbPath string
)
func init() {
flag.BoolVar(&optDebug, "debug", false, "Debug level logging")
flag.StringVar(&optBind, "bind", ":8000", "Bind address for HTTP server")
// Database driver. Choose one.
flag.StringVar(&optSQLite, "sqlite3", "", "Use SQLite database, default 'database.sqlite'")
flag.StringVar(&optPostgres, "postgres", "",
"Use Postgres database, format: "+
"host=myhost port=myport user=gorm dbname=gorm password=mypassword")
flag.StringVar(&optMySQL, "mysql", "",
"Use MySQL database, format: "+
"user:password@/dbname?charset=utf8&parseTime=True&loc=Local")
}
func main() {
flag.Parse()
// Validate the choice of database.
if optSQLite != "" {
dbDriver = "sqlite3"
dbPath = optSQLite
} else if optPostgres != "" {
dbDriver = "postgres"
dbPath = optPostgres
} else if optMySQL != "" {
dbDriver = "mysql"
dbPath = optMySQL
} else {
fmt.Print(
"Choose a database to use when running this command. Examples:\n" +
"gophertype -sqlite3 database.sqlite\n" +
"gophertype -postgres host=myhost port=myport user=gorm dbname=gorm password=mypassword\n" +
"gophertype -mysql user:password@/dbname?charset=utf8&parseTime=True&loc=Local",
)
os.Exit(1)
}
fmt.Println("Hello world")
app := gophertype.NewSite()
app.UseDB(dbDriver, dbPath)
app.SetupRouter()
app.ListenAndServe(optBind)
}