Пример #1
0
func main() {
	log.Printf("gotcha %s!\n", version)

	if len(os.Args) >= 2 {
		cmd := os.Args[1]
		args := os.Args[2:]
		switch cmd {
		case "new":
			if len(args) == 0 {
				log.Fatalf("Missing application name, e.g. 'gotcha new MyApp'")
			}
			new(args[0])
		case "install":
			if len(args) > 0 {
				log.Fatalf("No additional arguments required for install: %s", args)
			}
			if _, err := os.Stat("Makefile"); os.IsNotExist(err) {
				log.Fatalf("Current directory doesn't appear to be a Gotcha application")
			}
			out, err := exec.Command("make").Output()
			if err != nil {
				log.Fatalf("Error installing application: %s", err)
			}
			log.Printf("Install successful: %s", out)
		default:
			log.Fatalf("Unrecognised command: %s\n", cmd)
		}
	}
}
Пример #2
0
// AuthFile sets Authorised to a function which validates against file
func AuthFile(file string) {
	users = make(map[string]string)

	b, err := ioutil.ReadFile(file)
	if err != nil {
		log.Fatalf("[HTTP] Error reading auth-file: %s", err)
		// FIXME - go-log
		os.Exit(1)
	}

	buf := bytes.NewBuffer(b)

	for {
		l, err := buf.ReadString('\n')
		l = strings.TrimSpace(l)
		if len(l) > 0 {
			p := strings.SplitN(l, ":", 2)
			if len(p) < 2 {
				log.Fatalf("[HTTP] Error reading auth-file, invalid line: %s", l)
				// FIXME - go-log
				os.Exit(1)
			}
			users[p[0]] = p[1]
		}
		switch {
		case err == io.EOF:
			break
		case err != nil:
			log.Fatalf("[HTTP] Error reading auth-file: %s", err)
			// FIXME - go-log
			os.Exit(1)
			break
		}
		if err == io.EOF {
			break
		} else if err != nil {
		}
	}

	log.Printf("[HTTP] Loaded %d users from %s", len(users), file)

	Authorised = func(u, pw string) bool {
		hpw, ok := users[u]
		if !ok {
			return false
		}

		err := bcrypt.CompareHashAndPassword([]byte(hpw), []byte(pw))
		if err != nil {
			return false
		}

		return true
	}
}
Пример #3
0
// Listen binds to httpBindAddr
func Listen(httpBindAddr string, Asset func(string) ([]byte, error), exitCh chan int, registerCallback func(http.Handler)) {
	log.Info("[HTTP] Binding to address: %s", httpBindAddr)

	pat := pat.New()
	registerCallback(pat)

	f := func(w http.ResponseWriter, req *http.Request) {
		if Authorised == nil {
			pat.ServeHTTP(w, req)
			return
		}

		u, pw, ok := req.BasicAuth()
		if !ok || !Authorised(u, pw) {
			w.Header().Set("WWW-Authenticate", "Basic")
			w.WriteHeader(401)
			return
		}
		pat.ServeHTTP(w, req)
	}

	err := http.ListenAndServe(httpBindAddr, http.HandlerFunc(f))
	if err != nil {
		log.Fatalf("[HTTP] Error binding to address %s: %s", httpBindAddr, err)
	}
}
Пример #4
0
func createDir(dir string) {
	log.Printf("Creating directory %s", dir)

	err := os.MkdirAll(dir, 0777)
	if err != nil {
		log.Fatalf("Error creating directory %s: %s", dir, err)
	}
}
Пример #5
0
// SetBrightness takes a float value betweem [0,1] and sets the global screen brightness.
func SetBrightness(vNorm float64) {
	v := C.float(constrain(vNorm, 0, 1))
	log.Debug("Setting brightness to %v (%v)", vNorm, v)

	if res := C.setBrightness(v); int(res) != 0 {
		log.Fatalf("Failed to set brightness: %d", res)
	}
}
Пример #6
0
// SetVolume takes a float value between [0,1] and sets the global output volume.
func SetVolume(vNorm float64) {
	v := C.float(constrain(vNorm, 0, 1))
	log.Debug("Setting volume to %v (%v)", vNorm, v)

	if res := C.setVolume(v); int(res) != 0 {
		log.Fatalf("Failed to set volume: %d", res)
	}
}
Пример #7
0
func writeAsset(input string, output string) {
	log.Printf("Writing asset %s to %s", input, output)

	f, err := os.Create(filepath.FromSlash(output))
	if err != nil {
		log.Fatalf("Error creating %s: %s", output, err)
	}

	bytes, err := Asset(input)
	if err != nil {
		log.Fatalf("Error loading asset %s: %s", input, err)
	}

	_, err = f.Write(bytes)
	if err != nil {
		log.Fatalf("Error writing output %s: %s", output, err)
	}
}
Пример #8
0
Файл: main.go Проект: djui/necd
func actionMain(c *cli.Context) {
	if c.Bool("daemon") {
		if err := Daemonize("necd"); err != nil {
			log.Fatalf("Failed to daemonize: %v", err)
		}
	} else {
		KeepAlive(runLoop)
	}
}
Пример #9
0
Файл: main.go Проект: djui/necd
func atoi(a string, def int) int {
	if a == "" {
		return def
	}
	i, err := strconv.Atoi(a)
	if err != nil {
		log.Fatalf("Failed to parse value: %s", a)
	}
	return i
}
Пример #10
0
func (app *App) Start() *App {
	app.Server = &nethttp.Server{
		Addr:    app.Config.Listen,
		Handler: app.Router,
	}
	log.Printf("Starting application on %s", app.Config.Listen)
	go func() {
		err := app.Server.ListenAndServe()
		if err != nil {
			log.Fatalf("Error binding to %s: %s", app.Config.Listen, err)
		}
	}()
	return app
}
Пример #11
0
func main() {
	if len(os.Args) > 1 && os.Args[1] == "sendmail" {
		args := os.Args
		os.Args = []string{args[0]}
		if len(args) > 2 {
			os.Args = append(os.Args, args[2:]...)
		}
		cmd.Go()
		return
	}

	if len(os.Args) > 1 && os.Args[1] == "bcrypt" {
		var pw string
		if len(os.Args) > 2 {
			pw = os.Args[2]
		} else {
			// TODO: read from stdin
		}
		b, err := bcrypt.GenerateFromPassword([]byte(pw), 4)
		if err != nil {
			log.Fatalf("error bcrypting password: %s", err)
			os.Exit(1)
		}
		fmt.Println(string(b))
		os.Exit(0)
	}

	configure()

	if comconf.AuthFile != "" {
		http.AuthFile(comconf.AuthFile)
	}

	exitCh = make(chan int)
	if uiconf.UIBindAddr == apiconf.APIBindAddr {
		cb := func(r gohttp.Handler) {
			web.CreateWeb(uiconf, r.(*pat.Router), assets.Asset)
			api.CreateAPIv1(apiconf, r.(*pat.Router))
			api.CreateAPIv2(apiconf, r.(*pat.Router))
		}
		go http.Listen(uiconf.UIBindAddr, assets.Asset, exitCh, cb)
	} else {
		cb1 := func(r gohttp.Handler) {
			api.CreateAPIv1(apiconf, r.(*pat.Router))
			api.CreateAPIv2(apiconf, r.(*pat.Router))
		}
		cb2 := func(r gohttp.Handler) {
			web.CreateWeb(uiconf, r.(*pat.Router), assets.Asset)
		}
		go http.Listen(apiconf.APIBindAddr, assets.Asset, exitCh, cb1)
		go http.Listen(uiconf.UIBindAddr, assets.Asset, exitCh, cb2)
	}
	go smtp.Listen(apiconf, exitCh)

	for {
		select {
		case <-exitCh:
			log.Printf("Received exit signal")
			os.Exit(0)
		}
	}
}
Пример #12
0
Файл: util.go Проект: djui/necd
func AssertNoErr(err error, msg string) {
	if err != nil {
		log.Fatalf("%s: %v", msg, err)
	}
}