-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
144 lines (118 loc) · 3.01 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"context"
"crypto/tls"
"errors"
"flag"
"fmt"
"io"
"io/fs"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"reishimanfr/goimg/database"
"reishimanfr/goimg/flags"
"reishimanfr/goimg/routes"
"time"
"github.com/fsnotify/fsnotify"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"gorm.io/gorm"
)
func watchFiles(db *gorm.DB, logger *zap.Logger) {
path := filepath.Join(flags.BasePth, "files")
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
if err := watcher.Add(path); err != nil {
log.Fatal(err)
}
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if (event.Op&fsnotify.Remove == fsnotify.Remove) || (event.Op&fsnotify.Rename == fsnotify.Rename) {
if _, err := os.Stat(event.Name); os.IsNotExist(err) {
name := filepath.Base(event.Name)
if err := db.Delete(&database.FileRecord{}, "filename = ?", name).Error; err != nil {
logger.Error("Error while auto-deleting record", zap.String("Filename", event.Name), zap.Error(err))
}
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("Error:", err)
}
}
}
func main() {
flag.Parse()
config := zap.NewDevelopmentConfig()
logger, err := config.Build()
if err != nil {
panic(fmt.Errorf("failed to initialize logger: %v", err))
}
fileDirPath := filepath.Join(flags.BasePth, "files")
// Create file directory if it doesn't exist
if _, err := os.Stat(fileDirPath); errors.Is(err, fs.ErrNotExist) {
if err := os.Mkdir(fileDirPath, 0755); err != nil {
logger.Fatal("Failed to create file directory", zap.String("Path", fileDirPath), zap.Error(err))
}
logger.Debug("Created file directory", zap.String("Path", fileDirPath))
}
db, err := database.Init()
if err != nil {
logger.Fatal("Error while initializing database", zap.Error(err))
}
r := gin.Default()
server := &http.Server{
Addr: ":" + *flags.Port,
Handler: r,
TLSConfig: &tls.Config{
MinVersion: tls.VersionTLS12, // Enforce TLS 1.2 or higher
},
}
if *flags.Secure {
server.Addr = ":443"
}
if !*flags.Dev {
gin.SetMode(gin.ReleaseMode)
gin.DefaultWriter = io.Discard
}
h := routes.Handler{
Router: r,
Logger: logger,
Db: db,
}
h.Init()
go func() {
if *flags.Secure {
if err := server.ListenAndServeTLS(*flags.SslCertPath, *flags.SslKeyPath); err != nil && err != http.ErrServerClosed {
log.Fatal("Failed to start server", zap.Error(err))
}
} else {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal("Failed to start server", zap.Error(err))
}
}
}()
logger.Info("Server started on " + server.Addr)
watchFiles(db, logger)
q := make(chan os.Signal, 1)
signal.Notify(q, os.Interrupt)
<-q
logger.Info("Gracefully shutting down server...")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatal("Server forced to shutdown", zap.Error(err))
}
}