Smail/server/http_server/http_server.go
Jinnrry e6a56b199d
v2.6.2 (#177)
1、修复邮件插入失败
2、插件支持设置页面
3、修复pop3邮箱拉取权限判断
4、修复非管理员账户的附件权限判断

Co-authored-by: jinnrry <i@jinnrry.com>
2024-07-27 20:47:42 +08:00

96 lines
3.4 KiB
Go

package http_server
import (
"errors"
"fmt"
"github.com/Jinnrry/pmail/config"
"github.com/Jinnrry/pmail/controllers"
"github.com/Jinnrry/pmail/controllers/email"
"github.com/Jinnrry/pmail/session"
log "github.com/sirupsen/logrus"
"io/fs"
"net/http"
"time"
)
// 这个服务是为了拦截http请求转发到https
var httpServer *http.Server
func HttpStop() {
if httpServer != nil {
httpServer.Close()
}
}
func router(mux *http.ServeMux) {
fe, err := fs.Sub(local, "dist")
if err != nil {
panic(err)
}
mux.Handle("/", http.FileServer(http.FS(fe)))
// 挑战请求类似这样 /.well-known/acme-challenge/QPyMAyaWw9s5JvV1oruyqWHG7OqkHMJEHPoUz2046KM
mux.HandleFunc("/.well-known/", controllers.AcmeChallenge)
mux.HandleFunc("/api/ping", controllers.Ping)
mux.HandleFunc("/api/login", contextIterceptor(controllers.Login))
mux.HandleFunc("/api/logout", contextIterceptor(controllers.Logout))
mux.HandleFunc("/api/group", contextIterceptor(controllers.GetUserGroup))
mux.HandleFunc("/api/group/list", contextIterceptor(controllers.GetUserGroupList))
mux.HandleFunc("/api/group/add", contextIterceptor(controllers.AddGroup))
mux.HandleFunc("/api/group/del", contextIterceptor(controllers.DelGroup))
mux.HandleFunc("/api/email/list", contextIterceptor(email.EmailList))
mux.HandleFunc("/api/email/del", contextIterceptor(email.EmailDelete))
mux.HandleFunc("/api/email/read", contextIterceptor(email.MarkRead))
mux.HandleFunc("/api/email/detail", contextIterceptor(email.EmailDetail))
mux.HandleFunc("/api/email/move", contextIterceptor(email.Move))
mux.HandleFunc("/api/email/send", contextIterceptor(email.Send))
mux.HandleFunc("/api/settings/modify_password", contextIterceptor(controllers.ModifyPassword))
mux.HandleFunc("/api/rule/get", contextIterceptor(controllers.GetRule))
mux.HandleFunc("/api/rule/add", contextIterceptor(controllers.UpsertRule))
mux.HandleFunc("/api/rule/update", contextIterceptor(controllers.UpsertRule))
mux.HandleFunc("/api/rule/del", contextIterceptor(controllers.DelRule))
mux.HandleFunc("/attachments/", contextIterceptor(controllers.GetAttachments))
mux.HandleFunc("/attachments/download/", contextIterceptor(controllers.Download))
mux.HandleFunc("/api/user/create", contextIterceptor(controllers.CreateUser))
mux.HandleFunc("/api/user/edit", contextIterceptor(controllers.EditUser))
mux.HandleFunc("/api/user/info", contextIterceptor(controllers.Info))
mux.HandleFunc("/api/user/list", contextIterceptor(controllers.UserList))
mux.HandleFunc("/api/plugin/settings/", contextIterceptor(controllers.SettingsHtml))
mux.HandleFunc("/api/plugin/list", contextIterceptor(controllers.GetPluginList))
}
func HttpStart() {
mux := http.NewServeMux()
HttpPort := 80
if config.Instance.HttpPort > 0 {
HttpPort = config.Instance.HttpPort
}
if config.Instance.HttpsEnabled != 2 {
mux.HandleFunc("/api/ping", controllers.Ping)
mux.HandleFunc("/", controllers.Interceptor)
httpServer = &http.Server{
Addr: fmt.Sprintf(":%d", HttpPort),
Handler: mux,
ReadTimeout: time.Second * 90,
WriteTimeout: time.Second * 90,
}
} else {
router(mux)
log.Infof("HttpServer Start On Port :%d", HttpPort)
httpServer = &http.Server{
Addr: fmt.Sprintf(":%d", HttpPort),
Handler: session.Instance.LoadAndSave(mux),
ReadTimeout: time.Second * 90,
WriteTimeout: time.Second * 90,
}
}
err := httpServer.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
panic(err)
}
}