-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
79 lines (62 loc) · 1.53 KB
/
main.go
File metadata and controls
79 lines (62 loc) · 1.53 KB
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
/**
* @Time : 2025/4/22 14:22
* @File : main.go
* @Software: share-text
* @Author : Mr.Fang
* @Description: 共享剪切板应用
*/
package main
import (
"embed"
"flag"
"fmt"
"net/http"
"share-text/api"
"share-text/database"
"share-text/handles"
"share-text/job"
)
// 嵌入声明
//go:embed static/*
var content embed.FS
var port string
func init() {
flag.StringVar(&port, "p", "9999", "指定端口 0-65535")
// 解析命令行参数
flag.Parse()
// 初始化数据库
database.InitDB()
// 启动定时任务
job.DeleteTimeoutData()
}
func main() {
// 创建一个新的 mux 路由器
mux := http.NewServeMux()
// 静态文件处理器
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data, err := content.ReadFile("static/index.html")
if err != nil {
http.Error(w, "无法加载页面:"+err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(data)
})
mux.HandleFunc("/logo.png", func(w http.ResponseWriter, r *http.Request) {
data, err := content.ReadFile("static/logo.png")
if err != nil {
return
}
w.Header().Set("Content-Type", "image/png")
w.Write(data)
})
mux.HandleFunc("/content", func(writer http.ResponseWriter, request *http.Request) {
api.Content(writer, request)
})
fmt.Printf("http://127.0.0.1:%s\n", port)
// 应用中间件
handler := handles.Middleware(mux)
if err := http.ListenAndServe(fmt.Sprintf(":%s", port), handler); err != nil {
fmt.Println("无法启动", err)
}
}