Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
caddyproxy
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
caddyproxy
Commits
6ca129da
Commit
6ca129da
authored
Oct 07, 2024
by
vicotor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update create website
parent
52db8d1c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
1 deletion
+38
-1
caddyapi.go
caddy/caddyapi.go
+38
-1
No files found.
caddy/caddyapi.go
View file @
6ca129da
...
...
@@ -8,8 +8,10 @@ import (
"github.com/xueqianlu/caddyproxy/utils"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"text/template"
"time"
)
type
CaddyAPI
struct
{
...
...
@@ -115,9 +117,44 @@ func (c *CaddyAPI) buildPayloadForForwardWebsite(domain string, target string) (
return
buffer
.
String
(),
nil
}
func
pathExist
(
path
string
)
bool
{
_
,
err
:=
os
.
Stat
(
path
)
if
os
.
IsNotExist
(
err
)
{
return
false
}
return
true
}
func
(
c
*
CaddyAPI
)
CreateWebsite
(
domain
string
,
resource
string
)
error
{
// unzip resource to the website root directory.
websiteRoot
:=
filepath
.
Join
(
c
.
RootDir
,
"websites"
,
domain
)
rollback
:=
false
success
:=
false
backupname
:=
fmt
.
Sprintf
(
"%s.%s.bak"
,
websiteRoot
,
time
.
Now
()
.
Format
(
"2006-01-02-15-04-05"
))
defer
func
()
{
if
!
success
&&
rollback
{
os
.
Remove
(
websiteRoot
)
if
err
:=
os
.
Rename
(
backupname
,
websiteRoot
);
err
!=
nil
{
log
.
WithFields
(
log
.
Fields
{
"backup"
:
backupname
,
"website"
:
websiteRoot
,
})
.
WithError
(
err
)
.
Error
(
"failed to rollback website"
)
}
}
}()
// if websiteRoot exist, remove it first. and add rollback flag.
if
pathExist
(
websiteRoot
)
{
// backup old website.
if
err
:=
os
.
Rename
(
websiteRoot
,
backupname
);
err
!=
nil
{
log
.
WithFields
(
log
.
Fields
{
"backup"
:
backupname
,
"website"
:
websiteRoot
,
})
.
WithError
(
err
)
.
Error
(
"failed to backup website"
)
return
err
}
else
{
rollback
=
true
}
}
if
err
:=
utils
.
Unzip
(
resource
,
websiteRoot
);
err
!=
nil
{
return
err
}
...
...
@@ -138,7 +175,7 @@ func (c *CaddyAPI) CreateWebsite(domain string, resource string) error {
if
err
:=
c
.
put
(
path
,
[]
byte
(
payload
));
err
!=
nil
{
return
err
}
success
=
true
return
nil
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment