PMail/server/http_server/https_server.go
Jinnrry 054336fe9e
v2.6.1 (#169)
1、新增垃圾邮件过滤插件
2、使用使用github.com/dlclark/regexp2替换go原生的正则包
3、修复空数据导致的邮件插入失败
2024-07-20 10:39:17 +08:00

113 lines
2.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package http_server
import (
"embed"
"encoding/json"
"fmt"
"github.com/Jinnrry/pmail/config"
"github.com/Jinnrry/pmail/controllers"
"github.com/Jinnrry/pmail/dto/response"
"github.com/Jinnrry/pmail/i18n"
"github.com/Jinnrry/pmail/models"
"github.com/Jinnrry/pmail/session"
"github.com/Jinnrry/pmail/utils/context"
"github.com/Jinnrry/pmail/utils/id"
olog "log"
"net/http"
"time"
log "github.com/sirupsen/logrus"
"github.com/spf13/cast"
)
//go:embed dist/*
var local embed.FS
var httpsServer *http.Server
type nullWrite struct {
}
func (w *nullWrite) Write(p []byte) (int, error) {
return len(p), nil
}
func HttpsStart() {
mux := http.NewServeMux()
router(mux)
// go http server会打一堆没用的日志写一个空的日志处理器屏蔽掉日志输出
nullLog := olog.New(&nullWrite{}, "", olog.Ldate)
HttpsPort := 443
if config.Instance.HttpsPort > 0 {
HttpsPort = config.Instance.HttpsPort
}
if config.Instance.HttpsEnabled != 2 {
log.Infof("Https Server Start On Port :%d", HttpsPort)
httpsServer = &http.Server{
Addr: fmt.Sprintf(":%d", HttpsPort),
Handler: session.Instance.LoadAndSave(mux),
ReadTimeout: time.Second * 90,
WriteTimeout: time.Second * 90,
ErrorLog: nullLog,
}
err := httpsServer.ListenAndServeTLS("config/ssl/public.crt", "config/ssl/private.key")
if err != nil {
panic(err)
}
}
}
func HttpsStop() {
if httpsServer != nil {
httpsServer.Close()
}
}
// 注入context
func contextIterceptor(h controllers.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if w.Header().Get("Content-Type") == "" {
w.Header().Set("Content-Type", "application/json")
}
ctx := &context.Context{}
ctx.Context = r.Context()
ctx.SetValue(context.LogID, id.GenLogID())
lang := r.Header.Get("Lang")
if lang == "" {
lang = "en"
}
ctx.Lang = lang
if config.IsInit {
user := cast.ToString(session.Instance.Get(ctx, "user"))
var userInfo *models.User
if user != "" {
_ = json.Unmarshal([]byte(user), &userInfo)
}
if userInfo != nil && userInfo.ID > 0 {
ctx.UserID = userInfo.ID
ctx.UserName = userInfo.Name
ctx.UserAccount = userInfo.Account
ctx.IsAdmin = userInfo.IsAdmin == 1
}
if ctx.UserID == 0 {
if r.URL.Path != "/api/ping" && r.URL.Path != "/api/login" {
response.NewErrorResponse(response.NeedLogin, i18n.GetText(ctx.Lang, "login_exp"), "").FPrint(w)
return
}
}
} else if r.URL.Path != "/api/setup" {
response.NewErrorResponse(response.NeedSetup, "", "").FPrint(w)
return
}
h(ctx, w, r)
}
}