feat: add Midjourney (#138)

* 🚧 stash

*  feat: add Midjourney

* 📝 doc: update readme
This commit is contained in:
Buer
2024-04-05 04:03:46 +08:00
committed by GitHub
parent 87bfecf3e9
commit c1fc32add7
42 changed files with 2479 additions and 84 deletions

View File

@@ -37,6 +37,9 @@ var WeChatAuthEnabled = false
var TurnstileCheckEnabled = false
var RegisterEnabled = true
// mj
var MjNotifyEnabled = false
var EmailDomainRestrictionEnabled = false
var EmailDomainWhitelist = []string{
"gmail.com",
@@ -161,6 +164,7 @@ const (
ChannelTypeGroq = 31
ChannelTypeBedrock = 32
ChannelTypeLingyi = 33
ChannelTypeMidjourney = 34
)
var ChannelBaseURLs = []string{
@@ -198,6 +202,7 @@ var ChannelBaseURLs = []string{
"https://api.groq.com/openai", //31
"", //32
"https://api.lingyiwanwu.com", //33
"", //34
}
const (

32
common/go-channel.go Normal file
View File

@@ -0,0 +1,32 @@
package common
import (
"fmt"
"runtime/debug"
)
func SafeGoroutine(f func()) {
go func() {
defer func() {
if r := recover(); r != nil {
SysError(fmt.Sprintf("child goroutine panic occured: error: %v, stack: %s", r, string(debug.Stack())))
}
}()
f()
}()
}
func SafeSend(ch chan bool, value bool) (closed bool) {
defer func() {
// Recover from panic if one occured. A panic would mean the channel was closed.
if recover() != nil {
closed = true
}
}()
// This will panic if the channel is closed.
ch <- value
// If the code reaches here, then the channel was not closed.
return false
}

View File

@@ -106,7 +106,10 @@ func logHelper(ctx context.Context, level string, msg string) {
if level == loggerINFO {
writer = gin.DefaultWriter
}
id := ctx.Value(RequestIdKey)
id, ok := ctx.Value(RequestIdKey).(string)
if !ok {
id = "unknown"
}
now := time.Now()
_, _ = fmt.Fprintf(writer, "[%s] %v | %s | %s \n", level, now.Format("2006/01/02 - 15:04:05"), id, msg)
logCount++ // we don't need accurate count, so no lock here

View File

@@ -23,6 +23,7 @@ type HTTPRequester struct {
CreateFormBuilder func(io.Writer) FormBuilder
ErrorHandler HttpErrorHandler
proxyAddr string
Context context.Context
}
// NewHTTPRequester 创建一个新的 HTTPRequester 实例。
@@ -37,6 +38,7 @@ func NewHTTPRequester(proxyAddr string, errorHandler HttpErrorHandler) *HTTPRequ
},
ErrorHandler: errorHandler,
proxyAddr: proxyAddr,
Context: context.Background(),
}
}
@@ -47,18 +49,18 @@ type requestOptions struct {
type requestOption func(*requestOptions)
func (r *HTTPRequester) getContext() context.Context {
func (r *HTTPRequester) setProxy() context.Context {
if r.proxyAddr == "" {
return context.Background()
return r.Context
}
// 如果是以 socks5:// 开头的地址,那么使用 socks5 代理
if strings.HasPrefix(r.proxyAddr, "socks5://") {
return context.WithValue(context.Background(), ProxySock5AddrKey, r.proxyAddr)
return context.WithValue(r.Context, ProxySock5AddrKey, r.proxyAddr)
}
// 否则使用 http 代理
return context.WithValue(context.Background(), ProxyHTTPAddrKey, r.proxyAddr)
return context.WithValue(r.Context, ProxyHTTPAddrKey, r.proxyAddr)
}
@@ -71,7 +73,7 @@ func (r *HTTPRequester) NewRequest(method, url string, setters ...requestOption)
for _, setter := range setters {
setter(args)
}
req, err := r.requestBuilder.Build(r.getContext(), method, url, args.body, args.header)
req, err := r.requestBuilder.Build(r.setProxy(), method, url, args.body, args.header)
if err != nil {
return nil, err
}