2021-09-24 08:11:41 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2021-12-04 04:06:45 +00:00
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
2021-12-02 14:48:45 +00:00
|
|
|
"io"
|
2021-11-17 09:59:06 +00:00
|
|
|
"net/http"
|
2021-12-02 14:48:45 +00:00
|
|
|
"os"
|
2022-03-11 15:18:38 +00:00
|
|
|
|
|
|
|
|
"github.com/beancount-gs/script"
|
|
|
|
|
"github.com/beancount-gs/service"
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-09-24 08:11:41 +00:00
|
|
|
)
|
|
|
|
|
|
2021-11-18 08:27:28 +00:00
|
|
|
func InitServerFiles() error {
|
|
|
|
|
dataPath := script.GetServerConfig().DataPath
|
2021-11-17 09:59:06 +00:00
|
|
|
// 账本目录不存在,则创建
|
2021-12-01 09:32:15 +00:00
|
|
|
if dataPath != "" && !script.FileIfExist(dataPath) {
|
2021-11-18 08:27:28 +00:00
|
|
|
return script.MkDir(dataPath)
|
2021-11-17 09:59:06 +00:00
|
|
|
}
|
2021-11-18 08:27:28 +00:00
|
|
|
return nil
|
2021-11-17 06:16:22 +00:00
|
|
|
}
|
|
|
|
|
|
2021-11-18 08:27:28 +00:00
|
|
|
func LoadServerCache() error {
|
2021-11-22 14:50:10 +00:00
|
|
|
err := script.LoadLedgerConfigMap()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return script.LoadLedgerAccountsMap()
|
2021-11-17 06:16:22 +00:00
|
|
|
}
|
|
|
|
|
|
2021-11-18 10:10:19 +00:00
|
|
|
func AuthorizedHandler() gin.HandlerFunc {
|
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
|
ledgerId := c.GetHeader("ledgerId")
|
|
|
|
|
ledgerConfig := script.GetLedgerConfig(ledgerId)
|
|
|
|
|
if ledgerConfig != nil {
|
2021-11-19 09:54:02 +00:00
|
|
|
c.Set("LedgerConfig", ledgerConfig)
|
2021-11-18 10:10:19 +00:00
|
|
|
c.Next()
|
|
|
|
|
} else {
|
|
|
|
|
service.Unauthorized(c)
|
|
|
|
|
c.Abort()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-18 08:27:28 +00:00
|
|
|
func RegisterRouter(router *gin.Engine) {
|
2021-11-19 09:54:02 +00:00
|
|
|
// 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"))
|
2021-12-03 08:54:52 +00:00
|
|
|
router.GET("/api/version", service.QueryVersion)
|
2021-11-29 16:13:41 +00:00
|
|
|
router.POST("/api/check", service.CheckBeancount)
|
|
|
|
|
router.GET("/api/config", service.QueryServerConfig)
|
|
|
|
|
router.POST("/api/config", service.UpdateServerConfig)
|
2021-12-04 05:12:12 +00:00
|
|
|
router.GET("/api/ledger", service.QueryLedgerList)
|
2021-11-18 08:27:28 +00:00
|
|
|
router.POST("/api/ledger", service.OpenOrCreateLedger)
|
2021-11-18 10:10:19 +00:00
|
|
|
authorized := router.Group("/api/auth/")
|
|
|
|
|
authorized.Use(AuthorizedHandler())
|
|
|
|
|
{
|
|
|
|
|
// need authorized
|
2021-11-23 06:58:37 +00:00
|
|
|
authorized.GET("/account/valid", service.QueryValidAccount)
|
2021-11-23 14:42:03 +00:00
|
|
|
authorized.GET("/account/all", service.QueryAllAccount)
|
2021-11-23 06:58:37 +00:00
|
|
|
authorized.GET("/account/type", service.QueryAccountType)
|
2021-11-28 12:19:40 +00:00
|
|
|
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)
|
2021-12-14 14:09:17 +00:00
|
|
|
authorized.POST("/account/refresh", service.RefreshAccountCache)
|
2021-11-28 12:19:40 +00:00
|
|
|
authorized.POST("/commodity/price", service.SyncCommodityPrice)
|
2021-11-19 09:54:02 +00:00
|
|
|
authorized.GET("/stats/months", service.MonthsList)
|
2021-11-22 14:50:10 +00:00
|
|
|
authorized.GET("/stats/total", service.StatsTotal)
|
2021-11-26 09:12:07 +00:00
|
|
|
authorized.GET("/stats/payee", service.StatsPayee)
|
2021-11-24 15:45:45 +00:00
|
|
|
authorized.GET("/stats/account/percent", service.StatsAccountPercent)
|
|
|
|
|
authorized.GET("/stats/account/trend", service.StatsAccountTrend)
|
2021-12-02 14:48:45 +00:00
|
|
|
authorized.GET("/stats/account/balance", service.StatsAccountBalance)
|
2021-11-25 07:41:28 +00:00
|
|
|
authorized.GET("/stats/month/total", service.StatsMonthTotal)
|
2021-11-24 09:32:24 +00:00
|
|
|
authorized.GET("/transaction", service.QueryTransactions)
|
2021-11-28 12:19:40 +00:00
|
|
|
authorized.POST("/transaction", service.AddTransactions)
|
2021-12-12 14:42:07 +00:00
|
|
|
authorized.POST("/transaction/batch", service.AddBatchTransactions)
|
2021-11-24 09:32:24 +00:00
|
|
|
authorized.GET("/transaction/payee", service.QueryTransactionPayees)
|
|
|
|
|
authorized.GET("/transaction/template", service.QueryTransactionTemplates)
|
2021-11-29 14:21:25 +00:00
|
|
|
authorized.POST("/transaction/template", service.AddTransactionTemplate)
|
|
|
|
|
authorized.DELETE("/transaction/template", service.DeleteTransactionTemplate)
|
2021-11-22 10:05:12 +00:00
|
|
|
authorized.GET("/tags", service.QueryTags)
|
2021-11-23 15:33:14 +00:00
|
|
|
authorized.GET("/file/dir", service.QueryLedgerSourceFileDir)
|
|
|
|
|
authorized.GET("/file/content", service.QueryLedgerSourceFileContent)
|
|
|
|
|
authorized.POST("/file", service.UpdateLedgerSourceFileContent)
|
2021-12-12 14:42:07 +00:00
|
|
|
authorized.POST("/import/alipay", service.ImportAliPayCSV)
|
2021-12-14 13:24:39 +00:00
|
|
|
authorized.POST("/import/wx", service.ImportWxPayCSV)
|
2022-03-11 15:18:38 +00:00
|
|
|
authorized.DELETE("/ledger", service.DeleteLedger)
|
2021-11-18 10:10:19 +00:00
|
|
|
}
|
2021-11-17 06:16:22 +00:00
|
|
|
}
|
|
|
|
|
|
2021-09-24 08:11:41 +00:00
|
|
|
func main() {
|
2021-12-04 04:06:45 +00:00
|
|
|
var secret string
|
|
|
|
|
var port int
|
|
|
|
|
flag.StringVar(&secret, "secret", "", "服务器密钥")
|
|
|
|
|
flag.IntVar(&port, "p", 3001, "端口号")
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
2021-11-17 09:59:06 +00:00
|
|
|
// 读取配置文件
|
2021-11-18 08:27:28 +00:00
|
|
|
err := script.LoadServerConfig()
|
|
|
|
|
if err != nil {
|
2021-11-21 14:37:13 +00:00
|
|
|
script.LogSystemError("Failed to load server config, " + err.Error())
|
2021-11-18 08:27:28 +00:00
|
|
|
return
|
|
|
|
|
}
|
2021-12-01 09:32:15 +00:00
|
|
|
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
|
|
|
|
|
}
|
2021-11-18 08:27:28 +00:00
|
|
|
}
|
2021-12-02 14:48:45 +00:00
|
|
|
// gin 日志设置
|
|
|
|
|
gin.DisableConsoleColor()
|
|
|
|
|
fs, _ := os.Create("gin.log")
|
|
|
|
|
gin.DefaultWriter = io.MultiWriter(fs)
|
2021-11-18 08:27:28 +00:00
|
|
|
router := gin.Default()
|
2021-11-17 06:16:22 +00:00
|
|
|
// 注册路由
|
2021-11-18 08:27:28 +00:00
|
|
|
RegisterRouter(router)
|
2021-12-04 04:06:45 +00:00
|
|
|
|
|
|
|
|
portStr := fmt.Sprintf(":%d", port)
|
|
|
|
|
url := "http://localhost" + portStr
|
2021-12-03 08:46:18 +00:00
|
|
|
ip := script.GetIpAddress()
|
|
|
|
|
startLog := "beancount-gs start at " + url
|
|
|
|
|
if ip != "" {
|
2021-12-04 04:06:45 +00:00
|
|
|
startLog += " or http://" + ip + portStr
|
2021-12-03 08:46:18 +00:00
|
|
|
}
|
|
|
|
|
script.LogSystemInfo(startLog)
|
2021-12-04 04:06:45 +00:00
|
|
|
// 打开浏览器
|
|
|
|
|
script.OpenBrowser(url)
|
|
|
|
|
// 打印密钥
|
|
|
|
|
script.LogSystemInfo("Secret token is " + script.GenerateServerSecret(secret))
|
|
|
|
|
// 启动服务
|
|
|
|
|
err = router.Run(portStr)
|
2021-11-18 08:27:28 +00:00
|
|
|
if err != nil {
|
2021-11-21 14:37:13 +00:00
|
|
|
script.LogSystemError("Failed to start server, " + err.Error())
|
2021-11-18 08:27:28 +00:00
|
|
|
}
|
2021-09-24 08:11:41 +00:00
|
|
|
}
|