gophertype/cmd/gophertype/main.go

91 lines
2.2 KiB
Go
Raw Normal View History

2019-11-15 03:03:56 +00:00
//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/console"
2019-11-15 03:03:56 +00:00
_ "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
optRoot string
2019-11-15 03:03:56 +00:00
// 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")
flag.StringVar(&optRoot, "root", "./public_html", "User root for custom web pages")
2019-11-15 03:03:56 +00:00
// 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()
console.SetDebug(optDebug)
2019-11-15 03:03:56 +00:00
// 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(optRoot)
2019-11-15 03:03:56 +00:00
app.UseDB(dbDriver, dbPath)
app.SetupRouter()
2019-11-27 00:54:02 +00:00
if err := app.ListenAndServe(optBind); err != nil {
console.Error("ListenAndServe: %s", err)
os.Exit(0)
}
2019-11-15 03:03:56 +00:00
}