Skip to content

Commit

Permalink
Merge pull request #361 from 0xJacky/refresh-24.04
Browse files Browse the repository at this point in the history
refactor: refresh webui and certificates module
  • Loading branch information
0xJacky authored Apr 30, 2024
2 parents 59d59dd + a0013b5 commit cfa7629
Show file tree
Hide file tree
Showing 158 changed files with 8,471 additions and 6,102 deletions.
96 changes: 96 additions & 0 deletions api/certificate/acme_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package certificate

import (
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/internal/cosy"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/0xJacky/Nginx-UI/settings"
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
"net/http"
)

func GetAcmeUser(c *gin.Context) {
u := query.AcmeUser
id := cast.ToInt(c.Param("id"))
user, err := u.FirstByID(id)
if err != nil {
api.ErrHandler(c, err)
return
}
c.JSON(http.StatusOK, user)
}

func CreateAcmeUser(c *gin.Context) {
cosy.Core[model.AcmeUser](c).SetValidRules(gin.H{
"name": "required",
"email": "required,email",
"ca_dir": "omitempty",
}).BeforeExecuteHook(func(ctx *cosy.Ctx[model.AcmeUser]) {
if ctx.Model.CADir == "" {
ctx.Model.CADir = settings.ServerSettings.CADir
}
err := ctx.Model.Register()
if err != nil {
ctx.AbortWithError(err)
return
}
}).Create()
}

func ModifyAcmeUser(c *gin.Context) {
cosy.Core[model.AcmeUser](c).SetValidRules(gin.H{
"name": "omitempty",
"email": "omitempty,email",
"ca_dir": "omitempty",
}).BeforeExecuteHook(func(ctx *cosy.Ctx[model.AcmeUser]) {
if ctx.Model.CADir == "" {
ctx.Model.CADir = settings.ServerSettings.CADir
}

if ctx.OriginModel.Email != ctx.Model.Email ||
ctx.OriginModel.CADir != ctx.Model.CADir {
err := ctx.Model.Register()
if err != nil {
ctx.AbortWithError(err)
return
}
}
}).Modify()
}

func GetAcmeUserList(c *gin.Context) {
cosy.Core[model.AcmeUser](c).
SetFussy("name", "email").
PagingList()
}

func DestroyAcmeUser(c *gin.Context) {
cosy.Core[model.AcmeUser](c).Destroy()
}

func RecoverAcmeUser(c *gin.Context) {
cosy.Core[model.AcmeUser](c).Recover()
}

func RegisterAcmeUser(c *gin.Context) {
id := cast.ToInt(c.Param("id"))
u := query.AcmeUser
user, err := u.FirstByID(id)
if err != nil {
api.ErrHandler(c, err)
return
}
err = user.Register()
if err != nil {
api.ErrHandler(c, err)
return
}
_, err = u.Where(u.ID.Eq(id)).Updates(user)
if err != nil {
api.ErrHandler(c, err)
return
}
c.JSON(http.StatusOK, user)
}
5 changes: 4 additions & 1 deletion api/certificate/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package certificate

import (
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/api/cosy"
"github.com/0xJacky/Nginx-UI/internal/cert"
"github.com/0xJacky/Nginx-UI/internal/cosy"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -85,6 +85,7 @@ type certJson struct {
KeyType certcrypto.KeyType `json:"key_type" binding:"omitempty,auto_cert_key_type"`
ChallengeMethod string `json:"challenge_method"`
DnsCredentialID int `json:"dns_credential_id"`
ACMEUserID int `json:"acme_user_id"`
}

func AddCert(c *gin.Context) {
Expand All @@ -101,6 +102,7 @@ func AddCert(c *gin.Context) {
KeyType: json.KeyType,
ChallengeMethod: json.ChallengeMethod,
DnsCredentialID: json.DnsCredentialID,
ACMEUserID: json.ACMEUserID,
}

err := certModel.Insert()
Expand Down Expand Up @@ -151,6 +153,7 @@ func ModifyCert(c *gin.Context) {
ChallengeMethod: json.ChallengeMethod,
KeyType: json.KeyType,
DnsCredentialID: json.DnsCredentialID,
ACMEUserID: json.ACMEUserID,
})

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion api/certificate/dns_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package certificate

import (
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/api/cosy"
"github.com/0xJacky/Nginx-UI/internal/cert/dns"
"github.com/0xJacky/Nginx-UI/internal/cosy"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/gin-gonic/gin"
Expand Down
25 changes: 18 additions & 7 deletions api/certificate/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/model"
"github.com/gin-gonic/gin"
"github.com/go-acme/lego/v4/certcrypto"
"github.com/gorilla/websocket"
"net/http"
"strings"
Expand All @@ -18,10 +19,11 @@ const (
)

type IssueCertResponse struct {
Status string `json:"status"`
Message string `json:"message"`
SSLCertificate string `json:"ssl_certificate,omitempty"`
SSLCertificateKey string `json:"ssl_certificate_key,omitempty"`
Status string `json:"status"`
Message string `json:"message"`
SSLCertificate string `json:"ssl_certificate,omitempty"`
SSLCertificateKey string `json:"ssl_certificate_key,omitempty"`
KeyType certcrypto.KeyType `json:"key_type"`
}

func handleIssueCertLogChan(conn *websocket.Conn, log *cert.Logger, logChan chan string) {
Expand Down Expand Up @@ -75,13 +77,20 @@ func IssueCert(c *gin.Context) {
return
}

certModel, err := model.FirstOrCreateCert(c.Param("name"))

certModel, err := model.FirstOrCreateCert(c.Param("name"), payload.GetKeyType())
if err != nil {
logger.Error(err)
return
}

certInfo, err := cert.GetCertInfo(certModel.SSLCertificatePath)
if err != nil {
logger.Error("get certificate info error", err)
return
}
payload.Resource = certModel.Resource
payload.NotBefore = certInfo.NotBefore

logChan := make(chan string, 1)
errChan := make(chan error, 1)

Expand Down Expand Up @@ -113,7 +122,7 @@ func IssueCert(c *gin.Context) {
return
}

certDirName := strings.Join(payload.ServerName, "_")
certDirName := strings.Join(payload.ServerName, "_") + "_" + string(payload.GetKeyType())
sslCertificatePath := nginx.GetConfPath("ssl", certDirName, "fullchain.cer")
sslCertificateKeyPath := nginx.GetConfPath("ssl", certDirName, "private.key")

Expand All @@ -125,6 +134,7 @@ func IssueCert(c *gin.Context) {
KeyType: payload.KeyType,
ChallengeMethod: payload.ChallengeMethod,
DnsCredentialID: payload.DNSCredentialID,
Resource: payload.Resource,
})

if err != nil {
Expand All @@ -144,6 +154,7 @@ func IssueCert(c *gin.Context) {
Message: "Issued certificate successfully",
SSLCertificate: sslCertificatePath,
SSLCertificateKey: sslCertificateKeyPath,
KeyType: payload.GetKeyType(),
})

if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions api/certificate/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,13 @@ func InitCertificateRouter(r *gin.RouterGroup) {
func InitCertificateWebSocketRouter(r *gin.RouterGroup) {
r.GET("domain/:name/cert", IssueCert)
}

func InitAcmeUserRouter(r *gin.RouterGroup) {
r.GET("acme_users", GetAcmeUserList)
r.GET("acme_user/:id", GetAcmeUser)
r.POST("acme_user", CreateAcmeUser)
r.POST("acme_user/:id", ModifyAcmeUser)
r.POST("acme_user/:id/register", RegisterAcmeUser)
r.DELETE("acme_user/:id", DestroyAcmeUser)
r.PATCH("acme_user/:id", RecoverAcmeUser)
}
2 changes: 1 addition & 1 deletion api/notification/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package notification

import (
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/api/cosy"
"github.com/0xJacky/Nginx-UI/internal/cosy"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/gin-gonic/gin"
Expand Down
11 changes: 7 additions & 4 deletions api/sites/auto_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@ package sites

import (
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/model"
"github.com/gin-gonic/gin"
"github.com/go-acme/lego/v4/certcrypto"
"net/http"
)

func AddDomainToAutoCert(c *gin.Context) {
name := c.Param("name")

var json struct {
DnsCredentialID int `json:"dns_credential_id"`
ChallengeMethod string `json:"challenge_method"`
Domains []string `json:"domains"`
DnsCredentialID int `json:"dns_credential_id"`
ChallengeMethod string `json:"challenge_method"`
Domains []string `json:"domains"`
KeyType certcrypto.KeyType `json:"key_type"`
}

if !api.BindAndValid(c, &json) {
return
}

certModel, err := model.FirstOrCreateCert(name)
certModel, err := model.FirstOrCreateCert(name, helper.GetKeyType(json.KeyType))

if err != nil {
api.ErrHandler(c, err)
Expand Down
2 changes: 1 addition & 1 deletion api/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package user

import (
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/api/cosy"
"github.com/0xJacky/Nginx-UI/internal/cosy"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/0xJacky/Nginx-UI/settings"
Expand Down
8 changes: 4 additions & 4 deletions app/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports = {
before: false,
after: true,
}],
'key-spacing': ['error', { afterColon: true }],
'key-spacing': ['error', {afterColon: true}],

'vue/first-attribute-linebreak': ['error', {
singleline: 'beside',
Expand Down Expand Up @@ -78,7 +78,7 @@ module.exports = {
],

// Ignore _ as unused variable
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_+$' }],
'@typescript-eslint/no-unused-vars': ['error', {argsIgnorePattern: '^_+$'}],

'array-element-newline': ['error', 'consistent'],
'array-bracket-newline': ['error', 'consistent'],
Expand Down Expand Up @@ -111,7 +111,7 @@ module.exports = {

// Plugin: eslint-plugin-import
'import/prefer-default-export': 'off',
'import/newline-after-import': ['error', { count: 1 }],
'import/newline-after-import': ['error', {count: 1}],
'no-restricted-imports': ['error', 'vuetify/components'],

// For omitting extension for ts files
Expand Down Expand Up @@ -150,7 +150,7 @@ module.exports = {

// ESLint plugin vue
'vue/component-api-style': 'error',
'vue/component-name-in-template-casing': ['error', 'PascalCase', { registeredComponentsOnly: false }],
'vue/component-name-in-template-casing': ['error', 'PascalCase', {registeredComponentsOnly: false}],
'vue/custom-event-name-casing': ['error', 'camelCase', {
ignores: [
'/^(click):[a-z]+((\d)|([A-Z0-9][a-z0-9]+))*([A-Z])?/',
Expand Down
12 changes: 12 additions & 0 deletions app/auto-imports.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
// Generated by unplugin-auto-import
export {}
declare global {
const $gettext: typeof import('@/gettext')['$gettext']
const $ngettext: typeof import('@/gettext')['$ngettext']
const $npgettext: typeof import('@/gettext')['$npgettext']
const $pgettext: typeof import('@/gettext')['$pgettext']
const EffectScope: typeof import('vue')['EffectScope']
const acceptHMRUpdate: typeof import('pinia')['acceptHMRUpdate']
const computed: typeof import('vue')['computed']
Expand Down Expand Up @@ -86,6 +90,10 @@ import { UnwrapRef } from 'vue'
declare module 'vue' {
interface GlobalComponents {}
interface ComponentCustomProperties {
readonly $gettext: UnwrapRef<typeof import('@/gettext')['$gettext']>
readonly $ngettext: UnwrapRef<typeof import('@/gettext')['$ngettext']>
readonly $npgettext: UnwrapRef<typeof import('@/gettext')['$npgettext']>
readonly $pgettext: UnwrapRef<typeof import('@/gettext')['$pgettext']>
readonly EffectScope: UnwrapRef<typeof import('vue')['EffectScope']>
readonly acceptHMRUpdate: UnwrapRef<typeof import('pinia')['acceptHMRUpdate']>
readonly computed: UnwrapRef<typeof import('vue')['computed']>
Expand Down Expand Up @@ -160,6 +168,10 @@ declare module 'vue' {
declare module '@vue/runtime-core' {
interface GlobalComponents {}
interface ComponentCustomProperties {
readonly $gettext: UnwrapRef<typeof import('@/gettext')['$gettext']>
readonly $ngettext: UnwrapRef<typeof import('@/gettext')['$ngettext']>
readonly $npgettext: UnwrapRef<typeof import('@/gettext')['$npgettext']>
readonly $pgettext: UnwrapRef<typeof import('@/gettext')['$pgettext']>
readonly EffectScope: UnwrapRef<typeof import('vue')['EffectScope']>
readonly acceptHMRUpdate: UnwrapRef<typeof import('pinia')['acceptHMRUpdate']>
readonly computed: UnwrapRef<typeof import('vue')['computed']>
Expand Down
3 changes: 3 additions & 0 deletions app/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ declare module 'vue' {
ACollapsePanel: typeof import('ant-design-vue/es')['CollapsePanel']
AComment: typeof import('ant-design-vue/es')['Comment']
AConfigProvider: typeof import('ant-design-vue/es')['ConfigProvider']
ADescriptions: typeof import('ant-design-vue/es')['Descriptions']
ADescriptionsItem: typeof import('ant-design-vue/es')['DescriptionsItem']
ADivider: typeof import('ant-design-vue/es')['Divider']
ADrawer: typeof import('ant-design-vue/es')['Drawer']
ADropdown: typeof import('ant-design-vue/es')['Dropdown']
Expand Down Expand Up @@ -80,6 +82,7 @@ declare module 'vue' {
SetLanguageSetLanguage: typeof import('./src/components/SetLanguage/SetLanguage.vue')['default']
StdDesignStdDataDisplayStdBatchEdit: typeof import('./src/components/StdDesign/StdDataDisplay/StdBatchEdit.vue')['default']
StdDesignStdDataDisplayStdCurd: typeof import('./src/components/StdDesign/StdDataDisplay/StdCurd.vue')['default']
StdDesignStdDataDisplayStdCurdDetail: typeof import('./src/components/StdDesign/StdDataDisplay/StdCurdDetail.vue')['default']
StdDesignStdDataDisplayStdPagination: typeof import('./src/components/StdDesign/StdDataDisplay/StdPagination.vue')['default']
StdDesignStdDataDisplayStdTable: typeof import('./src/components/StdDesign/StdDataDisplay/StdTable.vue')['default']
StdDesignStdDataEntryComponentsStdPassword: typeof import('./src/components/StdDesign/StdDataEntry/components/StdPassword.vue')['default']
Expand Down
8 changes: 4 additions & 4 deletions app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nginx-ui-app-next",
"version": "2.0.0-beta.18",
"version": "2.0.0-beta.19",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down Expand Up @@ -29,14 +29,14 @@
"reconnecting-websocket": "^4.4.0",
"sortablejs": "^1.15.2",
"vite-plugin-build-id": "^0.2.8",
"vue": "^3.4.25",
"vue": "^3.4.26",
"vue-github-button": "github:0xJacky/vue-github-button",
"vue-router": "^4.3.2",
"vue3-ace-editor": "2.2.4",
"vue3-apexcharts": "1.4.4",
"vue3-gettext": "3.0.0-beta.4",
"vuedraggable": "^4.1.0",
"xterm": "^5.3.0",
"@xterm/xterm": "^5.5.0",
"@xterm/addon-attach": "^0.11.0",
"@xterm/addon-fit": "^0.10.0"
},
Expand All @@ -63,7 +63,7 @@
"less": "^4.2.0",
"postcss": "^8.4.38",
"tailwindcss": "^3.4.3",
"typescript": "^5.4.5",
"typescript": "5.3.3",
"unplugin-auto-import": "^0.17.5",
"unplugin-vue-components": "^0.26.0",
"unplugin-vue-define-options": "^1.4.3",
Expand Down
Loading

0 comments on commit cfa7629

Please sign in to comment.