示例#1
0
func (s *Server) SetMinLevel(w http.ResponseWriter, r *http.Request) {
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	var level lager.LogLevel

	switch logger.LogLevel(string(body)) {
	case logger.LogLevelDebug:
		level = lager.DEBUG
	case logger.LogLevelInfo:
		level = lager.INFO
	case logger.LogLevelError:
		level = lager.ERROR
	case logger.LogLevelFatal:
		level = lager.FATAL
	default:
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	s.sink.SetMinLevel(level)
}
示例#2
0
func main() {
	if version == "" {
		version = "dev"
	}

	if len(os.Args) > 1 {
		arg := os.Args[1]
		if arg == "version" || arg == "-v" || arg == "--version" {
			fmt.Printf("%s\n", version)
			os.Exit(0)
		}
	}

	flag.Parse()

	logger, sink, err := logger.InitializeLogger(logger.LogLevel(*logLevel))
	if err != nil {
		fmt.Printf("Failed to initialize logger\n")
		panic(err)
	}

	logger.Info("garagepi starting", lager.Data{"version": version})
	logger.Debug("flags", lager.Data{
		"enableHTTP":  enableHTTP,
		"enableHTTPS": enableHTTPS,
		"forceHTTPS":  forceHTTPS,
	})

	if !(*enableHTTP || *enableHTTPS) {
		logger.Fatal("exiting", fmt.Errorf("at least one of enableHTTP and enableHTTPS must be true"))
	}

	if *enableHTTPS {
		if *keyFile == "" {
			logger.Fatal("exiting", fmt.Errorf("keyFile must be provided if enableHTTPS is true"))
		}

		if *certFile == "" {
			logger.Fatal("exiting", fmt.Errorf("certFile must be provided if enableHTTPS is true"))
		}
	}

	if *forceHTTPS && !(*enableHTTP && *enableHTTPS) {
		logger.Fatal("exiting", fmt.Errorf("enableHTTP must be enabled if forceHTTPS is true"))
	}

	if !*dev && (*username == "" || *password == "") {
		logger.Fatal("exiting", fmt.Errorf("must specify -username and -password or turn on dev mode"))
	}

	var tlsConfig *tls.Config
	if *keyFile != "" && *certFile != "" {
		var err error
		tlsConfig, err = createTLSConfig(*keyFile, *certFile)
		if err != nil {
			logger.Fatal("exiting. Failed to create tlsConfig", err)
		}
	}

	cookieHandler := securecookie.New(
		securecookie.GenerateRandomKey(64),
		securecookie.GenerateRandomKey(32),
	)

	templates, err := filesystem.LoadTemplates()
	if err != nil {
		logger.Fatal("exiting", err)
	}

	osHelper := gpos.NewOSHelper(logger)

	loginHandler := login.NewHandler(
		logger,
		templates,
		cookieHandler,
		*cookieMaxAge,
	)

	webcamURL := fmt.Sprintf("%s:%d", *webcamHost, *webcamPort)
	wh := webcam.NewHandler(
		logger,
		webcamURL,
	)

	gpio := gpio.NewGpio(osHelper, logger)

	lh := light.NewHandler(
		logger,
		gpio,
		*gpioLightPin,
	)

	hh := homepage.NewHandler(
		logger,
		templates,
		lh,
		loginHandler,
	)

	dh := door.NewHandler(
		logger,
		osHelper,
		gpio,
		*gpioDoorPin)

	loglevelHandler := loglevel.NewServer(
		logger,
		sink,
	)

	staticFileServer := http.FileServer(static.FS(false))

	rtr := mux.NewRouter()

	rtr.PathPrefix("/static/").Handler(staticFileServer)

	rtr.HandleFunc("/", hh.Handle).Methods("GET")
	rtr.HandleFunc("/webcam", wh.Handle).Methods("GET")

	s := rtr.PathPrefix("/api/v1").Subrouter()
	s.HandleFunc("/toggle", dh.HandleToggle).Methods("POST")
	s.HandleFunc("/light", lh.HandleGet).Methods("GET")
	s.HandleFunc("/light", lh.HandleSet).Methods("POST")
	s.HandleFunc("/loglevel", loglevelHandler.GetMinLevel).Methods("GET")
	s.HandleFunc("/loglevel", loglevelHandler.SetMinLevel).Methods("POST")

	rtr.HandleFunc("/login", loginHandler.LoginGET).Methods("GET")
	rtr.HandleFunc("/login", loginHandler.LoginPOST).Methods("POST")
	rtr.HandleFunc("/logout", loginHandler.LogoutPOST).Methods("POST")

	members := grouper.Members{}
	if *enableHTTPS {
		forceHTTPS := false
		httpsRunner := NewWebRunner(
			*httpsPort,
			logger,
			rtr,
			tlsConfig,
			forceHTTPS,
			*redirectPort,
			*username,
			*password,
			cookieHandler,
		)

		members = append(members, grouper.Member{
			Name:   "https",
			Runner: httpsRunner,
		})
	}

	if *enableHTTP {
		var tlsConfig *tls.Config // nil
		httpRunner := NewWebRunner(
			*httpPort,
			logger,
			rtr,
			tlsConfig,
			*forceHTTPS,
			*redirectPort,
			*username,
			*password,
			cookieHandler,
		)
		members = append(members, grouper.Member{
			Name:   "http",
			Runner: httpRunner,
		})
	}

	group := grouper.NewParallel(os.Kill, members)
	process := ifrit.Invoke(group)

	if *pidFile != "" {
		pid := os.Getpid()
		err = ioutil.WriteFile(*pidFile, []byte(strconv.Itoa(os.Getpid())), 0644)
		if err != nil {
			logger.Fatal("Failed to write pid file", err, lager.Data{
				"pid":     pid,
				"pidFile": *pidFile,
			})
		}
	}
	logger.Info("garagepi started")

	err = <-process.Wait()
	if err != nil {
		logger.Error("Error running garagepi", err)
	}
}