2024-10-26 23:48:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-11-05 12:41:35 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"flag"
|
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-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-10-26 23:48:17 +00:00
|
|
|
func main() {
|
2024-11-05 12:41:35 +00:00
|
|
|
//SETUP FLAGS
|
|
|
|
var debugFlag = flag.Bool("debug", false, "help message for flag n")
|
|
|
|
flag.Parse()
|
2024-11-04 20:25:11 +00:00
|
|
|
|
2024-11-05 12:41:35 +00:00
|
|
|
//INITIALIZE CONFIG
|
|
|
|
err := config.load()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("unable to initialize config", err)
|
|
|
|
}
|
2024-10-26 23:48:17 +00:00
|
|
|
|
2024-11-05 12:41:35 +00:00
|
|
|
//SETUP LOGGER
|
2024-11-04 15:46:56 +00:00
|
|
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
2024-11-05 12:41:35 +00:00
|
|
|
if *debugFlag {
|
|
|
|
logfile, err := openLogfile()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("failed to load config", err)
|
|
|
|
cleanExit(1)
|
|
|
|
}
|
|
|
|
log.SetOutput(logfile)
|
|
|
|
}
|
|
|
|
log.SetFlags(log.LstdFlags | log.Lshortfile | log.Lmicroseconds)
|
|
|
|
log.Println("Welcome to GoPaper!")
|
2024-11-04 20:13:28 +00:00
|
|
|
|
2024-11-05 12:41:35 +00:00
|
|
|
killPrevProc()
|
|
|
|
//create new pidfile
|
|
|
|
pid := os.Getpid()
|
|
|
|
pidfile, err := os.Create(pidfilePath)
|
2024-11-04 20:13:28 +00:00
|
|
|
if err != nil {
|
2024-11-05 12:41:35 +00:00
|
|
|
log.Println("error trying to create pidfile:", err)
|
2024-11-04 15:42:24 +00:00
|
|
|
cleanExit(1)
|
2024-11-04 20:13:28 +00:00
|
|
|
}
|
2024-11-05 12:41:35 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err = binary.Write(buf, binary.LittleEndian, pid)
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
pidfile.Write(buf.Bytes())
|
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-10-29 15:18:21 +00:00
|
|
|
var dir string
|
|
|
|
args := os.Args[1:]
|
|
|
|
if len(args) > 0 {
|
|
|
|
dir = args[0]
|
|
|
|
} else {
|
2024-11-04 20:13:28 +00:00
|
|
|
dir = config.Default
|
2024-10-26 23:48:17 +00:00
|
|
|
}
|
2024-10-29 15:18:21 +00:00
|
|
|
slideshowDir <- dir
|
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
|
|
|
|
|
|
|
func openLogfile() (*os.File, error) {
|
|
|
|
//TODO: log rotation
|
|
|
|
f, err := os.OpenFile(config.Cache+"/logfile", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|
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) {
|
|
|
|
log.Printf("unable to remove sockfile at %v. Error: %v\n", sockfile, err)
|
|
|
|
} 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) {
|
|
|
|
log.Printf("unable to remove pidfile at %v. Error: %v\n", pidfilePath, err)
|
|
|
|
} 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 {
|
|
|
|
log.Println("unable to read pre-existing pidfile", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
//kill process
|
|
|
|
//BUG: kill command doesn't work. prexPid == []
|
|
|
|
err = exec.Command("kill", "-9", string(prexPid)).Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("unable to kill process %v\n", string(prexPid))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
//remove pidfile
|
|
|
|
err = os.Remove(pidfilePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("unable to remove pidfile %v\n", pidfilePath)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|