82 lines
1.8 KiB
Go
82 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"path/filepath"
|
|
|
|
"github.com/fasthttp/router"
|
|
"github.com/fsnotify/fsnotify"
|
|
"github.com/valyala/fasthttp"
|
|
"trle5.xyz/gopkg/trpic/src/site"
|
|
)
|
|
|
|
func main() {
|
|
watcher, err := fsnotify.NewWatcher()
|
|
if err != nil { log.Fatal(err) }
|
|
defer watcher.Close()
|
|
|
|
trpic := site.New()
|
|
|
|
paths := []string{
|
|
trpic.Config.GalleryDir,
|
|
"config.yaml",
|
|
"web/templates",
|
|
"web/scripts",
|
|
"web/styles",
|
|
}
|
|
|
|
for _, path := range paths {
|
|
err = watcher.Add(path)
|
|
if err != nil { log.Fatal(path, err) }
|
|
}
|
|
|
|
go func() {
|
|
for {
|
|
select {
|
|
case event, ok := <-watcher.Events:
|
|
if !ok { return }
|
|
switch filepath.ToSlash(filepath.Dir(event.Name)) {
|
|
case "web/templates":
|
|
trpic.LoadTemplates()
|
|
log.Println("Template Reloaded")
|
|
case "web/scripts":
|
|
trpic.LoadScripts()
|
|
log.Println("Script Reloaded")
|
|
case "web/styles":
|
|
trpic.LoadStyles()
|
|
log.Println("Style Reloaded")
|
|
case trpic.Config.GalleryDir:
|
|
trpic.LoadImages()
|
|
log.Println("Image Reloaded")
|
|
default:
|
|
switch filepath.Base(event.Name) {
|
|
case "config.yaml":
|
|
trpic.Reload()
|
|
log.Println("Config Reloaded")
|
|
}
|
|
}
|
|
log.Println("event:", event.Name, event.Op)
|
|
case _, ok := <-watcher.Errors:
|
|
if !ok {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}()
|
|
|
|
r := router.New()
|
|
r.GET("/", trpic.PageHandler)
|
|
r.GET("/noscript", trpic.PageHandler)
|
|
r.GET("/favicon.ico", func(ctx *fasthttp.RequestCtx) {
|
|
fasthttp.ServeFile(ctx, "web/what.png")
|
|
})
|
|
api := r.Group("/api"); {
|
|
api.GET("/", trpic.APIHandler)
|
|
api.GET("/images", trpic.APIImagesHandler)
|
|
}
|
|
r.ServeFiles("/image/{filepath:*}", trpic.Config.GalleryDir)
|
|
r.ServeFiles("/thumbnail/{filepath:*}", trpic.Config.ThumbnailDir)
|
|
log.Println("服务器启动,访问 http://localhost:8080 进行预览")
|
|
log.Fatal(fasthttp.ListenAndServe(":8080", r.Handler))
|
|
}
|