parent
594c61a0ff
commit
f5a2a634a1
@ -0,0 +1,25 @@
|
||||
import Curd from '@/api/curd'
|
||||
import http from '@/lib/http'
|
||||
|
||||
class Template extends Curd {
|
||||
get_config_list() {
|
||||
return http.get('template/configs')
|
||||
}
|
||||
|
||||
get_block_list() {
|
||||
return http.get('template/blocks')
|
||||
}
|
||||
|
||||
get_config(name: string) {
|
||||
return http.get('template/config/' + name)
|
||||
}
|
||||
|
||||
get_block(name: string) {
|
||||
return http.get('template/block/' + name)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const template = new Template('/template')
|
||||
|
||||
export default template
|
||||
@ -0,0 +1,106 @@
|
||||
<script setup lang="ts">
|
||||
import {useGettext} from 'vue3-gettext'
|
||||
import template from '@/api/template'
|
||||
import {computed, ref} from 'vue'
|
||||
import {storeToRefs} from 'pinia'
|
||||
import {useSettingsStore} from '@/pinia'
|
||||
import Template from '@/views/template/Template.vue'
|
||||
import DirectiveEditor from '@/views/domain/ngx_conf/directive/DirectiveEditor.vue'
|
||||
import LocationEditor from '@/views/domain/ngx_conf/LocationEditor.vue'
|
||||
import CodeEditor from '@/components/CodeEditor/CodeEditor.vue'
|
||||
|
||||
const {$gettext} = useGettext()
|
||||
const {language} = storeToRefs(useSettingsStore())
|
||||
const props = defineProps(['ngx_config', 'current_server_index'])
|
||||
|
||||
const blocks = ref([])
|
||||
const data: any = ref({})
|
||||
const visible = ref(false)
|
||||
|
||||
function get_block_list() {
|
||||
template.get_block_list().then(r => {
|
||||
blocks.value = r.data
|
||||
})
|
||||
}
|
||||
|
||||
get_block_list()
|
||||
|
||||
function view(name: string) {
|
||||
visible.value = true
|
||||
template.get_block(name).then(r => {
|
||||
data.value = r
|
||||
})
|
||||
}
|
||||
|
||||
const trans_description = computed(() => {
|
||||
return (item: any) => item.description?.[language.value] ?? item.description?.en ?? ''
|
||||
})
|
||||
|
||||
async function add() {
|
||||
|
||||
if (data.value.custom) {
|
||||
props.ngx_config.custom += '\n' + data.value.custom
|
||||
}
|
||||
|
||||
props.ngx_config.custom = props.ngx_config.custom.trim()
|
||||
|
||||
if (data.value.locations) {
|
||||
props.ngx_config.servers[props.current_server_index].locations.push(...data.value.locations)
|
||||
}
|
||||
|
||||
if (data.value.directives) {
|
||||
props.ngx_config.servers[props.current_server_index].directives.push(...data.value.directives)
|
||||
}
|
||||
|
||||
visible.value = false
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<h2 v-translate>Config Templates</h2>
|
||||
<div class="config-list-wrapper">
|
||||
<a-list
|
||||
:grid="{ gutter: 16, xs: 1, sm: 2, md: 2, lg: 2, xl: 2, xxl: 2, xxxl: 2 }"
|
||||
:data-source="blocks"
|
||||
>
|
||||
<template #renderItem="{ item }">
|
||||
<a-list-item>
|
||||
<a-card size="small" :title="item.name">
|
||||
<template #extra>
|
||||
<a-button type="link" @click="view(item.filename)">View</a-button>
|
||||
</template>
|
||||
<p>{{ $gettext('Author') }}: {{ item.author }}</p>
|
||||
<p>{{ $gettext('Description') }}: {{ trans_description(item) }}</p>
|
||||
</a-card>
|
||||
</a-list-item>
|
||||
</template>
|
||||
</a-list>
|
||||
</div>
|
||||
<a-modal
|
||||
:title="data.name"
|
||||
v-model:visible="visible"
|
||||
:mask="false"
|
||||
:ok-text="$gettext('Add')"
|
||||
@ok="add"
|
||||
>
|
||||
<p>{{ $gettext('Author') }}: {{ data.author }}</p>
|
||||
<p>{{ $gettext('Description') }}: {{ trans_description(data) }}</p>
|
||||
<template v-if="data.custom">
|
||||
<h2>{{ $gettext('Custom') }}</h2>
|
||||
<code-editor v-model:content="data.custom" default-height="150px"/>
|
||||
</template>
|
||||
<directive-editor v-if="data.directives" :ngx_directives="data.directives" :readonly="true"/>
|
||||
<br/>
|
||||
<location-editor v-if="data.locations" :locations="data.locations" :readonly="true"/>
|
||||
</a-modal>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style lang="less" scoped>
|
||||
.config-list-wrapper {
|
||||
max-height: 200px;
|
||||
overflow-y: scroll;
|
||||
overflow-x: hidden;
|
||||
}
|
||||
</style>
|
||||
@ -0,0 +1,145 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"github.com/0xJacky/Nginx-UI/server/pkg/nginx"
|
||||
"github.com/0xJacky/Nginx-UI/template"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/tufanbarisyildirim/gonginx/parser"
|
||||
"io"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ConfigInfoItem struct {
|
||||
Name string `json:"name"`
|
||||
Description map[string]string `json:"description"`
|
||||
Author string `json:"author"`
|
||||
Filename string `json:"filename"`
|
||||
}
|
||||
|
||||
func GetTemplateInfo(path, name string) (configListItem ConfigInfoItem) {
|
||||
configListItem = ConfigInfoItem{
|
||||
Description: make(map[string]string),
|
||||
Filename: name,
|
||||
}
|
||||
|
||||
file, _ := template.DistFS.Open(filepath.Join(path, name))
|
||||
defer file.Close()
|
||||
r := bufio.NewReader(file)
|
||||
bytes, _, err := r.ReadLine()
|
||||
if err == io.EOF {
|
||||
return
|
||||
}
|
||||
line := strings.TrimSpace(string(bytes))
|
||||
|
||||
if line != "# Nginx UI Template Start" {
|
||||
return
|
||||
}
|
||||
var content string
|
||||
for {
|
||||
bytes, _, err = r.ReadLine()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
line = strings.TrimSpace(string(bytes))
|
||||
if line == "# Nginx UI Template End" {
|
||||
break
|
||||
}
|
||||
content += line + "\n"
|
||||
}
|
||||
re := regexp.MustCompile(`# (\S+): (.*)`)
|
||||
matches := re.FindAllStringSubmatch(content, -1)
|
||||
for _, match := range matches {
|
||||
if len(match) < 3 {
|
||||
continue
|
||||
}
|
||||
key := match[1]
|
||||
switch {
|
||||
case key == "Name":
|
||||
configListItem.Name = match[2]
|
||||
case key == "Author":
|
||||
configListItem.Author = match[2]
|
||||
case strings.Contains(key, "Description"):
|
||||
re = regexp.MustCompile(`(\w+)\[(\w+)\]`)
|
||||
matches = re.FindAllStringSubmatch(key, -1)
|
||||
for _, m := range matches {
|
||||
if len(m) < 3 {
|
||||
continue
|
||||
}
|
||||
// lang => description
|
||||
configListItem.Description[m[2]] = match[2]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
type ConfigDetail struct {
|
||||
Custom string `json:"custom"`
|
||||
nginx.NgxServer
|
||||
}
|
||||
|
||||
func ParseTemplate(path, name string) (c ConfigDetail, err error) {
|
||||
file, err := template.DistFS.Open(filepath.Join(path, name))
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "error tokenized template")
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
r := bufio.NewReader(file)
|
||||
var flag bool
|
||||
custom := ""
|
||||
content := ""
|
||||
for {
|
||||
bytes, _, err := r.ReadLine()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
orig := string(bytes)
|
||||
line := strings.TrimSpace(orig)
|
||||
switch {
|
||||
case line == "# Nginx UI Custom Start":
|
||||
flag = true
|
||||
case line == "# Nginx UI Custom End":
|
||||
flag = false
|
||||
case flag == true:
|
||||
custom += orig + "\n"
|
||||
case flag == false:
|
||||
content += orig + "\n"
|
||||
}
|
||||
}
|
||||
p := parser.NewStringParser(content)
|
||||
config := p.Parse()
|
||||
c.Custom = custom
|
||||
for _, d := range config.GetDirectives() {
|
||||
switch d.GetName() {
|
||||
case nginx.Location:
|
||||
l := &nginx.NgxLocation{}
|
||||
l.ParseLocation(d, 0)
|
||||
c.NgxServer.Locations = append(c.NgxServer.Locations, l)
|
||||
default:
|
||||
dir := &nginx.NgxDirective{}
|
||||
dir.ParseDirective(d, 0)
|
||||
c.NgxServer.Directives = append(c.NgxServer.Directives, dir)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func GetTemplateList(path string) (configList []ConfigInfoItem, err error) {
|
||||
configs, err := template.DistFS.ReadDir(path)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "error get template list")
|
||||
return
|
||||
}
|
||||
|
||||
for _, config := range configs {
|
||||
configList = append(configList, GetTemplateInfo(path, config.Name()))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@ -1,3 +1,9 @@
|
||||
location / {
|
||||
try_files $uri $uri/ /index.php;
|
||||
}
|
||||
# Nginx UI Template Start
|
||||
# Name: Codeigniter Rewrite
|
||||
# Description[en]: Codeigniter URL Rewrite Config
|
||||
# Description[zh_CN]: Codeigniter 伪静态配置
|
||||
# Author: @0xJacky
|
||||
# Nginx UI Template End
|
||||
location / {
|
||||
try_files $uri $uri/ /index.php;
|
||||
}
|
||||
|
||||
@ -0,0 +1,13 @@
|
||||
# Nginx UI Template Start
|
||||
# Name: PHP8.1
|
||||
# Description[en]: Enabled PHP 8.1 Config
|
||||
# Description[zh_CN]: 启用 PHP 8.1 配置
|
||||
# Author: @0xJacky
|
||||
# Nginx UI Template End
|
||||
location ~ [^/]\.php(/|$)
|
||||
{
|
||||
try_files $uri =404;
|
||||
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
|
||||
fastcgi_index index.php;
|
||||
include fastcgi.conf;
|
||||
}
|
||||
@ -1,3 +1,9 @@
|
||||
location / {
|
||||
try_files $uri $uri/ /server.php?$query_string;
|
||||
}
|
||||
# Nginx UI Template Start
|
||||
# Name: Laravel Rewrite
|
||||
# Description[en]: Laravel URL Rewrite Config
|
||||
# Description[zh_CN]: Laravel 伪静态配置
|
||||
# Author: @0xJacky
|
||||
# Nginx UI Template End
|
||||
location / {
|
||||
try_files $uri $uri/ /server.php?$query_string;
|
||||
}
|
||||
|
||||
@ -0,0 +1,15 @@
|
||||
# Nginx UI Template Start
|
||||
# Name: Reverse Proxy
|
||||
# Description[en]: Reverse Proxy Config
|
||||
# Description[zh_CN]: 反向代理配置
|
||||
# Author: @0xJacky
|
||||
# Nginx UI Template End
|
||||
location / {
|
||||
proxy_pass http://127.0.0.1:9000/;
|
||||
proxy_redirect off;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
client_max_body_size 1000m;
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
# Nginx UI Template Start
|
||||
# Name: Reverse Proxy WebSocket
|
||||
# Description[en]: Reverse Proxy with WebSocket Config
|
||||
# Description[zh_CN]: 反向代理 WebSocket 配置
|
||||
# Author: @0xJacky
|
||||
# Nginx UI Template End
|
||||
|
||||
# Nginx UI Custom Start
|
||||
map $http_upgrade $connection_upgrade {
|
||||
default upgrade;
|
||||
'' close;
|
||||
}
|
||||
# Nginx UI Custom End
|
||||
|
||||
location / {
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection $connection_upgrade;
|
||||
proxy_pass http://127.0.0.1:9000/;
|
||||
proxy_redirect off;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
client_max_body_size 1000m;
|
||||
}
|
||||
Loading…
Reference in New Issue