Exemplo n.º 1
0
Arquivo: main.go Projeto: cwc/webconf
func main() {
	myConfig := Config{}

	if c := webconf.New(&myConfig, "config.json", ":50000", nil); c != nil {
		if err := c.Load(); err == nil {
			log.Printf("Loaded config: %#v", myConfig)
		} else {
			log.Println(err)
		}
	}
}
Exemplo n.º 2
0
func main() {
	// Load config with webconf on first available port
	appConfig := Config{}
	for p := 50000; ; p++ {
		c := webconf.New(&appConfig, "config.json", ":"+strconv.Itoa(p), nil)
		if c != nil {
			if err := c.Load(); err == nil {
				break // Config loaded
			}
		}
	}

	// Connect to DB
	db, err := sql.Open("postgres", appConfig.DbConnection)
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	// Intialize DB schema if necessary
	err = initDatabase(db)
	if err != nil {
		log.Fatal(err)
	}

	// Configure Mango
	stack := &mango.Stack{}
	stack.Address = appConfig.BindAddress

	cookieOptions := mango.CookieOptions{
		strings.Split(appConfig.BindAddress, ":")[0],
		"/",
		86400, // One day expiration
		true,
		true,
	}

	// Setup routes
	routes := make(map[string]mango.App)

	routes["/login"] = stack.Compile(login)
	routes["/edit/event"] = stack.Compile(editEvent)
	routes["/edit/event/([0-9]+)"] = stack.Compile(editEvent)
	routes["/edit/asset"] = stack.Compile(editAsset)
	routes["/edit/asset/([0-9]+)"] = stack.Compile(editAsset)
	routes["/asset/([0-9]+)"] = stack.Compile(displayAsset)
	routes["/user/([0-9]+)"] = stack.Compile(displayUser)
	routes["/asset"] = stack.Compile(listAssets)
	routes["/user"] = stack.Compile(listUsers)
	routes["/"] = stack.Compile(listAssets)

	stack.Middleware(
		mango.ShowErrors(""),
		mango.Sessions(appConfig.SessionSecret, "asset-tracker", &cookieOptions),
		AssetTracker(db, initTemplates()),
		mango.Static("static"),
		mango.Routing(routes),
	)

	stack.Run(defaultHandler)

	for {
	}
}