Skip to content

Commit

Permalink
refactor Starter Code
Browse files Browse the repository at this point in the history
  • Loading branch information
jinyaoMa committed Oct 10, 2022
1 parent 0716ca8 commit ec59fcc
Show file tree
Hide file tree
Showing 75 changed files with 2,203 additions and 2,214 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
air/bin
build/bin
frontend/wailsjs
frontend/public/docs
backend/web/static/certs
backend/web/static/docs
backend/web/static/swagger
*-res.syso

Expand Down
50 changes: 29 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ My Application is a continuously updated personal service collection.
> You can find all preview screenshots [here](./screenshots/).
<p align="center">
<img width="1024" src="./screenshots/home.jpg" alt="Home">
<img width="1024" src="./screenshots/settings.jpg" alt="Settings">
<img width="50%" src="./screenshots/home.jpg" alt="Home">
<img width="50%" src="./screenshots/settings.jpg" alt="Settings">
</p>

## Technologies
Expand Down Expand Up @@ -56,7 +56,7 @@ Prepare and install environment for development in Window 10/11?
$ pnpm wails:dev # run wails in development mode
$ pnpm wails:build # build wails application
$ pnpm upx:compress # compress the generated executable by `wails:build` script
$ pnpm air:dev # test web service individually
$ pnpm air:dev # test API service individually
$ pnpm swag:docs # generate/update swagger docs
$ pnpm docs:dev # test vitepress docs individually
$ pnpm docs:build # generate/update vitepress docs
Expand All @@ -77,40 +77,47 @@ $ pnpm <[install|preinstall]:[task]> # install/preinstall scripts trigger during
│ └── main.go # run web service individually w/o wails and tray
├── backend # sources related to backend code
│ ├── app # app module, business layer
│ │ ├── app.go # contain global state and resources
│ │ ├── config.go # load application options from database
│ │ ├── env.go # load os environment variable
│ │ ├── logger.go # setup loggers
│ ├── model # model module, data layer
│ │ ├── model.go # database setup
│ │ └── my_option.go # define application options for app config storage
│ │ ├── config # load application options from database, global resource
│ | │ ├── config.go # application config with functions for state and database manupulation
│ | │ ├── config.web.go # web config
│ | │ └── env.go # load os environment variable
│ │ ├── i18n # manage locale/translation strings for backend, global resource
│ │ ├── logger # setup loggers
│ │ └── app.go # setup global state and resources
│ ├── database # database module, persistence layer
│ │ ├── option # define application options for app config storage
│ │ └── database.go # setup database
│ ├── pkg # pkg module, cross cutting
│ │ ├── i18n # manage locale/translation strings for backend
│ │ ├── log # define loggers for backend
│ │ └── utils # helper functions
│ ├── service # service module, business layer
│ │ ├── service.go # initialize provided services
│ │ └── settings.go # functions about configure application
│ ├── tray # tray module, presentations & services layer
│ ├── tray # tray module, presentation layer
│ │ ├── icons # tray icons
│ │ ├── menus # tray menus
│ │ ├── listeners.go # listen to tray menu-item click events
│ │ └── tray.go # system tray
│ └── web # web module, presentations & services layer
│ │ ├── menus # tray menu types
│ │ ├── expose.go # functions exposed to wails frontend
│ │ ├── listen.go # listen to tray menu-item click events
│ │ ├── ready.go # onReady() for system tray to initialize tray menu
│ │ ├── refresh.go # refresh functions for language switch and tray icon tooltip
│ │ └── tray.go # setup system tray
│ └── web # web module, service layer
│ ├── api # API services
│ ├── auth # Auth services
│ ├── middleware # custom gin middlewares
│ ├── static # static websites and sources
│ │ ├── certs # auto-generated for localhost TLS, try script `install:certs`
│ │ ├── docs # auto-generated, try script `docs:build`
│ │ ├── swagger # auto-generated, try script `swag:docs`
│ │ ├── static.go # manage static sources
│ │ └── # ...
│ ├── air.go # special function for air to run web service individually
│ ├── air.go # special function for air to test API service individually
│ ├── router.go # entry point of swaggo docs generator, manage routes for API
│ └── web.go # web service
├── build # sources to use during wails build process
│ ├── bin # auto-generated, try script `wails:dev` or `wails:build`
│ │ ├── Assets # wails frontend static resources, auto-generated, try script `wails:build`
│ │ ├── Docs # vitepress docs static resources, auto-generated, try script `docs:build`
│ │ ├── UserData # webview2 user data, auto-generated during application runtime
│ │ └── # ...
│ └── # ...
├── diagrams # diagrams about 4+1 view model
├── docs # vitepress documentation
Expand All @@ -120,8 +127,9 @@ $ pnpm <[install|preinstall]:[task]> # install/preinstall scripts trigger during
│ │ ├── vite-env.d.ts # put go struct associated types into it
│ │ └── # ...
│ └── # ...
├── main.go # wails main application, presentations & services layer
├── wails_life_cycle.go # wails life cycle
├── screenshots # README application preview screenshots
├── main.go # wails main application, presentation layer
├── main.wails.go # wails life cycle
├── wails.json # wails CLI config
└── # ...
```
Expand Down
50 changes: 29 additions & 21 deletions README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
> 你可以在[这里](./screenshots/)找到所有的预览截图。
<p align="center">
<img width="1024" src="./screenshots/home.zh.jpg" alt="Home">
<img width="1024" src="./screenshots/settings.zh.jpg" alt="Settings">
<img width="50%" src="./screenshots/home.zh.jpg" alt="Home">
<img width="50%" src="./screenshots/settings.zh.jpg" alt="Settings">
</p>

## 技术
Expand Down Expand Up @@ -56,7 +56,7 @@
$ pnpm wails:dev # 开发模式运行(带 wails,tray 和 web)
$ pnpm wails:build # 编译导出 wails 可执行文件
$ pnpm upx:compress # 压缩由 `wails:build` 导出的 wails 可执行文件
$ pnpm air:dev # 单独测试 Web 服务
$ pnpm air:dev # 单独测试 API 服务
$ pnpm swag:docs # 生成或更新 swagger 文档
$ pnpm docs:dev # 单独测试 vitepress 文档
$ pnpm docs:build # 生成或更新 vitepress 文档
Expand All @@ -77,50 +77,58 @@ $ pnpm <[install|preinstall]:[task]> # 安装项目依赖时会触发这些 inst
│ └── main.go # 单独运行 Web 服务(不带 wails 和 tray)
├── backend # 后端相关源文件
│ ├── app # app 模块,业务层
│ │ ├── config # 从数据库加载应用设置,全局资源
│ | │ ├── config.go # 应用设置,与状态和数据操作相关的函数
│ | │ ├── config.web.go # Web 设置
│ | │ └── env.go # 加载系统环境变量
│ │ ├── i18n # 后端 i18n 语言源文件,全局资源
│ │ ├── logger # 定义后端 loggers
│ │ ├── app.go # 存放全局状态及资源
│ │ ├── config.go # 从数据库加载应用设置
│ │ ├── env.go # 加载系统环境变量
│ │ ├── logger.go # 初始化 loggers
│ ├── model # model 模块,数据层
│ │ ├── model.go # 初始化 database
│ │ └── my_option.go # 定义 options 表,用于储存应用设置
│ ├── database # database 模块,持久层
│ │ ├── option # 定义 options 表,用于储存应用设置
│ │ └── database.go # 初始化 database
│ ├── pkg # pkg 模块,横切层
│ │ ├── i18n # 后端 i18n 语言源文件
│ │ ├── log # 定义后端 loggers
│ │ └── utils # 辅助函数/方法
│ ├── service # service 模块,业务层
│ │ ├── service.go # 初始化提供给服务
│ │ └── settings.go # 应用设置相关函数/方法
│ ├── tray # tray 模块,表示层 & 服务层
│ │ ├── icons # tray 图标
│ │ ├── menus # tray 菜单
│ │ ├── listeners.go # 监听 tray 菜单项点击事件
│ │ └── tray.go # 系统 tray 入口
│ └── web # web 模块,表示层 & 服务层
│ ├── tray # tray 模块,表示层
│ │ ├── icons # 托盘图标
│ │ ├── menus # 托盘菜单类型
│ │ ├── expose.go # 暴露给 wails 前端的函数
│ │ ├── listen.go # 监听托盘菜单项点击事件
│ │ ├── ready.go # 定义 onReady(),初始化系统托盘菜单
│ │ ├── refresh.go # 定义语言切换和鼠标放置托盘图标时出现提示的刷新函数
│ │ └── tray.go # 系统托盘入口
│ └── web # web 模块,服务层
│ ├── api # API 服务
│ ├── auth # Auth 服务
│ ├── middleware # 自定义的 gin 中间件
│ ├── static # 静态网站及资源
│ │ ├── certs # 自动生成的 localhost TLS 证书, 可以尝试 `install:certs` 脚本
│ │ ├── docs # 自动生成,可以尝试 `docs:build` 脚本
│ │ ├── swagger # 自动生成,可以尝试 `swag:docs` 脚本
│ │ ├── static.go # 管理静态资源
│ │ └── # ...
│ ├── air.go # 包含提供给 air 的特殊方法,用于单独运行 Web 服务
│ ├── air.go # 包含提供给 air 的特殊方法,用于单独运行 API 服务
│ ├── router.go # swaggo 生成文档入口, 管理 API 路由
│ └── web.go # web 服务入口
├── build # 编译 wails 时用到的资源...
│ ├── bin # 自动生成,可以尝试 `wails:dev` 或 `wails:build` 脚本
│ │ ├── Assets # wails 前端静态资源,自动生成,可以尝试 `wails:build` 脚本
│ │ ├── Docs # vitepress 文档静态资源,自动生成,可以尝试 `docs:build` 脚本
│ │ ├── UserData # webview2 用户数据,应用运行时自动生成
│ │ └── # ...
│ └── # ...
├── diagrams # 4+1 视图模型相关图解
├── docs # vitepress 文档
├── docs # vitepress 文档源文件
├── frontend # 前端相关源文件(用 PNPM 管理 workspace)
│ ├── packages # 前端组件、图标字体等等
│ ├── src # wails 前端源文件
│ │ ├── vite-env.d.ts # put go struct associated types into it
│ │ └── # ...
│ └── # ...
├── main.go # wails main 入口, 表示层 & 服务层
├── screenshots # README 应用预览截图
├── main.go # wails main 入口, 表示层
├── wails_life_cycle.go # wails 生命周期
├── wails.json # wails CLI 配置
└── # ...
Expand Down
7 changes: 6 additions & 1 deletion air/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package main

import "my-app/backend/web"
import (
"fmt"
"my-app/backend/app"
"my-app/backend/web"
)

func main() {
go fmt.Printf("\n[GIN-debug] ➜ Local: https://localhost%s/swagger/index.html \n\n", app.App().Config().PortHttps)
web.Web().Air()
}
97 changes: 34 additions & 63 deletions backend/app/app.go
Original file line number Diff line number Diff line change
@@ -1,92 +1,63 @@
package app

import (
"context"
"my-app/backend/model"
"my-app/backend/pkg/log"
"os"
"sync"

"github.com/wailsapp/wails/v2/pkg/logger"
"my-app/backend/app/config"
"my-app/backend/app/i18n"
"my-app/backend/app/logger"
"my-app/backend/database"
)

var (
once sync.Once
instance *app
)

type app struct {
wailsContext context.Context
logger *Logger
env *Env
config *Config
cfg *config.Config
env *config.Env
log *logger.Logger
i18n *i18n.I18n
}

func init() {
var log *logger.Logger
cfg := config.LoadConfig()
env := config.LoadEnv().
Log2Console(func() {
log = logger.NewConsoleLogger()
}).
Log2File(func() {
log = logger.NewFileLogger(cfg.LogPath)
})

instance = &app{
env: LoadEnv(),
logger: LoadConsoleLogger(),
cfg: cfg,
env: env,
log: log,
i18n: i18n.NewI18n(),
}

database.SetLogger(instance.log.Database())
}

func App() *app {
once.Do(func() {
instance.config = LoadConfig()

if instance.env.Log2File() {
logFile, err := os.OpenFile(
instance.config.LogPath,
os.O_CREATE|os.O_WRONLY|os.O_APPEND,
0666,
)
if err != nil {
instance.logger.App.Fatalf("failed to open log file: %+v\n", err)
}
instance.logger = LoadFileLogger(logFile)
}

model.SetLogger(instance.logger.Model)
})
return instance
}

func (a *app) SetWailsContext(ctx context.Context) *app {
a.wailsContext = ctx
return a
}

func (a *app) WailsContext() context.Context {
return a.wailsContext
}

func (a *app) Config() *Config {
return a.config
func (a *app) Config() *config.Config {
return a.cfg
}

func (a *app) WebConfig() *WebConfig {
return a.config.Web
}

func (a *app) Env() *Env {
func (a *app) Env() *config.Env {
return a.env
}

func (a *app) AppLog() *log.Logger {
return a.logger.App
func (a *app) Log() *logger.Logger {
return a.log
}

func (a *app) WebLog() *log.Logger {
return a.logger.Web
}

func (a *app) TrayLog() *log.Logger {
return a.logger.Tray
}

func (a *app) WailsLog() logger.Logger {
return a.logger.Wails
}

func (a *app) ServiceLog() *log.Logger {
return a.logger.Service
func (a *app) CurrentTranslation() *i18n.Translation {
if t := a.i18n.Translation(a.cfg.DisplayLanguage); t != nil {
return t
}
return &i18n.Translation{}
}
Loading

0 comments on commit ec59fcc

Please sign in to comment.