func main() { flag.Parse() // called here in main() to allow other packages to set flags in their inits caddy.AppName = appName caddy.AppVersion = appVersion // set up process log before anything bad happens switch logfile { case "stdout": log.SetOutput(os.Stdout) case "stderr": log.SetOutput(os.Stderr) case "": log.SetOutput(ioutil.Discard) default: file, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644) if err != nil { log.Fatalf("Error opening log file: %v", err) } log.SetOutput(file) } if version { fmt.Printf("%s %s\n", caddy.AppName, caddy.AppVersion) os.Exit(0) } if revoke != "" { err := letsencrypt.Revoke(revoke) if err != nil { log.Fatal(err) } fmt.Printf("Revoked certificate for %s\n", revoke) os.Exit(0) } // Set CPU cap err := setCPU(cpu) if err != nil { mustLogFatal(err) } // Get Caddyfile input caddyfile, err := caddy.LoadCaddyfile(loadCaddyfile) if err != nil { mustLogFatal(err) } // Start your engines err = caddy.Start(caddyfile) if err != nil { if caddy.IsRestart() { log.Printf("[ERROR] Upon starting %s: %v", appName, err) } else { mustLogFatal(err) } } // Twiddle your thumbs caddy.Wait() }
func main() { flag.Parse() // called here in main() to allow other packages to set flags in their inits caddy.AppName = appName caddy.AppVersion = appVersion acme.UserAgent = appName + "/" + appVersion // set up process log before anything bad happens switch logfile { case "stdout": log.SetOutput(os.Stdout) case "stderr": log.SetOutput(os.Stderr) case "": log.SetOutput(ioutil.Discard) default: log.SetOutput(&lumberjack.Logger{ Filename: logfile, MaxSize: 100, MaxAge: 14, MaxBackups: 10, }) } if revoke != "" { err := https.Revoke(revoke) if err != nil { log.Fatal(err) } fmt.Printf("Revoked certificate for %s\n", revoke) os.Exit(0) } if version { fmt.Printf("%s %s\n", caddy.AppName, caddy.AppVersion) os.Exit(0) } // Set CPU cap err := setCPU(cpu) if err != nil { mustLogFatal(err) } // Get Caddyfile input caddyfile, err := caddy.LoadCaddyfile(loadCaddyfile) if err != nil { mustLogFatal(err) } // Start your engines err = caddy.Start(caddyfile) if err != nil { mustLogFatal(err) } // Twiddle your thumbs caddy.Wait() }
func main() { setupConfiguration() // Load state if _, err := os.Stat("state.json"); err == nil { dat, err := ioutil.ReadFile("state.json") if err != nil { panic(err) } json.Unmarshal(dat, &statevar) fmt.Println("\n*******\nLast song:") fmt.Println(statevar.CurrentSong) fmt.Println("*******\n") statevar.IsPlaying = false statevar.SongList = []string{} statevar.LastMuted = 0 statevar.IsMuted = false statevar.IndexPage = "" } else { if len(conf.MusicFolders) == 0 { executable := strings.Split(os.Args[0], "\\") executable_name := executable[len(executable)-1] fmt.Println("Run \"" + executable_name + " --help\" to learn how to add a folder of music") os.Exit(0) } statevar = State{ SongMap: make(map[string]Song), SongList: []string{}, PathList: make(map[string]bool), SongStartTime: 0, IsPlaying: false, CurrentSong: "None", CurrentSongIndex: 0, LastMuted: 0, IsMuted: false, IndexPage: "", } } statevar.IPAddress = GetLocalIP() statevar.Port = conf.Server.Port // Load Mp3s if len(conf.MusicFolders) > 0 { for _, folder := range conf.MusicFolders { loadMp3s(folder) } } // Load song list for k, _ := range statevar.SongMap { statevar.SongList = append(statevar.SongList, k) } statevar.SongList.Sort() // Load index page index_contents, err := ioutil.ReadFile("./templates/index.html") if err != nil { panic(err) } statevar.IndexPage = string(index_contents) skipTrack(statevar.CurrentSongIndex) mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { //defer timeTrack(time.Now(), r.RemoteAddr+" /") html_response := statevar.IndexPage html_response = strings.Replace(html_response, "{{ data['random_integer'] }}", strconv.Itoa(rand.Intn(10000)), -1) html_response = strings.Replace(html_response, "{{ data['check_up_wait_time'] }}", strconv.Itoa(conf.Client.CheckupWaitTime), -1) html_response = strings.Replace(html_response, "{{ data['max_sync_lag'] }}", strconv.Itoa(conf.Client.MaxSyncLag), -1) html_response = strings.Replace(html_response, "{{ data['message'] }}", "Syncing...", -1) html_response = strings.Replace(html_response, "{{ data['playlist_html'] | safe }}", getPlaylistHTML(), -1) html_response = strings.Replace(html_response, "{{ data['sound_url'] }}", "http://"+statevar.IPAddress+":"+strconv.Itoa(conf.Server.Port+1), -1) fmt.Fprintf(w, html_response) }) mux.HandleFunc("/sound.mp3", func(w http.ResponseWriter, r *http.Request) { defer timeTrack(time.Now(), r.RemoteAddr+" /sound.mp3") w.Header().Set("Content-Type", "audio/mpeg") w.Write([]byte(rawSongData)) }) mux.HandleFunc("/static/", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, r.URL.Path[1:]) }) mux.HandleFunc("/sync", SyncRequest) mux.HandleFunc("/nextsong", NextSongRequest) //http.ListenAndServe(":5000", nil) for _, k := range conf.Autostart { fmt.Println(k) response, err := runSSHCommand(k, "pkill -9 midori </dev/null > log 2>&1 &") fmt.Println(response) fmt.Println(err) } for _, k := range conf.Autostart { fmt.Println("Running autostart...") fmt.Println(k) cmd := "xinit /usr/bin/midori -a http://" + statevar.IPAddress + ":" + strconv.Itoa(statevar.Port) + "/ </dev/null > log 2>&1 &" fmt.Println(cmd) response, err := runSSHCommand(k, cmd) fmt.Println(response) fmt.Println(err) } go graceful.Run(":"+strconv.Itoa(statevar.Port), 10*time.Second, mux) caddy.AppName = appName caddy.AppVersion = appVersion // Get Caddyfile input caddyfile, err := caddy.LoadCaddyfile(loadCaddyfile) if err != nil { panic(err) } // Start your engines err = caddy.Start(caddyfile) if err != nil { panic(err) } fmt.Println("\n\n######################################################################") fmt.Printf("# musicsaur - version %s\n", appVersion) fmt.Printf("# Starting server with %d songs\n", len(statevar.SongList)) fmt.Println("# To use, open a browser to http://" + statevar.IPAddress + ":" + strconv.Itoa(statevar.Port)) fmt.Println("# To stop server, use Ctl + C") fmt.Println("######################################################################\n\n") // Twiddle your thumbs caddy.Wait() }
} // Get Caddyfile input caddyfile, err := caddy.LoadCaddyfile(loadCaddyfile) if err != nil { mustLogFatal(err) } // Start your engines err = caddy.Start(caddyfile) if err != nil { mustLogFatal(err) } // Twiddle your thumbs caddy.Wait() }, } func init() { caddy.AppName = appName caddy.AppVersion = appVersion caddy.TrapSignals() RootCmd.PersistentFlags().BoolVar(&letsencrypt.Agreed, "agree", false, "Agree to Let's Encrypt Subscriber Agreement") RootCmd.PersistentFlags().StringVar(&letsencrypt.CAUrl, "ca", "https://acme-staging.api.letsencrypt.org/directory", "Certificate authority ACME server") RootCmd.PersistentFlags().StringVar(&conf, "conf", "", "Configuration file to use (default="+caddy.DefaultConfigFile+")") RootCmd.PersistentFlags().StringVar(&cpu, "cpu", "100%", "CPU cap") RootCmd.PersistentFlags().StringVar(&letsencrypt.DefaultEmail, "email", "", "Default Let's Encrypt account email address") RootCmd.PersistentFlags().DurationVar(&caddy.GracefulTimeout, "grace", 5*time.Second, "Maximum duration of graceful shutdown") RootCmd.PersistentFlags().StringVar(&caddy.Host, "host", caddy.DefaultHost, "Default host")