-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
101 lines (88 loc) · 1.98 KB
/
Copy pathmain.go
File metadata and controls
101 lines (88 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"io"
"net/http"
"pypi/config"
"pypi/storage"
"pypi/storage/filesystem"
"html/template"
"github.com/gin-gonic/gin"
)
const indexHTML = `
<!DOCTYPE html>
<html>
<body>
{{range .}}
<a href="{{.Url}}">{{.Name}}</a><br>
{{end}}
</body>
</html>
`
var indexTemplate = template.Must(template.New("index").Parse(indexHTML))
func main() {
var store storage.Storage
if config.StorageType == "filesystem" {
store = filesystem.NewStorageFs()
} else {
panic("other storage types not supported yet")
}
r := gin.Default()
r.SetHTMLTemplate(indexTemplate)
r.GET("/simple/", func(c *gin.Context) {
// Get link index
links, err := store.GetIndex()
if err != nil {
c.String(500, "Error getting links")
return
}
c.HTML(200, "index", links)
})
r.GET("/simple/:package/", func(c *gin.Context) {
pkg := c.Param("package")
// Get package links
links, err := store.GetPackageLinks(pkg)
if err != 200 {
c.String(err, http.StatusText(err))
return
}
c.HTML(200, "index", links)
})
r.GET("/simple/:package/:hash/:filename/", func(c *gin.Context) {
packageName := c.Param("package")
filename := c.Param("filename")
hash := c.Param("hash")
file, err := store.GetFile(packageName, filename, hash)
if err != 200 {
c.String(err, http.StatusText(err))
return
}
c.Data(200, "application/octet-stream", file)
})
r.POST("/", func(c *gin.Context) {
file, err := c.FormFile("content")
if err != nil {
c.String(500, "Error getting file")
return
}
fileContent, err := file.Open()
if err != nil {
c.String(500, "Error opening file")
return
}
fileData, err := io.ReadAll(fileContent)
if err != nil {
c.String(500, "Error reading file")
return
}
sha256Digest := c.PostForm("sha256_digest")
packageName := c.PostForm("name")
// Save file
err = store.PutFile(packageName,file.Filename, fileData, sha256Digest)
if err != nil {
c.String(500, err.Error())
return
}
c.String(200, "OK")
})
r.Run()
}