mirror of
https://github.com/nxshock/gallery.git
synced 2024-11-28 00:21:01 +05:00
59 lines
912 B
Go
59 lines
912 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"log"
|
||
|
"math/rand"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
"os/signal"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
previewCache = NewPreviewCache("cache.zkv")
|
||
|
config *Config
|
||
|
semaphore chan struct{}
|
||
|
)
|
||
|
|
||
|
func init() {
|
||
|
log.SetFlags(0)
|
||
|
rand.Seed(time.Now().Unix())
|
||
|
|
||
|
configPath := defaultConfigPath
|
||
|
if len(os.Args) > 1 {
|
||
|
configPath = os.Args[1]
|
||
|
}
|
||
|
|
||
|
var err error
|
||
|
|
||
|
config, err = loadConfig(configPath)
|
||
|
if err != nil {
|
||
|
log.Fatalln(err)
|
||
|
}
|
||
|
|
||
|
err = os.Chdir(config.WorkingDirectory)
|
||
|
if err != nil {
|
||
|
log.Fatalln(err)
|
||
|
}
|
||
|
|
||
|
semaphore = make(chan struct{}, config.ProcessCount)
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
go func() {
|
||
|
http.HandleFunc("/preview/", previewHandler)
|
||
|
http.HandleFunc("/view/", viewHandler)
|
||
|
http.HandleFunc("/", rootHandler)
|
||
|
log.Fatalln(http.ListenAndServe(":8080", nil))
|
||
|
}()
|
||
|
|
||
|
c := make(chan os.Signal, 1)
|
||
|
signal.Notify(c, os.Interrupt)
|
||
|
<-c
|
||
|
|
||
|
err := previewCache.Save()
|
||
|
if err != nil {
|
||
|
log.Fatalln(err)
|
||
|
}
|
||
|
}
|