Skip to content

Instantly share code, notes, and snippets.

@tosone
Created March 20, 2022 02:36
Show Gist options
  • Save tosone/46a78308b57b2f6ed3768efe1cafa94e to your computer and use it in GitHub Desktop.
Save tosone/46a78308b57b2f6ed3768efe1cafa94e to your computer and use it in GitHub Desktop.
maven server tutorial
package main
import (
"crypto/sha1"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
)
func main() {
var metadata = `<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>com.neo</groupId>
<artifactId>hello</artifactId>
<versioning>
<release>1.2</release>
<versions>
<version>1.2</version>
</versions>
<lastUpdated>20220319104041</lastUpdated>
</versioning>
</metadata>`
r := gin.Default()
// r.GET("/ping", func(c *gin.Context) {
// c.JSON(200, gin.H{
// "message": "pong",
// })
// })
r.Any("/repository/maven-releases/*path", func(ctx *gin.Context) {
var path = ctx.Request.URL.Path
if strings.HasSuffix(path, ".jar") {
var file, _ = os.Create("./test.jar")
io.Copy(file, ctx.Request.Body)
} else if strings.HasSuffix(path, ".jar.md5") {
var file, _ = os.Create("./test.jar.md5")
io.Copy(file, ctx.Request.Body)
} else if strings.HasSuffix(path, ".jar.sha1") {
var file, _ = os.Create("./test.jar.sha1")
io.Copy(file, ctx.Request.Body)
} else if strings.HasSuffix(path, ".pom") {
var file, _ = os.Create("./test.pom")
io.Copy(file, ctx.Request.Body)
} else if strings.HasSuffix(path, ".pom.md5") {
var file, _ = os.Create("./test.pom.md5")
io.Copy(file, ctx.Request.Body)
} else if strings.HasSuffix(path, ".pom.sha1") {
var file, _ = os.Create("./test.pom.sha1")
io.Copy(file, ctx.Request.Body)
} else if strings.HasSuffix(path, "maven-metadata.xml") {
if ctx.Request.Method == http.MethodGet {
ctx.Data(200, "text/xml", []byte(metadata))
} else if ctx.Request.Method == http.MethodPut {
var data, _ = ioutil.ReadAll(ctx.Request.Body)
metadata = string(data)
fmt.Printf("update metada: %s\n", metadata)
}
} else if strings.HasSuffix(path, "maven-metadata.xml.sha1") && ctx.Request.Method == http.MethodGet {
var hash = sha1.New()
hash.Write([]byte(metadata))
ctx.Data(200, "text/plain", []byte(fmt.Sprintf("%x", hash.Sum(nil))))
} else if strings.HasSuffix(path, "maven-metadata.xml.md5") && ctx.Request.Method == http.MethodPut {
var data, _ = ioutil.ReadAll(ctx.Request.Body)
fmt.Printf("maven-metadata.xml.md5: %s\n", data)
} else if strings.HasSuffix(path, "maven-metadata.xml.sha1") && ctx.Request.Method == http.MethodPut {
var data, _ = ioutil.ReadAll(ctx.Request.Body)
fmt.Printf("maven-metadata.xml.sha1: %s\n", data)
} else {
ctx.Data(200, "text/plain", []byte("ok"))
}
})
r.Run(":8081")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment