Skip to content

Commit

Permalink
download maven jar
Browse files Browse the repository at this point in the history
  • Loading branch information
archmagece committed Feb 24, 2021
1 parent 0255a80 commit b6e7570
Show file tree
Hide file tree
Showing 18 changed files with 276 additions and 224 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@
* apt
* vagrant box
* git

## Repo

* https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-milestone
24 changes: 12 additions & 12 deletions config/maven-proxy-public.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
maven:
server:
# https://docs.gradle.org/current/userguide/declaring_repositories.html
centeral:
id: maven-center
Expand All @@ -8,17 +8,17 @@ maven:
jcener:
url: https://jcenter.bintray.com
description: "jcenter"
google:
url: https://maven.google.com
description: "google android"
# https://repo.spring.io/webapp/#/home
spring_release:
name: repo.spring.io-releases
url: https://repo.spring.io/release
spring_snapshorts:
id: spring-snapshots
name: repo.spring.io-snapshots
url: https://repo.spring.io/snapshot
# google:
# url: https://maven.google.com
# description: "google android"
# # https://repo.spring.io/webapp/#/home
# spring_release:
# name: repo.spring.io-releases
# url: https://repo.spring.io/release
# spring_snapshorts:
# id: spring-snapshots
# name: repo.spring.io-snapshots
# url: https://repo.spring.io/snapshot

# company_internal:
# url: http://company
Expand Down
42 changes: 42 additions & 0 deletions config/maven_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package config

import (
"fmt"
"gopkg.in/yaml.v3"
"io/ioutil"
"log"
)

type MavenServer struct {
Id string `yaml:"id"`
Name string `yaml:"name"`
Url string `yaml:"url"`
Description string `yaml:"description"`
}

type MavenConfig struct {
Server map[string]MavenServer `yaml:"server"`
}

type DownloadService struct {
MavenConfig
}

func (ds *DownloadService) DownloadProxy(path string) (MavenConfig, error) {
// cache - no expire
yamlFile, err := ioutil.ReadFile("config/maven-proxy-public.yaml")
if err != nil {
log.Printf("read config err #%v ", err)
}
fmt.Println(yamlFile)
mavenConfig := MavenConfig{
Server: map[string]MavenServer{},
}
err = yaml.Unmarshal(yamlFile, mavenConfig)
fmt.Println(mavenConfig)
if err != nil {
log.Fatalf("Unmarshal: %v", err)
}
fmt.Printf("config %s \n", mavenConfig)
return mavenConfig, nil
}
65 changes: 65 additions & 0 deletions config/maven_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package config

import (
"fmt"
"gopkg.in/yaml.v3"
"io/ioutil"
"log"
"os"
"testing"
)

func TestYamlMake(t *testing.T) {
mavenConfig := MavenConfig{}
mavenConfig.Server = map[string]MavenServer{
"centeral": {
Id: "maven-center",
Name: "Center",
Url: "https://repo.maven.com",
Description: "desc1",
},
"jcenter": {
Id: "jcenter-center",
Name: "JCenter",
Url: "https://repo.jmaven.com",
Description: "desc2",
},
"google": {
Url: "https://repo.jmaven.com",
Description: "desc2",
},
"spring-release": {
Name: "springrelease",
Url: "https://repo.jmaven.com",
},
}
yamlFile, err := yaml.Marshal(mavenConfig)
if err != nil {
fmt.Println("errrrr write")
}
ioutil.WriteFile("../config/maven-tmp.yaml", yamlFile, os.ModeAppend)
fmt.Println(string(yamlFile))

mavenConfig2 := MavenConfig{
Server: map[string]MavenServer{},
}
err = yaml.Unmarshal(yamlFile, mavenConfig2)
fmt.Println(mavenConfig2)
}

func TestYaml(t *testing.T) {

yamlFile, err := ioutil.ReadFile("../config/maven-proxy-public.yaml")
if err != nil {
log.Printf("yamlFile.Get err #%v ", err)
}
fmt.Println(string(yamlFile))
mavenConfig := MavenConfig{
Server: map[string]MavenServer{},
}
err = yaml.Unmarshal(yamlFile, mavenConfig)
if err != nil {
log.Fatalf("Unmarshal: %v", err)
}

}
27 changes: 0 additions & 27 deletions controllers/api/v1/user.go

This file was deleted.

27 changes: 0 additions & 27 deletions controllers/api/v2/user.go

This file was deleted.

66 changes: 66 additions & 0 deletions controllers/proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package controllers

import (
u "dohoarding/apiHelpers"
config2 "dohoarding/config"
"dohoarding/utils"
"fmt"
"github.com/gin-gonic/gin"
"io"
"log"
"net/http"
"os"
"path"
"path/filepath"
)

func Maven(c *gin.Context) {
log.Printf("Access proxy maven\n")
var downloadService config2.DownloadService

requestPath := c.Param("path")
config, err := downloadService.DownloadProxy(requestPath)
if err != nil {
u.Respond(c.Writer, u.Message(1, "Invalid request"))
return
}

// Create the file
dirpath := filepath.Dir(path.Join("d:/tmp/cachedir" + requestPath))
filename := filepath.Base(requestPath)
os.MkdirAll(dirpath, os.ModePerm)
out, err := os.Create(path.Join("d:/tmp/cachedir" + requestPath))
//out, err := os.Create("d:/tmp/cachedir/aaa/bbb")
if err != nil {
panic(err)
return
}
defer out.Close()

// Get the data
fmt.Println(len(config.Server))
for s, server := range config.Server {
fmt.Printf("for moon %s\n", s)
resp, err := http.Get(utils.JoinURL(server.Url, requestPath))
if err != nil {
log.Fatal(err)
return
}
//fmt.Println(resp.Header)
fmt.Println(resp.StatusCode)
// Writer the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
log.Fatal(err)
return
}
resp.Body.Close()
break
}

c.Header("Content-Description", "File Transfer")
c.Header("Content-Transfer-Encoding", "binary")
c.Header("Content-Disposition", "attachment; filename="+filename)
c.Header("Content-Type", "application/octet-stream")
c.File("d:/tmp/cachedir" + requestPath)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ require (
github.com/jinzhu/gorm v1.9.16
github.com/joho/godotenv v1.3.0
gopkg.in/testfixtures.v2 v2.6.0
gopkg.in/yaml.v2 v2.2.8
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,5 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import (

//Execution starts from main function
func main() {

e := godotenv.Load()
if e != nil {
fmt.Print(e)
}
r := Routers.SetupRouter()
r := routers.SetupRouter()

port := os.Getenv("port")

Expand All @@ -34,5 +33,4 @@ func main() {
}

r.Run(":" + port)

}
9 changes: 9 additions & 0 deletions middlewares/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ func UserMiddlewares() gin.HandlerFunc {
c.Next()
}
}

func ProxyMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {

//Code for middlewares

c.Next()
}
}
58 changes: 0 additions & 58 deletions models/base.go

This file was deleted.

13 changes: 0 additions & 13 deletions models/user.go

This file was deleted.

Loading

0 comments on commit b6e7570

Please sign in to comment.