-
-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(func): support virtual host via sharing record #2202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PIKACHUIM
wants to merge
16
commits into
main
Choose a base branch
from
dev-vhost
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
12bd5a0
add(func): spuuort virtual host
PIKACHUIM 8a878fa
feat(func): support virtual host
PIKACHUIM 38f761f
Update internal/op/virtual_host.go
PIKACHUIM 0b54be2
Update internal/db/virtual_host.go
PIKACHUIM a3b1029
Merge branch 'main' into dev-vhost
PIKACHUIM 166af74
Update server/middlewares/virtual_host.go
PIKACHUIM d825769
[WIP] Add support for virtual host functionality (#2212)
Copilot 8489950
Merge branch 'main' into dev-vhost
PIKACHUIM bedf0a9
Merge branch 'main' into dev-vhost
PIKACHUIM ae00551
feat(func): merge vhost_db to sharing_db
PIKACHUIM a5f92bd
Merge branch 'dev-vhost' of https://github.com/OpenListTeam/OpenList …
PIKACHUIM 8b3ef7c
Merge branch 'main' into dev-vhost
PIKACHUIM 379958b
fix(vhost): remove useless files
PIKACHUIM 4ae2d1f
fix(vhost): fix vhost function error
PIKACHUIM da588ba
fix(vhost): fix vhost function error
PIKACHUIM c8b0f5e
Merge branch 'main' into dev-vhost
PIKACHUIM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -191,4 +191,6 @@ const ( | |
| PathKey | ||
| SharingIDKey | ||
| SkipHookKey | ||
| VirtualHostKey | ||
| VhostPrefixKey | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package db | ||
|
|
||
| import ( | ||
| "github.com/OpenListTeam/OpenList/v4/internal/model" | ||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| func GetVirtualHostByDomain(domain string) (*model.VirtualHost, error) { | ||
| var v model.VirtualHost | ||
| if err := db.Where("domain = ?", domain).First(&v).Error; err != nil { | ||
| return nil, errors.Wrapf(err, "failed select virtual host") | ||
| } | ||
| return &v, nil | ||
| } | ||
|
|
||
| func GetVirtualHostById(id uint) (*model.VirtualHost, error) { | ||
| var v model.VirtualHost | ||
| if err := db.First(&v, id).Error; err != nil { | ||
| return nil, errors.Wrapf(err, "failed get virtual host") | ||
| } | ||
| return &v, nil | ||
| } | ||
|
|
||
| func CreateVirtualHost(v *model.VirtualHost) error { | ||
| return errors.WithStack(db.Create(v).Error) | ||
| } | ||
|
|
||
| func UpdateVirtualHost(v *model.VirtualHost) error { | ||
| return errors.WithStack(db.Save(v).Error) | ||
| } | ||
|
|
||
| func GetVirtualHosts(pageIndex, pageSize int) (vhosts []model.VirtualHost, count int64, err error) { | ||
| vhostDB := db.Model(&model.VirtualHost{}) | ||
| if err = vhostDB.Count(&count).Error; err != nil { | ||
| return nil, 0, errors.Wrapf(err, "failed get virtual hosts count") | ||
| } | ||
| if err = vhostDB.Order(columnName("id")).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&vhosts).Error; err != nil { | ||
| return nil, 0, errors.Wrapf(err, "failed find virtual hosts") | ||
| } | ||
| return vhosts, count, nil | ||
| } | ||
|
|
||
| func DeleteVirtualHostById(id uint) error { | ||
| return errors.WithStack(db.Delete(&model.VirtualHost{}, id).Error) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package model | ||
|
|
||
| type VirtualHost struct { | ||
| ID uint `json:"id" gorm:"primaryKey"` | ||
| Enabled bool `json:"enabled"` | ||
| Domain string `json:"domain" gorm:"unique" binding:"required"` | ||
| Path string `json:"path" binding:"required"` | ||
| WebHosting bool `json:"web_hosting"` | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package op | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/OpenListTeam/OpenList/v4/internal/db" | ||
| "github.com/OpenListTeam/OpenList/v4/internal/model" | ||
| "github.com/OpenListTeam/OpenList/v4/pkg/utils" | ||
| "github.com/OpenListTeam/go-cache" | ||
| "github.com/pkg/errors" | ||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| var vhostCache = cache.NewMemCache(cache.WithShards[*model.VirtualHost](2)) | ||
|
|
||
| // GetVirtualHostByDomain 根据域名获取虚拟主机配置(带缓存) | ||
| func GetVirtualHostByDomain(domain string) (*model.VirtualHost, error) { | ||
| if v, ok := vhostCache.Get(domain); ok { | ||
| if v == nil { | ||
| utils.Log.Infof("[VirtualHost] cache hit (nil) for domain=%q", domain) | ||
| return nil, errors.New("virtual host not found") | ||
| } | ||
| utils.Log.Infof("[VirtualHost] cache hit for domain=%q id=%d", domain, v.ID) | ||
| return v, nil | ||
| } | ||
| utils.Log.Infof("[VirtualHost] cache miss for domain=%q, querying db...", domain) | ||
| v, err := db.GetVirtualHostByDomain(domain) | ||
| if err != nil { | ||
| if errors.Is(errors.Cause(err), gorm.ErrRecordNotFound) { | ||
| utils.Log.Infof("[VirtualHost] domain=%q not found in db, caching nil", domain) | ||
| vhostCache.Set(domain, nil, cache.WithEx[*model.VirtualHost](time.Minute*5)) | ||
| return nil, errors.New("virtual host not found") | ||
| } | ||
| utils.Log.Errorf("[VirtualHost] db error for domain=%q: %v", domain, err) | ||
| return nil, err | ||
| } | ||
| utils.Log.Infof("[VirtualHost] db found domain=%q id=%d enabled=%v web_hosting=%v", domain, v.ID, v.Enabled, v.WebHosting) | ||
|
PIKACHUIM marked this conversation as resolved.
Outdated
|
||
| vhostCache.Set(domain, v, cache.WithEx[*model.VirtualHost](time.Hour)) | ||
| return v, nil | ||
| } | ||
|
|
||
| func GetVirtualHostById(id uint) (*model.VirtualHost, error) { | ||
| return db.GetVirtualHostById(id) | ||
| } | ||
|
|
||
| func CreateVirtualHost(v *model.VirtualHost) error { | ||
| v.Path = utils.FixAndCleanPath(v.Path) | ||
| vhostCache.Del(v.Domain) | ||
| return db.CreateVirtualHost(v) | ||
| } | ||
|
|
||
| func UpdateVirtualHost(v *model.VirtualHost) error { | ||
| v.Path = utils.FixAndCleanPath(v.Path) | ||
| old, err := db.GetVirtualHostById(v.ID) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| // 如果域名变更,清除旧域名缓存 | ||
| vhostCache.Del(old.Domain) | ||
| vhostCache.Del(v.Domain) | ||
| return db.UpdateVirtualHost(v) | ||
|
PIKACHUIM marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| func DeleteVirtualHostById(id uint) error { | ||
| old, err := db.GetVirtualHostById(id) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| vhostCache.Del(old.Domain) | ||
| return db.DeleteVirtualHostById(id) | ||
| } | ||
|
|
||
| func GetVirtualHosts(pageIndex, pageSize int) ([]model.VirtualHost, int64, error) { | ||
| return db.GetVirtualHosts(pageIndex, pageSize) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package handles | ||
|
|
||
| import ( | ||
| "strconv" | ||
|
|
||
| "github.com/OpenListTeam/OpenList/v4/internal/model" | ||
| "github.com/OpenListTeam/OpenList/v4/internal/op" | ||
| "github.com/OpenListTeam/OpenList/v4/server/common" | ||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| func ListVirtualHosts(c *gin.Context) { | ||
| var req model.PageReq | ||
| if err := c.ShouldBind(&req); err != nil { | ||
| common.ErrorResp(c, err, 400) | ||
| return | ||
| } | ||
| req.Validate() | ||
| vhosts, total, err := op.GetVirtualHosts(req.Page, req.PerPage) | ||
| if err != nil { | ||
| common.ErrorResp(c, err, 500, true) | ||
| return | ||
| } | ||
| common.SuccessResp(c, common.PageResp{ | ||
| Content: vhosts, | ||
| Total: total, | ||
| }) | ||
| } | ||
|
|
||
| func GetVirtualHost(c *gin.Context) { | ||
| idStr := c.Query("id") | ||
| id, err := strconv.Atoi(idStr) | ||
| if err != nil { | ||
| common.ErrorResp(c, err, 400) | ||
| return | ||
| } | ||
| vhost, err := op.GetVirtualHostById(uint(id)) | ||
| if err != nil { | ||
| common.ErrorResp(c, err, 500, true) | ||
| return | ||
| } | ||
| common.SuccessResp(c, vhost) | ||
| } | ||
|
|
||
| func CreateVirtualHost(c *gin.Context) { | ||
| var req model.VirtualHost | ||
| if err := c.ShouldBind(&req); err != nil { | ||
| common.ErrorResp(c, err, 400) | ||
| return | ||
| } | ||
| if err := op.CreateVirtualHost(&req); err != nil { | ||
| common.ErrorResp(c, err, 500, true) | ||
| } else { | ||
| common.SuccessResp(c) | ||
| } | ||
| } | ||
|
|
||
| func UpdateVirtualHost(c *gin.Context) { | ||
| var req model.VirtualHost | ||
| if err := c.ShouldBind(&req); err != nil { | ||
| common.ErrorResp(c, err, 400) | ||
| return | ||
| } | ||
| if err := op.UpdateVirtualHost(&req); err != nil { | ||
| common.ErrorResp(c, err, 500, true) | ||
| } else { | ||
| common.SuccessResp(c) | ||
| } | ||
| } | ||
|
|
||
| func DeleteVirtualHost(c *gin.Context) { | ||
| idStr := c.Query("id") | ||
| id, err := strconv.Atoi(idStr) | ||
| if err != nil { | ||
| common.ErrorResp(c, err, 400) | ||
| return | ||
| } | ||
| if err := op.DeleteVirtualHostById(uint(id)); err != nil { | ||
| common.ErrorResp(c, err, 500, true) | ||
| return | ||
| } | ||
| common.SuccessResp(c) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.