func main() {
	required_env.Ensure(map[string]string{
		"AWS_ACCESS_KEY_ID":     "",
		"AWS_SECRET_ACCESS_KEY": "",
		"RETHINK_LOC":           "",
		"S3_BUCKET":             "",
		"AWS_REGION":            "us-east-1",
	})

	if os.Getenv("CRON_STRING") != "" {
		c := cron.New()
		err := c.AddFunc(os.Getenv("CRON_STRING"), doBackup)
		if err != nil {
			log.Fatal(err)
		}
		c.Start()
		select {}
	} else {
		doBackup()
	}
}
Ejemplo n.º 2
0
func main() {
	required_env.Ensure(map[string]string{"ROUTER": "", "PORT": "3000", "COUNTRIES": "US,AU"})

	countries := strings.Split(os.Getenv("COUNTRIES"), ",")
	allowed_countries := map[string]bool{"": true}

	for _, key := range countries {
		allowed_countries[key] = true
	}

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		target := r.FormValue("country")

		if !allowed_countries[target] {
			w.WriteHeader(403)
			w.Write([]byte("That country is not permitted!"))
			return
		}

		if target != "" {
			fmt.Println(target)
			err := fly(target, w)
			if err != nil {
				w.Write([]byte(err.Error()))
				return
			}
		}
		t, err := template.New("foo").Parse(`
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Jetlag</title>
  </head>
  <body>
    {{if .LastAction}}
      <h4>Just flew to {{ .LastAction }}</h4>
    {{end}}
    {{range .Countries}}
      <div>
        <form action="/" method="POST">
          <input type="hidden" name="country" value="{{ . }}"></input>
          <button type="submit">{{ . }}</button>
        </form>
      </div>
    {{end}}
  </body>
</html>
`)
		if err != nil {
			w.Write([]byte(err.Error()))
			return
		}
		data := struct {
			Countries  []string
			LastAction string
		}{
			Countries:  countries,
			LastAction: target,
		}
		err = t.Execute(w, data)
		if err != nil {
			w.Write([]byte(err.Error()))
			return
		}
	})
	log.Fatal(http.ListenAndServe(":"+os.Getenv("PORT"), nil))
}