Skip to content

Commit

Permalink
feat: web ui for message history
Browse files Browse the repository at this point in the history
  • Loading branch information
ttktatakai committed Jan 24, 2024
1 parent 8dad9ed commit ddd691f
Show file tree
Hide file tree
Showing 31 changed files with 19,140 additions and 21 deletions.
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
FROM golang:1.20.4-alpine3.17
RUN set -eux && sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
RUN apk add tzdata
RUN apk add build-base
ENV TZ=Asia/Shanghai
WORKDIR /messenger
COPY . .
RUN go env -w GOPROXY=https://goproxy.cn,direct \
&& go build -o ./messenger ./main.go
&& CGO_ENABLED=1 go build -o ./messenger ./main.go

FROM alpine:latest
RUN set -eux && sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
RUN apk add tzdata
ENV TZ=Asia/Shanghai
WORKDIR /messenger
COPY --from=0 /messenger/messenger .
COPY --from=0 /messenger/web/build ./web/build
CMD [ "./messenger"]
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,8 @@ senders:
return &wechatBot{conf: conf}
}
}
```
```

## Web

http://127.0.0.1:8888/web
25 changes: 18 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,27 @@ func main() {

gin.SetMode(gin.ReleaseMode)
r := gin.Default()
v1 := r.Group("/v1").Use(middleware.Auth(authConf), middleware.Error2Resp())
g1 := r.Group("/v1").Use(middleware.Auth(authConf), middleware.Error2Resp())
{
v1.POST("/message", send.PushMessage)
v1.POST("/uid/getbyphone", send.GetUIDByPhone)
v1.GET("/histories", send.QueryHistory)
g1.POST("/message", send.PushMessage)
g1.POST("/uid/getbyphone", send.GetUIDByPhone)

v1.POST("/senders", global.PushRemoteConf)
v1.PUT("/senders", global.PushRemoteConf)
v1.DELETE("/senders", global.PushRemoteConf)
g1.POST("/senders", global.PushRemoteConf)
g1.PUT("/senders", global.PushRemoteConf)
g1.DELETE("/senders", global.PushRemoteConf)
}
g2 := r.Group("/v1").Use(middleware.Error2Resp())
{
g2.GET("/histories", send.QueryHistory)
}

r.StaticFile("/web", "./web/build/index.html")
r.StaticFile("/manifest.json", "./web/build/manifest.json")
r.StaticFile("/logo192.png", "./web/build/logo192.png")
r.StaticFile("/favicon.ico", "./web/build//favicon.ico")
r.Static("/static", "./web/build/static")
// r.Static("/manifest.json", ".web/build/manifest.json")

docs.SwaggerInfo.Title = "Messenger api"
docs.SwaggerInfo.Version = ""
docs.SwaggerInfo.BasePath = "/"
Expand Down
32 changes: 21 additions & 11 deletions send/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"fmt"
"log"
"net/http"
"strings"

"github.com/gin-gonic/gin"
"github.com/go-resty/resty/v2"
"github.com/moul/http2curl"
"github.com/samber/lo"
"github.com/spf13/cast"
"golang.org/x/sync/errgroup"
"gopkg.in/gomail.v2"
Expand All @@ -34,14 +36,14 @@ func init() {
}

type History struct {
Id int `gorm:"column:id"`
Message string `gorm:"column:message"`
Err string `gorm:"column:err"`
Req string `gorm:"column:req"`
Resp string `gorm:"column:resp"`
Status bool `gorm:"column:status"`
ReceivedAt int64 `gorm:"column:received_at"`
CreatedAt int64 `gorm:"column:created_at"`
Id int `gorm:"column:id" json:"id"`
Message string `gorm:"column:message" json:"message"`
Err string `gorm:"column:err" json:"err"`
Req string `gorm:"column:req" json:"req"`
Resp string `gorm:"column:resp" json:"resp"`
Status bool `gorm:"column:status" json:"status"`
ReceivedAt int64 `gorm:"column:received_at" json:"received_at"`
CreatedAt int64 `gorm:"column:created_at" json:"created_at"`
}

func (History) TableName() string {
Expand Down Expand Up @@ -83,19 +85,27 @@ func QueryHistory(ctx *gin.Context) {
pageIndex, pageSize := cast.ToInt(ctx.Query("page_index")), cast.ToInt(ctx.Query("page_size"))
q := db.Model(&History{}).Offset((pageIndex - 1) * pageSize).Limit(pageSize)
if v, ok := ctx.GetQuery("start"); ok {
q = q.Where("received_at >= ?", v)
q = q.Where("created_at >= ?", v)
}
if v, ok := ctx.GetQuery("end"); ok {
q = q.Where("received_at <= ?", v)
q = q.Where("created_at <= ?", v)
}
if v, ok := ctx.GetQuery("status"); ok {
q = q.Where("status = ?", cast.ToBool(v))
ss := strings.Split(v, ",")
if len(ss) == 1 {
q = q.Where("status = ?", cast.ToBool(v))
}
}
for _, k := range []string{"sender", "content"} {
if v, ok := ctx.GetQuery(k); ok {
q = q.Where(fmt.Sprintf("JSON_EXTRACT(`message`,'$.%s') LIKE ?", k), fmt.Sprintf("%%%s%%", v))
}
}
if v, ok := ctx.GetQuery("sort"); ok {
if len(v) > 1 {
q = q.Order(fmt.Sprintf("%s %s", v[1:], lo.Ternary(v[:1] == "+", "ASC", "DESC")))
}
}
count := int64(0)
histories := make([]*History, 0)
cfg := &gorm.Session{}
Expand Down
2 changes: 1 addition & 1 deletion send/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func handleMessage(msg *message) (err error) {
if err != nil && !msg.Sync {
log.Println(err)
}
if msg.Err != nil && err != nil {
if msg.Err == nil {
msg.Err = err
}
AddHistory(msg)
Expand Down
23 changes: 23 additions & 0 deletions web/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
# /build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
70 changes: 70 additions & 0 deletions web/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Getting Started with Create React App

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in your browser.

The page will reload when you make changes.\
You may also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can't go back!**

If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.

You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).

### Code Splitting

This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)

### Analyzing the Bundle Size

This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)

### Making a Progressive Web App

This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)

### Advanced Configuration

This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)

### Deployment

This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)

### `npm run build` fails to minify

This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
13 changes: 13 additions & 0 deletions web/build/asset-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"files": {
"main.css": "/static/css/main.a92f1fff.css",
"main.js": "/static/js/main.144d6b76.js",
"index.html": "/index.html",
"main.a92f1fff.css.map": "/static/css/main.a92f1fff.css.map",
"main.144d6b76.js.map": "/static/js/main.144d6b76.js.map"
},
"entrypoints": [
"static/css/main.a92f1fff.css",
"static/js/main.144d6b76.js"
]
}
Binary file added web/build/favicon.ico
Binary file not shown.
1 change: 1 addition & 0 deletions web/build/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>React App</title><script defer="defer" src="/static/js/main.144d6b76.js"></script><link href="/static/css/main.a92f1fff.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
Binary file added web/build/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added web/build/logo512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions web/build/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
3 changes: 3 additions & 0 deletions web/build/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
Loading

0 comments on commit ddd691f

Please sign in to comment.