mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-10 18:05:48 +02:00
fix: gen code generator; some unit tests
This commit is contained in:
parent
33a996e777
commit
918f920d57
8 changed files with 86 additions and 178 deletions
|
@ -152,11 +152,13 @@ func FinishPasskeyLogin(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
secureSessionID := user.SetSecureSessionID(outUser.ID)
|
||||
|
||||
c.JSON(http.StatusOK, LoginResponse{
|
||||
Code: LoginSuccess,
|
||||
Message: "ok",
|
||||
Token: token,
|
||||
// SecureSessionID: secureSessionID,
|
||||
Code: LoginSuccess,
|
||||
Message: "ok",
|
||||
Token: token,
|
||||
SecureSessionID: secureSessionID,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -167,9 +167,12 @@ async function handlePasskeyLogin() {
|
|||
})
|
||||
|
||||
if (r.token) {
|
||||
const cookies = useCookies(['nginx-ui-2fa'])
|
||||
const next = (route.query?.next || '').toString() || '/'
|
||||
|
||||
passkeyLogin(asseResp.rawId, r.token)
|
||||
secureSessionId.value = r.secure_session_id
|
||||
cookies.set('secure_session_id', r.secure_session_id, { maxAge: 60 * 3 })
|
||||
|
||||
await router.push(next)
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ import (
|
|||
"flag"
|
||||
"fmt"
|
||||
"github.com/0xJacky/Nginx-UI/model"
|
||||
"github.com/0xJacky/Nginx-UI/settings"
|
||||
"github.com/uozi-tech/cosy/settings"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gen"
|
||||
"gorm.io/gorm"
|
||||
|
@ -21,26 +21,26 @@ func main() {
|
|||
Mode: gen.WithoutContext | gen.WithDefaultQuery,
|
||||
//if you want the nullable field generation property to be pointer type, set FieldNullable true
|
||||
FieldNullable: true,
|
||||
//if you want to assign field which has default value in `Create` API, set FieldCoverable true, reference: https://gorm.io/docs/create.html#Default-Values
|
||||
//if you want to assign field which has the default value in `Create` API, set FieldCoverable true, reference: https://gorm.io/docs/create.html#Default-Values
|
||||
FieldCoverable: true,
|
||||
// if you want to generate field with unsigned integer type, set FieldSignable true
|
||||
// if you want to generate field with an unsigned integer type, set FieldSignable true
|
||||
/* FieldSignable: true,*/
|
||||
//if you want to generate index tags from database, set FieldWithIndexTag true
|
||||
//if you want to generate index tags from the database, set FieldWithIndexTag true
|
||||
/* FieldWithIndexTag: true,*/
|
||||
//if you want to generate type tags from database, set FieldWithTypeTag true
|
||||
//if you want to generate type tags from the database, set FieldWithTypeTag true
|
||||
/* FieldWithTypeTag: true,*/
|
||||
//if you need unit tests for query code, set WithUnitTest true
|
||||
/* WithUnitTest: true, */
|
||||
})
|
||||
|
||||
// reuse the database connection in Project or create a connection here
|
||||
// if you want to use GenerateModel/GenerateModelAs, UseDB is necessary or it will panic
|
||||
// if you want to use GenerateModel/GenerateModelAs, UseDB is necessary, or it will panic
|
||||
var confPath string
|
||||
flag.StringVar(&confPath, "config", "app.ini", "Specify the configuration file")
|
||||
flag.Parse()
|
||||
|
||||
settings.Init(confPath)
|
||||
dbPath := path.Join(path.Dir(settings.ConfPath), fmt.Sprintf("%s.db", settings.ServerSettings.Database))
|
||||
dbPath := path.Join(path.Dir(confPath), fmt.Sprintf("%s.db", settings.DataBaseSettings.Name))
|
||||
|
||||
var err error
|
||||
db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{
|
||||
|
@ -56,7 +56,7 @@ func main() {
|
|||
g.UseDB(db)
|
||||
|
||||
// apply basic crud api on structs or table models which is specified by table name with function
|
||||
// GenerateModel/GenerateModelAs. And generator will generate table models' code when calling Excute.
|
||||
// GenerateModel/GenerateModelAs. And the generator will generate table models' code when calling Excute.
|
||||
g.ApplyBasic(model.GenerateAllModel()...)
|
||||
|
||||
// apply diy interfaces on structs or table models
|
||||
|
|
|
@ -3,45 +3,48 @@ package cluster
|
|||
import (
|
||||
"github.com/0xJacky/Nginx-UI/settings"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/uozi-tech/cosy/sandbox"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_parseNodeUrl(t *testing.T) {
|
||||
settings.Init("../../app.example.ini")
|
||||
t.Log(settings.ClusterSettings.Node)
|
||||
node := settings.ClusterSettings.Node[0]
|
||||
sandbox.NewInstance("../../app.example.ini", "sqlite").
|
||||
Run(func(instance *sandbox.Instance) {
|
||||
t.Log(settings.ClusterSettings.Node)
|
||||
node := settings.ClusterSettings.Node[0]
|
||||
|
||||
env, err := parseNodeUrl(node)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
env, err := parseNodeUrl(node)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Equal(t, "node1", env.Name)
|
||||
assert.Equal(t, "http://10.0.0.1:9000", env.URL)
|
||||
assert.Equal(t, "my-node-secret", env.Token)
|
||||
assert.Equal(t, true, env.Enabled)
|
||||
assert.Equal(t, "node1", env.Name)
|
||||
assert.Equal(t, "http://10.0.0.1:9000", env.URL)
|
||||
assert.Equal(t, "my-node-secret", env.Token)
|
||||
assert.Equal(t, true, env.Enabled)
|
||||
|
||||
node = settings.ClusterSettings.Node[1]
|
||||
node = settings.ClusterSettings.Node[1]
|
||||
|
||||
env, err = parseNodeUrl(node)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
env, err = parseNodeUrl(node)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Equal(t, "node2", env.Name)
|
||||
assert.Equal(t, "http://10.0.0.2:9000", env.URL)
|
||||
assert.Equal(t, "my-node-secret", env.Token)
|
||||
assert.Equal(t, true, env.Enabled)
|
||||
assert.Equal(t, "node2", env.Name)
|
||||
assert.Equal(t, "http://10.0.0.2:9000", env.URL)
|
||||
assert.Equal(t, "my-node-secret", env.Token)
|
||||
assert.Equal(t, true, env.Enabled)
|
||||
|
||||
node = settings.ClusterSettings.Node[2]
|
||||
node = settings.ClusterSettings.Node[2]
|
||||
|
||||
env, err = parseNodeUrl(node)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
env, err = parseNodeUrl(node)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Equal(t, "node3", env.Name)
|
||||
assert.Equal(t, "http://10.0.0.3", env.URL)
|
||||
assert.Equal(t, "my-node-secret", env.Token)
|
||||
assert.Equal(t, true, env.Enabled)
|
||||
assert.Equal(t, "node3", env.Name)
|
||||
assert.Equal(t, "http://10.0.0.3", env.URL)
|
||||
assert.Equal(t, "my-node-secret", env.Token)
|
||||
assert.Equal(t, true, env.Enabled)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,22 +0,0 @@
|
|||
package cron
|
||||
|
||||
import (
|
||||
"github.com/0xJacky/Nginx-UI/internal/kernal"
|
||||
"github.com/0xJacky/Nginx-UI/settings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestRestartLogrotate(t *testing.T) {
|
||||
settings.Init("../../app.ini")
|
||||
|
||||
kernal.InitDatabase()
|
||||
|
||||
InitCronJobs()
|
||||
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
RestartLogrotate()
|
||||
|
||||
time.Sleep(2 * time.Second)
|
||||
}
|
|
@ -1,82 +0,0 @@
|
|||
[app]
|
||||
PageSize = 20
|
||||
JwtSecret = newSecret
|
||||
|
||||
[server]
|
||||
Host = 0.0.0.0
|
||||
Port = 9000
|
||||
RunMode = debug
|
||||
|
||||
[database]
|
||||
Host =
|
||||
Port = 0
|
||||
User =
|
||||
Password =
|
||||
Name = database
|
||||
TablePrefix =
|
||||
|
||||
[auth]
|
||||
IPWhiteList = 127.0.0.1
|
||||
BanThresholdMinutes = 10
|
||||
MaxAttempts = 10
|
||||
|
||||
[casdoor]
|
||||
Endpoint = http://127.0.0.1:8001
|
||||
ClientId = 1234567890qwertyuiop
|
||||
ClientSecret = 1234567890qwertyuiop1234567890qwertyuiop
|
||||
CertificatePath = ./casdoor.pub
|
||||
Organization = built-in
|
||||
Application = nginx-ui-dev
|
||||
RedirectUri =
|
||||
|
||||
[cert]
|
||||
Email = test
|
||||
CADir = /test
|
||||
CertRenewalInterval = 7
|
||||
RecursiveNameservers = 8.8.8.8,1.1.1.1
|
||||
HTTPChallengePort = 9181
|
||||
|
||||
[cluster]
|
||||
Node = http://10.0.0.1:9000?name=test&node_secret=asdfghjklqwertyuiopzxcvbnm&enabled=true
|
||||
|
||||
[crypto]
|
||||
Secret = 12345678901234567890
|
||||
|
||||
[http]
|
||||
GithubProxy = https://mirror.ghproxy.com/
|
||||
InsecureSkipVerify = true
|
||||
|
||||
[logrotate]
|
||||
Enabled = true
|
||||
CMD = logrotate /etc/logrotate.d/nginx
|
||||
Interval = 1440
|
||||
|
||||
[nginx]
|
||||
AccessLogPath =
|
||||
ErrorLogPath =
|
||||
LogDirWhiteList = /var/log/nginx
|
||||
ConfigDir =
|
||||
PIDPath =
|
||||
TestConfigCmd =
|
||||
ReloadCmd =
|
||||
RestartCmd =
|
||||
|
||||
[node]
|
||||
Name = Local
|
||||
Secret =
|
||||
SkipInstallation = false
|
||||
Demo = false
|
||||
|
||||
[openai]
|
||||
BaseUrl =
|
||||
Token =
|
||||
Proxy =
|
||||
Model = gpt-4o
|
||||
|
||||
[terminal]
|
||||
StartCmd = bash
|
||||
|
||||
[webauthn]
|
||||
RPDisplayName = Nginx UI
|
||||
RPID = localhost
|
||||
RPOrigins = http://localhost:3002,http://127.0.0.1:3002
|
|
@ -39,7 +39,7 @@ func TestSetup(t *testing.T) {
|
|||
// Cert
|
||||
_ = os.Setenv("NGINX_UI_CERT_EMAIL", "test")
|
||||
_ = os.Setenv("NGINX_UI_CERT_CA_DIR", "/test/ca")
|
||||
_ = os.Setenv("NGINX_UI_CERT_CERT_RENEWAL_INTERVAL", "14")
|
||||
_ = os.Setenv("NGINX_UI_CERT_RENEWAL_INTERVAL", "14")
|
||||
_ = os.Setenv("NGINX_UI_CERT_RECURSIVE_NAMESERVERS", "8.8.8.8,1.1.1.1")
|
||||
_ = os.Setenv("NGINX_UI_CERT_HTTP_CHALLENGE_PORT", "1080")
|
||||
|
||||
|
@ -72,7 +72,7 @@ func TestSetup(t *testing.T) {
|
|||
|
||||
// Node
|
||||
_ = os.Setenv("NGINX_UI_NODE_NAME", "test")
|
||||
_ = os.Setenv("NGINX_UI_NODE_NODE_SECRET", "nodeSecret")
|
||||
_ = os.Setenv("NGINX_UI_NODE_SECRET", "nodeSecret")
|
||||
_ = os.Setenv("NGINX_UI_NODE_SKIP_INSTALLATION", "true")
|
||||
_ = os.Setenv("NGINX_UI_NODE_DEMO", "true")
|
||||
|
||||
|
|
|
@ -6,46 +6,50 @@ import (
|
|||
"github.com/0xJacky/Nginx-UI/settings"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sashabaranov/go-openai"
|
||||
"github.com/uozi-tech/cosy/sandbox"
|
||||
"io"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestChatGPT(t *testing.T) {
|
||||
settings.Init("../../app.ini")
|
||||
c := openai.NewClient(settings.OpenAISettings.Token)
|
||||
sandbox.NewInstance("../../app.ini", "sqlite").
|
||||
Run(func(instance *sandbox.Instance) {
|
||||
c := openai.NewClient(settings.OpenAISettings.Token)
|
||||
|
||||
ctx := context.Background()
|
||||
ctx := context.Background()
|
||||
|
||||
req := openai.ChatCompletionRequest{
|
||||
Model: openai.GPT3Dot5Turbo0301,
|
||||
Messages: []openai.ChatCompletionMessage{
|
||||
{
|
||||
Role: openai.ChatMessageRoleUser,
|
||||
Content: "帮我写一个 nginx 配置文件的示例",
|
||||
},
|
||||
},
|
||||
Stream: true,
|
||||
}
|
||||
stream, err := c.CreateChatCompletionStream(ctx, req)
|
||||
if err != nil {
|
||||
fmt.Printf("CompletionStream error: %v\n", err)
|
||||
return
|
||||
}
|
||||
defer stream.Close()
|
||||
req := openai.ChatCompletionRequest{
|
||||
Model: openai.GPT3Dot5Turbo0301,
|
||||
Messages: []openai.ChatCompletionMessage{
|
||||
{
|
||||
Role: openai.ChatMessageRoleUser,
|
||||
Content: "帮我写一个 nginx 配置文件的示例",
|
||||
},
|
||||
},
|
||||
Stream: true,
|
||||
}
|
||||
stream, err := c.CreateChatCompletionStream(ctx, req)
|
||||
if err != nil {
|
||||
fmt.Printf("CompletionStream error: %v\n", err)
|
||||
return
|
||||
}
|
||||
defer stream.Close()
|
||||
|
||||
for {
|
||||
response, err := stream.Recv()
|
||||
if errors.Is(err, io.EOF) {
|
||||
return
|
||||
}
|
||||
for {
|
||||
response, err := stream.Recv()
|
||||
if errors.Is(err, io.EOF) {
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Stream error: %v\n", err)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Printf("Stream error: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("%v", response.Choices[0].Delta.Content)
|
||||
_ = os.Stdout.Sync()
|
||||
}
|
||||
})
|
||||
|
||||
fmt.Printf("%v", response.Choices[0].Delta.Content)
|
||||
_ = os.Stdout.Sync()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue