func Serve(c *cli.Context) { conf, err := loadConfig(c.String("config")) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } httpConfig, err := loadHTTPConfig(c, conf) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } logger, err := newLogger(conf) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } pool, err := newPool(conf, logger) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } mailer, err := newMailer(conf, logger) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } apiHandler := NewAPIHandler(pool, mailer, logger.New("module", "http")) http.Handle("/api/", http.StripPrefix("/api", apiHandler)) if httpConfig.staticURL != "" { staticURL, err := url.Parse(httpConfig.staticURL) if err != nil { logger.Crit(fmt.Sprintf("Bad static-url: %v", err)) os.Exit(1) } http.Handle("/", httputil.NewSingleHostReverseProxy(staticURL)) } listenAt := fmt.Sprintf("%s:%s", httpConfig.listenAddress, httpConfig.listenPort) fmt.Printf("Starting to listen on: %s\n", listenAt) feedUpdater := NewFeedUpdater(pool, logger.New("module", "feedUpdater")) go feedUpdater.KeepFeedsFresh() if err := http.ListenAndServe(listenAt, nil); err != nil { os.Stderr.WriteString("Could not start web server!\n") os.Exit(1) } }
func ResetPassword(c *cli.Context) { if len(c.Args()) != 1 { cli.ShowCommandHelp(c, c.Command.Name) os.Exit(1) } name := c.Args()[0] conf, err := loadConfig(c.String("config")) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } logger, err := newLogger(conf) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } pool, err := newPool(conf, logger) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } user, err := data.SelectUserByName(pool, name) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } password, err := genRandPassword() if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } update := &data.User{} SetPassword(update, password) err = data.UpdateUser(pool, user.ID.Value, update) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } fmt.Println("User:"******"Password:", password) }
func loadHTTPConfig(c *cli.Context, conf ini.File) (httpConfig, error) { config := httpConfig{} config.listenAddress = c.String("address") config.listenPort = c.String("port") config.staticURL = c.String("static-url") var ok bool if !c.IsSet("address") { if config.listenAddress, ok = conf.Get("server", "address"); !ok { return config, errors.New("Missing server address") } } if !c.IsSet("port") { if config.listenPort, ok = conf.Get("server", "port"); !ok { return config, errors.New("Missing server port") } } return config, nil }