-
Notifications
You must be signed in to change notification settings - Fork 43
/
server.go
163 lines (155 loc) · 5.71 KB
/
server.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"flag"
"fmt"
"github.com/beancount-gs/script"
"github.com/beancount-gs/service"
"github.com/gin-gonic/gin"
"io"
"net/http"
"os"
)
func InitServerFiles() error {
dataPath := script.GetServerConfig().DataPath
// 账本目录不存在,则创建
if dataPath != "" && !script.FileIfExist(dataPath) {
return script.MkDir(dataPath)
}
return nil
}
func LoadServerCache() error {
err := script.LoadLedgerConfigMap()
if err != nil {
return err
}
return script.LoadLedgerAccountsMap()
}
func AuthorizedHandler() gin.HandlerFunc {
return func(c *gin.Context) {
ledgerId := c.GetHeader("ledgerId")
ledgerConfig := script.GetLedgerConfig(ledgerId)
if ledgerConfig != nil {
c.Set("LedgerConfig", ledgerConfig)
c.Next()
} else {
service.Unauthorized(c)
c.Abort()
}
}
}
func RegisterRouter(router *gin.Engine) {
// fix wildcard and static file router conflict, https://github.com/gin-gonic/gin/issues/360
router.GET("/", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "/web")
})
router.StaticFS("/web", http.Dir("./public"))
router.GET("/api/version", service.QueryVersion)
router.POST("/api/check", service.CheckBeancount)
router.GET("/api/config", service.QueryServerConfig)
router.POST("/api/config", service.UpdateServerConfig)
router.GET("/api/ledger", service.QueryLedgerList)
router.POST("/api/ledger", service.OpenOrCreateLedger)
authorized := router.Group("/api/auth/")
authorized.Use(AuthorizedHandler())
{
// need authorized
authorized.GET("/account/valid", service.QueryValidAccount)
authorized.GET("/account/all", service.QueryAllAccount)
authorized.GET("/account/type", service.QueryAccountType)
authorized.POST("/account", service.AddAccount)
authorized.POST("/account/type", service.AddAccountType)
authorized.POST("/account/close", service.CloseAccount)
authorized.POST("/account/icon", service.ChangeAccountIcon)
authorized.POST("/account/balance", service.BalanceAccount)
authorized.POST("/account/refresh", service.RefreshAccountCache)
authorized.POST("/commodity/price", service.SyncCommodityPrice)
authorized.GET("/commodity/currencies", service.QueryAllCurrencies)
authorized.GET("/stats/months", service.MonthsList)
authorized.GET("/stats/total", service.StatsTotal)
authorized.GET("/stats/payee", service.StatsPayee)
authorized.GET("/stats/account/percent", service.StatsAccountPercent)
authorized.GET("/stats/account/trend", service.StatsAccountTrend)
authorized.GET("/stats/account/balance", service.StatsAccountBalance)
authorized.GET("/stats/account/flow", service.StatsAccountSankey)
authorized.GET("/stats/month/total", service.StatsMonthTotal)
authorized.GET("/stats/month/calendar", service.StatsMonthCalendar)
authorized.GET("/stats/commodity/price", service.StatsCommodityPrice)
authorized.GET("/transaction/detail", service.QueryTransactionDetailById)
authorized.GET("/transaction/raw", service.QueryTransactionRawTextById)
authorized.GET("/transaction", service.QueryTransactions)
authorized.POST("/transaction", service.AddTransactions)
authorized.POST("/transaction/raw", service.UpdateTransactionRawTextById)
authorized.DELETE("/transaction", service.DeleteTransactionById)
authorized.POST("/transaction/batch", service.AddBatchTransactions)
authorized.GET("/transaction/payee", service.QueryTransactionPayees)
authorized.GET("/transaction/template", service.QueryTransactionTemplates)
authorized.POST("/transaction/template", service.AddTransactionTemplate)
authorized.DELETE("/transaction/template", service.DeleteTransactionTemplate)
authorized.GET("/event/all", service.GetAllEvents)
authorized.POST("/event", service.AddEvent)
authorized.DELETE("/event", service.DeleteEvent)
authorized.GET("/tags", service.QueryTags)
authorized.GET("/file/dir", service.QueryLedgerSourceFileDir)
authorized.GET("/file/content", service.QueryLedgerSourceFileContent)
authorized.POST("/file", service.UpdateLedgerSourceFileContent)
authorized.POST("/import/alipay", service.ImportAliPayCSV)
authorized.POST("/import/wx", service.ImportWxPayCSV)
authorized.POST("/import/icbc", service.ImportICBCCSV)
authorized.POST("/import/abc", service.ImportABCCSV)
authorized.GET("/ledger/check", service.CheckLedger)
authorized.DELETE("/ledger", service.DeleteLedger)
}
}
func main() {
var secret string
var port int
flag.StringVar(&secret, "secret", "", "服务器密钥")
flag.IntVar(&port, "p", 10000, "端口号")
flag.Parse()
// 读取配置文件
err := script.LoadServerConfig()
if err != nil {
script.LogSystemError("Failed to load server config, " + err.Error())
return
}
serverConfig := script.GetServerConfig()
// 若 DataPath == "" 则配置未初始化
if serverConfig.DataPath != "" {
// 初始化账本文件结构
err = InitServerFiles()
if err != nil {
script.LogSystemError("Failed to init server files, " + err.Error())
return
}
// 加载缓存
err = LoadServerCache()
if err != nil {
script.LogSystemError("Failed to load server cache, " + err.Error())
return
}
}
// gin 日志设置
gin.DisableConsoleColor()
fs, _ := os.Create("logs/gin.log")
gin.DefaultWriter = io.MultiWriter(fs, os.Stdout)
router := gin.Default()
// 注册路由
RegisterRouter(router)
portStr := fmt.Sprintf(":%d", port)
url := "http://localhost" + portStr
ip := script.GetIpAddress()
startLog := "beancount-gs start at " + url
if ip != "" {
startLog += " or http://" + ip + portStr
}
script.LogSystemInfo(startLog)
// 打开浏览器
script.OpenBrowser(url)
// 打印密钥
script.LogSystemInfo("Secret token is " + script.GenerateServerSecret(secret))
// 启动服务
err = router.Run(portStr)
if err != nil {
script.LogSystemError("Failed to start server, " + err.Error())
}
}