Files
ufile/main.go
T

58 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"context"
"embed"
"flag"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
)
//go:embed all:frontend/dist
var frontendAssets embed.FS
func main() {
dataPath := flag.String("data", "data", "文件保存目录")
dbPath := flag.String("db", "", "SQLite 数据库路径(默认 <data>/data.db)")
address := flag.String("address", "0.0.0.0:6616", "监听地址")
flag.Parse()
if *dbPath == "" {
*dbPath = filepath.Join(*dataPath, "data.db")
}
if err := os.MkdirAll(filepath.Dir(*dbPath), 0o750); err != nil {
log.Fatal(err)
}
web, err := fs.Sub(frontendAssets, "frontend/dist")
if err != nil {
log.Fatal(err)
}
app, initialPassword, err := newApp(*dbPath, *dataPath, web)
if err != nil {
log.Fatal(err)
}
if initialPassword != "" {
fmt.Printf("\n首次启动管理员账号:admin\n随机密码:%s\n登录后必须立即修改密码。\n\n", initialPassword)
}
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
go app.runCleanup(ctx)
server := &http.Server{Addr: *address, Handler: app.router(), ReadHeaderTimeout: 10 * time.Second}
go func() {
<-ctx.Done()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_ = server.Shutdown(shutdownCtx)
}()
log.Printf("ufile listening on %s", *address)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}