// redirect go goes from here func Redirect(params martini.Params, r render.Render, pool *redis.Pool) { entity := &Entity{} entity.Token = params["token"] err := entity.Retrieve(pool) if err != nil { syslog.Critf("Error: Token not found %s", params["token"]) r.HTML(404, "404", nil) } else { syslog.Critf("Redirect from %s to %s", Get("lilliput.domain", "").(string)+params["token"], entity.Url) r.Redirect(entity.Url, 301) } }
func main() { syslog.Openlog("cinefade", syslog.LOG_PID, syslog.LOG_USER) var action string flag.StringVar(&action, "action", "on", "lights on/off") flag.Parse() bridge := cinefade.GetBridge(false) cinefade.MapRoutes(bridge) // make a http server using the goweb.DefaultHttpHandler() s := &http.Server{ Addr: Address, Handler: goweb.DefaultHttpHandler(), ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, MaxHeaderBytes: 1 << 20, } c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) listener, listenErr := net.Listen("tcp", Address) syslog.Info("Server cinefade started") syslog.Infof("visit: %s", Address) if listenErr != nil { syslog.Critf("Could not listen: %s", listenErr) os.Exit(1) } go func() { for _ = range c { // sig is a ^C, handle it // stop the HTTP server syslog.Info("Stopping the cinefade server...") listener.Close() syslog.Info("Tearing down...") os.Exit(0) } }() syslog.Critf("Error in Serve: %s", s.Serve(listener)) os.Exit(1) }
func poll(client *http.Client, c chan string) { syslog.Info("Launch plex poller") conf := config.NewConfig(EtcDir + "/cinefade.conf") err := conf.Read() if err != nil { syslog.Critf("cannot read config: %v", err) } plexUrl := conf.Get("", "plexUrl") for { select { case <-r: syslog.Info("Exit from plex poller") r <- true return case <-time.After(5000 * time.Millisecond): req, _ := http.NewRequest("GET", plexUrl, nil) resp, err := client.Do(req) if err != nil { syslog.Warningf("can't access plex %v", err) c <- "unknown" } else { defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) var m MediaContainer xml.Unmarshal(body, &m) if err != nil { syslog.Critf("error: %v", err) } switch m.Video.Player.State { case "": c <- "stopped" case "paused": c <- "paused" case "playing": c <- "playing" default: c <- "unknown" } } } } }
func GetBridge(debug bool) *hue.Bridge { conf := config.NewConfig(EtcDir + "/cinefade.conf") err := conf.Read() if err != nil { syslog.Critf("cannot read config: %v", err) os.Exit(1) } hueIpAddr := conf.Get("", "hueIpAddr") hueUser := conf.Get("", "hueUser") if hueIpAddr == "" || hueUser == "" { syslog.Crit("One of configuration hueIpAddr|hueUser not found") } bridge := hue.NewBridge(hueIpAddr, hueUser) if debug { bridge.Debug() } return bridge }
func (entity *Entity) Save(iaddress string, pool *redis.Pool) (*Entity, error) { resource := strings.TrimSpace(iaddress) if len(resource) == 0 { entity.Err = ERROR_NO_URL entity.Message = "url must be provided in request." err := errors.New(entity.Message) return entity, err } u, _ := url.Parse(resource) domains := Get("lilliput.alloweddomain", nil).([]interface{}) flag := true for _, d := range domains { if d.(string) == u.Host { flag = false } } if flag { entity.Err = ERROR_INVALID_DOMAIN entity.Message = "Invalid domain in url" err := errors.New(entity.Message) return entity, err } expression := regexp.MustCompile(EMAIL_REGEX) if expression.MatchString(resource) == false { entity.Err = ERROR_INVALID_URL entity.Message = "Invalid url" err := errors.New(entity.Message) return entity, err } entity.Url = iaddress bytes := make([]byte, 7) db := pool.Get() defer db.Close() for i := 0; i < 5; i++ { rand.Read(bytes) for i, b := range bytes { bytes[i] = ALPHANUM[b%byte(len(ALPHANUM))] } id := string(bytes) if exists, _ := redis.Bool(db.Do("EXISTS", id)); !exists { entity.Token = id break } } if entity.Token == "" { syslog.Critf("Error: failed to generate token") entity.Err = ERROR_TOKEN_GENERATION_FAILED entity.Message = "Faild to generate token please try again." err := errors.New(entity.Message) return entity, err } reply, err := db.Do("SET", entity.Token, entity.Url) if err == nil && reply != "OK" { syslog.Critf("Error: %s", err) entity.Err = ERROR_FAILED_TO_SAVE entity.Message = "Invalid Redis response" err := errors.New(entity.Message) return entity, err } entity.Err = SUCCESS entity.Tiny = Get("lilliput.domain", "").(string) + entity.Token syslog.Critf("Tiny url from %s to %s", entity.Url, entity.Tiny) return entity, nil }