Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
archmagece committed Sep 5, 2024
1 parent 1a867b7 commit b2bf910
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 25 deletions.
2 changes: 1 addition & 1 deletion configs/apt_mirror_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ func TestAptMirrorConfig_ReadConfig(t *testing.T) {
cfg := AptMirrorConfig{}
cfg.ReadConfig()
fmt.Println(cfg)
assert.Equal(t, cfg.Path, "~/tmp/mirror/apt")
assert.Equal(t, cfg.Path, "mirror/apt")
fmt.Println(helpers.ToStringYaml(cfg))
}
2 changes: 1 addition & 1 deletion configs/apt_proxy_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ func TestAptProxyConfig_ReadConfig(t *testing.T) {
cfg := AptProxyConfig{}
cfg.ReadConfig()
fmt.Println(cfg)
assert.Equal(t, cfg.Path, "~/tmp/proxy/apt")
assert.Equal(t, cfg.Path, "proxy/apt")
fmt.Println(helpers.ToStringYaml(cfg))
}
22 changes: 7 additions & 15 deletions configs/global_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,20 @@ package configs
import (
"deproxy/helpers"
"path"
"strings"
)

type Cache struct {
TTL int `yaml:"ttl,omitempty" default:36000`
}

type GlobalConfig struct {
StorageDir string `yaml:"storage_dir,omitempty"`
//StorageDir string `yaml:"storage_dir,omitempty"`
//ConfigDir string `yaml:"config_dir,omitempty"`
//ProxyName
Cache Cache `yaml:"cache,omitempty"`
}

func (cfg *GlobalConfig) ReadConfig() {
confDir := helpers.GetConfigDir()
helpers.ReadYaml(path.Join(confDir, "global.yaml"), cfg)

cfg.StorageDir = helpers.GetStorageDir()
if cfg.StorageDir == "" {
cfg.StorageDir = "~/tmp/deproxy"
}
if strings.HasPrefix(cfg.StorageDir, "~") {
//homeDir, err := os.UserHomeDir()
//if err != nil {
// fmt.Printf("Error retrieving home directory: %v\n", err)
// return
//}
cfg.StorageDir = helpers.ExpandHome(cfg.StorageDir)
}
}
3 changes: 2 additions & 1 deletion configs/global_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (

func TestRead_GlobalConfig(t *testing.T) {
os.Setenv("CONFIG_DIR", "../sample-conf/")
os.Setenv("STORAGE_DIR", "../sample-conf/")
cfg := GlobalConfig{}
cfg.ReadConfig()
assert.Equal(t, cfg.StorageDir, "~/tmp/deproxy")
assert.Equal(t, cfg.Cache.TTL, 3600)
//assert.Equal(t, cfg.ConfigDir, "~/tmp/config")
fmt.Println(helpers.ToStringYaml(cfg))
}
2 changes: 1 addition & 1 deletion configs/maven_mirror_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ func TestMavenConfig_MavenMirror(t *testing.T) {
cfg := MavenMirrorConfig{}
cfg.ReadConfig()
//fmt.Println(cfg)
assert.Equal(t, cfg.Path, "~/tmp/mirror/maven")
assert.Equal(t, cfg.Path, "mirror/maven")
fmt.Println(helpers.ToStringYaml(cfg))
}
2 changes: 1 addition & 1 deletion configs/maven_proxy_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestMavenConfig_MavenProxy(t *testing.T) {
cfg := MavenProxyConfig{}
cfg.ReadConfig()
//fmt.Println(cfg)
assert.Equal(t, cfg.Path, "~/tmp/proxy/maven")
assert.Equal(t, cfg.Path, "proxy/maven")
fmt.Println(helpers.ToStringYaml(cfg))
}

Expand Down
3 changes: 2 additions & 1 deletion handlers/proxy/apt_proxy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func AptProxy(c *gin.Context) {
requestPath := c.Param("requestPath")

// fixme di
storageDir := helpers.GetStorageDir()
globalConfig := configs.GlobalConfig{}
globalConfig.ReadConfig()
config := configs.AptProxyConfig{}
Expand All @@ -35,7 +36,7 @@ func AptProxy(c *gin.Context) {
//}

// Create the file
filefullpath := path.Join(globalConfig.StorageDir, config.Path, requestPath)
filefullpath := path.Join(storageDir, config.Path, requestPath)
filename := filepath.Base(filefullpath)
if _, err := os.Stat(filefullpath); os.IsNotExist(err) {
dirpath := filepath.Dir(filefullpath)
Expand Down
4 changes: 3 additions & 1 deletion handlers/proxy/maven_proxy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func MavenProxy(c *gin.Context) {

requestPath := c.Param("path")

// fixme di
storageDir := helpers.GetStorageDir()
globalConfig := configs.GlobalConfig{}
globalConfig.ReadConfig()
config := configs.MavenProxyConfig{}
Expand All @@ -55,7 +57,7 @@ func MavenProxy(c *gin.Context) {

var responseContent []byte
// Create the file
filefullpath := path.Join(globalConfig.StorageDir, config.Path, requestPath)
filefullpath := path.Join(storageDir, config.Path, requestPath)
filename := filepath.Base(filefullpath)
if _, err := os.Stat(filefullpath); os.IsNotExist(err) {
dirpath := filepath.Dir(filefullpath)
Expand Down
6 changes: 4 additions & 2 deletions routers/proxy_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package routers
import (
"deproxy/configs"
dproxy "deproxy/handlers/proxy"
"deproxy/helpers"
"github.com/gin-gonic/gin"
)

func ProxyRouter(r *gin.Engine) {
// fixme di??
storageDir := helpers.GetStorageDir()
globalConfig := configs.GlobalConfig{}
globalConfig.ReadConfig()

Expand All @@ -20,7 +22,7 @@ func ProxyRouter(r *gin.Engine) {
} else {
r.GET("/proxy/maven/*path", func(c *gin.Context) {
c.HTML(200, "alert.html", gin.H{
"ConfigFileDir": globalConfig.StorageDir,
"ConfigFileDir": storageDir,
"ConfigFileName": "maven-proxy.yaml",
})
})
Expand All @@ -35,7 +37,7 @@ func ProxyRouter(r *gin.Engine) {
} else {
r.GET("/proxy/maven/*path", func(c *gin.Context) {
c.HTML(200, "alert.html", gin.H{
"ConfigFileDir": globalConfig.StorageDir,
"ConfigFileDir": storageDir,
"ConfigFileName": "apt-proxy.yaml",
})
})
Expand Down
5 changes: 4 additions & 1 deletion sample-conf/global.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
# http: 192.168.11.99:3128
# https: 192.168.11.99:3129

cache:
ttl: 3600

#if storage_dir set? proxy, mirror relative
storage_dir: ~/tmp/deproxy
#storage_dir: ~/tmp/deproxy
#proxy_root: ~/tmp/proxy
#mirror_root: ~/tmp/mirror

0 comments on commit b2bf910

Please sign in to comment.