gopaper/main.go

143 lines
3.3 KiB
Go
Raw Normal View History

2024-10-26 23:48:17 +00:00
package main
import (
2024-11-05 12:41:35 +00:00
"flag"
2024-11-05 21:16:48 +00:00
"github.com/natefinch/lumberjack"
2024-11-04 20:13:28 +00:00
"log"
2024-10-26 23:48:17 +00:00
"os"
2024-11-05 12:41:35 +00:00
"os/exec"
2024-11-05 14:28:39 +00:00
"strconv"
2024-10-28 23:29:20 +00:00
"sync"
2024-10-26 23:48:17 +00:00
)
2024-10-28 23:29:20 +00:00
var config Config
2024-11-04 15:46:56 +00:00
const sockfile = "/tmp/gopaper.sock"
2024-11-05 12:41:35 +00:00
const pidfilePath = "/tmp/gopaper.pid"
2024-11-04 15:46:56 +00:00
2024-11-05 15:15:25 +00:00
var clog = log.New(os.Stdout, "## ", 0)
var elog = log.New(os.Stdout, "ERROR: ", 3)
2024-10-26 23:48:17 +00:00
func main() {
2024-11-05 12:41:35 +00:00
//SETUP FLAGS
2024-11-05 21:21:52 +00:00
var debugFlag = flag.Bool("debug", false, "debug mode sends logs to $HOME/.gopaper/logfile")
var dirFlag = flag.String("dir", "", "the directory from which to randomly select images")
2024-11-05 12:41:35 +00:00
flag.Parse()
2024-11-04 20:25:11 +00:00
2024-11-05 15:15:25 +00:00
clog.Println("Welcome to GoPaper!")
2024-11-04 20:13:28 +00:00
2024-11-05 15:15:25 +00:00
//INIT CONFIG
err := config.load()
2024-11-04 20:13:28 +00:00
if err != nil {
2024-11-05 15:15:25 +00:00
elog.Println("unable to initialize config", err)
2024-11-04 20:13:28 +00:00
}
2024-11-05 15:15:25 +00:00
handleProcesses()
setupLogging(*debugFlag)
2024-10-26 23:48:17 +00:00
2024-10-30 18:22:03 +00:00
var waitGroup sync.WaitGroup
waitGroup.Add(1)
slideshowDir := make(chan string)
2024-11-04 15:46:56 +00:00
2024-11-04 15:58:54 +00:00
func() {
defer func() {
if r := recover(); r != nil {
2024-11-05 12:41:35 +00:00
log.Println("Panic recovered at top level", r)
2024-11-04 15:58:54 +00:00
cleanExit(1)
}
}()
go server(slideshowDir)
go slideshow(slideshowDir)
}()
2024-10-30 18:22:03 +00:00
2024-11-05 21:02:46 +00:00
slideshowDir <- *dirFlag
2024-10-30 18:22:03 +00:00
2024-10-28 23:29:20 +00:00
waitGroup.Wait()
2024-10-26 23:48:17 +00:00
}
2024-11-04 20:13:28 +00:00
2024-11-04 20:25:11 +00:00
func cleanExit(code int) {
2024-11-05 12:41:35 +00:00
if code == 1 {
log.Println("shutting down gracefully after errors")
} else {
log.Println("shutting down gracefully")
}
err := os.Remove(sockfile)
if err != nil && !os.IsNotExist(err) {
2024-11-05 15:15:25 +00:00
elog.Printf("unable to remove sockfile at %v. Error: %v\n", sockfile, err)
2024-11-05 12:41:35 +00:00
} else if !os.IsNotExist(err) {
log.Println("sockfile removed")
} else {
log.Println("no need to remove sockfile -- it doesn't exist")
}
err = os.Remove(pidfilePath)
if err != nil && !os.IsNotExist(err) {
2024-11-05 15:15:25 +00:00
elog.Printf("unable to remove pidfile at %v. Error: %v\n", pidfilePath, err)
2024-11-05 12:41:35 +00:00
} else if !os.IsNotExist(err) {
log.Println("pidfile removed")
} else {
log.Println("no need to remove pidfile -- it doesn't exist")
}
2024-11-04 20:25:11 +00:00
os.Exit(code)
}
2024-11-05 12:41:35 +00:00
func killPrevProc() error {
//check if pidfile already exists
if _, err := os.Stat(pidfilePath); os.IsNotExist(err) {
return nil
}
//if it does, kill the process and delete the pidfile
prexPid, err := os.ReadFile(pidfilePath)
if err != nil {
2024-11-05 15:15:25 +00:00
elog.Println("unable to read pre-existing pidfile", err)
2024-11-05 12:41:35 +00:00
return err
}
//kill process
2024-11-05 14:28:39 +00:00
err = exec.Command("kill", string(prexPid)).Run()
2024-11-05 12:41:35 +00:00
if err != nil {
2024-11-05 15:15:25 +00:00
elog.Printf("Error attempting to kill process %v: %v\n", string(prexPid), err)
2024-11-05 12:41:35 +00:00
}
//remove pidfile
err = os.Remove(pidfilePath)
2024-11-05 15:15:25 +00:00
if err != nil && !os.IsNotExist(err) {
elog.Printf("unable to remove pidfile at %v. Error: %v\n", pidfilePath, err)
2024-11-05 12:41:35 +00:00
}
return nil
}
2024-11-05 15:15:25 +00:00
func handleProcesses() {
err := killPrevProc()
if err != nil {
elog.Println("error attempting to kill pre-existing process: ", err)
os.Exit(1)
}
//create new pidfile
pid := strconv.FormatInt(int64(os.Getpid()), 10)
pidfile, err := os.Create(pidfilePath)
if err != nil {
elog.Println("error trying to create pidfile:", err)
os.Exit(1)
}
pidfile.WriteString(pid)
}
func setupLogging(debug bool) {
2024-11-05 21:02:46 +00:00
clog.Printf("debug: %v", debug)
2024-11-05 15:15:25 +00:00
//SETUP LOGGERS
if debug {
2024-11-05 21:16:48 +00:00
logfile := config.Cache + "logfile"
clog.Printf("Logging to %v", logfile)
output := &lumberjack.Logger{
Filename: logfile,
MaxSize: 1, // megabytes after which new file is created
MaxBackups: 3, // number of backups
MaxAge: 28, //days
2024-11-05 15:15:25 +00:00
}
2024-11-05 21:16:48 +00:00
log.SetOutput(output)
clog.SetOutput(output)
elog.SetOutput(output)
2024-11-05 15:15:25 +00:00
}
elog.SetFlags(log.LstdFlags | log.Lshortfile)
log.SetFlags(log.LstdFlags)
}