refactor: refactor app and api

This commit is contained in:
0xJacky 2023-11-29 00:08:44 +08:00
parent 5ab50b8a93
commit 287ef7527d
No known key found for this signature in database
GPG key ID: B6E4A6E4A561BAF0
157 changed files with 8116 additions and 3587 deletions

17
internal/config/config.go Normal file
View file

@ -0,0 +1,17 @@
package config
import (
"github.com/sashabaranov/go-openai"
"time"
)
type Config struct {
Name string `json:"name"`
Content string `json:"content,omitempty"`
ChatGPTMessages []openai.ChatCompletionMessage `json:"chatgpt_messages,omitempty"`
FilePath string `json:"file_path,omitempty"`
ModifiedAt time.Time `json:"modified_at"`
Size int64 `json:"size,omitempty"`
IsDir bool `json:"is_dir"`
Enabled bool `json:"enabled"`
}

View file

@ -0,0 +1,59 @@
package config
import (
"sort"
)
type ConfigsSort struct {
Key string
Order string
ConfigList []Config
}
func boolToInt(b bool) int {
if b {
return 1
}
return 0
}
func (c ConfigsSort) Len() int {
return len(c.ConfigList)
}
func (c ConfigsSort) Less(i, j int) bool {
flag := false
switch c.Key {
case "name":
flag = c.ConfigList[i].Name > c.ConfigList[j].Name
case "modified_at":
flag = c.ConfigList[i].ModifiedAt.After(c.ConfigList[j].ModifiedAt)
case "is_dir":
flag = boolToInt(c.ConfigList[i].IsDir) > boolToInt(c.ConfigList[j].IsDir)
case "enabled":
flag = boolToInt(c.ConfigList[i].Enabled) > boolToInt(c.ConfigList[j].Enabled)
}
if c.Order == "asc" {
flag = !flag
}
return flag
}
func (c ConfigsSort) Swap(i, j int) {
c.ConfigList[i], c.ConfigList[j] = c.ConfigList[j], c.ConfigList[i]
}
func Sort(key string, order string, configs []Config) []Config {
configsSort := ConfigsSort{
Key: key,
ConfigList: configs,
Order: order,
}
sort.Sort(configsSort)
return configsSort.ConfigList
}

View file

@ -1,62 +0,0 @@
package config_list
import (
"github.com/gin-gonic/gin"
"sort"
"time"
)
type MapsSort struct {
Key string
Type string
Order string
MapList []gin.H
}
func boolToInt(b bool) int {
if b {
return 1
}
return 0
}
func (m MapsSort) Len() int {
return len(m.MapList)
}
func (m MapsSort) Less(i, j int) bool {
flag := false
if m.Type == "int" {
flag = m.MapList[i][m.Key].(int) > m.MapList[j][m.Key].(int)
} else if m.Type == "bool" {
flag = boolToInt(m.MapList[i][m.Key].(bool)) > boolToInt(m.MapList[j][m.Key].(bool))
} else if m.Type == "bool" {
flag = m.MapList[i][m.Key].(string) > m.MapList[j][m.Key].(string)
} else if m.Type == "time" {
flag = m.MapList[i][m.Key].(time.Time).After(m.MapList[j][m.Key].(time.Time))
}
if m.Order == "asc" {
flag = !flag
}
return flag
}
func (m MapsSort) Swap(i, j int) {
m.MapList[i], m.MapList[j] = m.MapList[j], m.MapList[i]
}
func Sort(key string, order string, Type string, maps []gin.H) []gin.H {
mapsSort := MapsSort{
Key: key,
MapList: maps,
Type: Type,
Order: order,
}
sort.Sort(mapsSort)
return mapsSort.MapList
}

View file

@ -4,7 +4,7 @@ import (
"bufio"
"bytes"
"github.com/0xJacky/Nginx-UI/internal/logger"
nginx2 "github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/settings"
templ "github.com/0xJacky/Nginx-UI/template"
"github.com/BurntSushi/toml"
@ -12,23 +12,24 @@ import (
"github.com/pkg/errors"
"github.com/tufanbarisyildirim/gonginx/parser"
"io"
"io/fs"
"path/filepath"
"strings"
"text/template"
)
type TVariable struct {
type Variable struct {
Type string `json:"type"`
Name map[string]string `json:"name"`
Value interface{} `json:"value"`
}
type ConfigInfoItem struct {
Name string `json:"name"`
Description map[string]string `json:"description"`
Author string `json:"author"`
Filename string `json:"filename"`
Variables map[string]TVariable `json:"variables"`
Name string `json:"name"`
Description map[string]string `json:"description"`
Author string `json:"author"`
Filename string `json:"filename"`
Variables map[string]Variable `json:"variables"`
}
func GetTemplateInfo(path, name string) (configListItem ConfigInfoItem) {
@ -38,7 +39,14 @@ func GetTemplateInfo(path, name string) (configListItem ConfigInfoItem) {
}
file, _ := templ.DistFS.Open(filepath.Join(path, name))
defer file.Close()
defer func(file fs.File) {
err := file.Close()
if err != nil {
logger.Error(err)
}
}(file)
r := bufio.NewReader(file)
bytes, _, err := r.ReadLine()
if err == io.EOF {
@ -71,10 +79,10 @@ func GetTemplateInfo(path, name string) (configListItem ConfigInfoItem) {
type ConfigDetail struct {
Custom string `json:"custom"`
nginx2.NgxServer
nginx.NgxServer
}
func ParseTemplate(path, name string, bindData map[string]TVariable) (c ConfigDetail, err error) {
func ParseTemplate(path, name string, bindData map[string]Variable) (c ConfigDetail, err error) {
file, err := templ.DistFS.Open(filepath.Join(path, name))
if err != nil {
err = errors.Wrap(err, "error tokenized template")
@ -160,14 +168,14 @@ func ParseTemplate(path, name string, bindData map[string]TVariable) (c ConfigDe
c.Custom = custom
for _, d := range config.GetDirectives() {
switch d.GetName() {
case nginx2.Location:
l := &nginx2.NgxLocation{
case nginx.Location:
l := &nginx.NgxLocation{
Path: strings.Join(d.GetParameters(), " "),
}
l.ParseLocation(d, 0)
c.NgxServer.Locations = append(c.NgxServer.Locations, l)
default:
dir := &nginx2.NgxDirective{
dir := &nginx.NgxDirective{
Directive: d.GetName(),
}
dir.ParseDirective(d, 0)