root = flag.String("root", "./packages", "Path to packages directory")
bind = flag.String("bind", "[::]:8080", "Address to bind to")
tlsCert = flag.String("tls-cert", "", "Path to TLS X.509 certificate")
root = flag.String("root", "./packages", "Path to packages directory")
bind = flag.String("bind", "[::]:8080", "Address to bind to")
tlsCert = flag.String("tls-cert", "", "Path to TLS X.509 certificate")
version = flag.Bool("version", false, "Print version information")
warranty = flag.Bool("warranty", false, "Print warranty information")
version = flag.Bool("version", false, "Print version information")
warranty = flag.Bool("warranty", false, "Print warranty information")
func mkdirForPkg(w http.ResponseWriter, r *http.Request, dir string) bool {
path := filepath.Join(*root, dir)
if _, err := os.Stat(path); os.IsNotExist(err) {
func mkdirForPkg(w http.ResponseWriter, r *http.Request, dir string) bool {
path := filepath.Join(*root, dir)
if _, err := os.Stat(path); os.IsNotExist(err) {
func refreshDir(w http.ResponseWriter, r *http.Request, dir, filenameGet string) bool {
if _, err := os.Stat(filepath.Join(*root, dir, InternalFlag)); err == nil {
func refreshDir(w http.ResponseWriter, r *http.Request, dir, filenameGet string) bool {
if _, err := os.Stat(filepath.Join(*root, dir, InternalFlag)); err == nil {
resp, err := http.Get(*pypiURL + dir + "/")
if err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
return false
}
resp, err := http.Get(*pypiURL + dir + "/")
if err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
return false
}
log.Println(r.RemoteAddr, "pypi download", filename)
path = filepath.Join(*root, dir, filename)
resp, err = http.Get(uri)
log.Println(r.RemoteAddr, "pypi download", filename)
path = filepath.Join(*root, dir, filename)
resp, err = http.Get(uri)
- } else {
- if !os.IsNotExist(err) {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return false
- }
+ }
+ if !os.IsNotExist(err) {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return false
}
log.Println(r.RemoteAddr, "pypi touch", filename)
if err = ioutil.WriteFile(path, digest, os.FileMode(0666)); err != nil {
}
log.Println(r.RemoteAddr, "pypi touch", filename)
if err = ioutil.WriteFile(path, digest, os.FileMode(0666)); err != nil {
files, err := ioutil.ReadDir(*root)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
files, err := ioutil.ReadDir(*root)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
- w.Write([]byte(fmt.Sprintf(HTMLBegin, "root", "root")))
+ var result bytes.Buffer
+ result.WriteString(fmt.Sprintf(HTMLBegin, "root", "root"))
}
func listDir(w http.ResponseWriter, r *http.Request, dir string, autorefresh bool) {
}
func listDir(w http.ResponseWriter, r *http.Request, dir string, autorefresh bool) {
dirPath := filepath.Join(*root, dir)
if autorefresh {
if !refreshDir(w, r, dir, "") {
dirPath := filepath.Join(*root, dir)
if autorefresh {
if !refreshDir(w, r, dir, "") {
- w.Write([]byte(fmt.Sprintf(HTMLBegin, dir, dir)))
+ var result bytes.Buffer
+ result.WriteString(fmt.Sprintf(HTMLBegin, dir, dir))
var data []byte
var filenameClean string
for _, file := range files {
if !strings.HasSuffix(file.Name(), SHA256Ext) {
continue
}
var data []byte
var filenameClean string
for _, file := range files {
if !strings.HasSuffix(file.Name(), SHA256Ext) {
continue
}
data, err = ioutil.ReadFile(filepath.Join(dirPath, file.Name()))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
filenameClean = strings.TrimSuffix(file.Name(), SHA256Ext)
data, err = ioutil.ReadFile(filepath.Join(dirPath, file.Name()))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
filenameClean = strings.TrimSuffix(file.Name(), SHA256Ext)
HTMLElement,
strings.Join([]string{
*refreshURLPath, dir, "/",
HTMLElement,
strings.Join([]string{
*refreshURLPath, dir, "/",
- filenameClean, "#", SHA256Prefix, string(data),
+ filenameClean, "#", SHA256Prefix, hex.EncodeToString(data),
}
func servePkg(w http.ResponseWriter, r *http.Request, dir, filename string) {
}
func servePkg(w http.ResponseWriter, r *http.Request, dir, filename string) {
path := filepath.Join(*root, dir, filename)
if _, err := os.Stat(path); os.IsNotExist(err) {
if !refreshDir(w, r, dir, filename) {
path := filepath.Join(*root, dir, filename)
if _, err := os.Stat(path); os.IsNotExist(err) {
if !refreshDir(w, r, dir, filename) {
}
for _, file := range r.MultipartForm.File["content"] {
filename := file.Filename
}
for _, file := range r.MultipartForm.File["content"] {
filename := file.Filename
- log.Println(r.RemoteAddr, "upload", filename, "by", username)
+ log.Println(r.RemoteAddr, "put", filename, "by", username)
dir := filename[:strings.LastIndex(filename, "-")]
dirPath := filepath.Join(*root, dir)
path := filepath.Join(dirPath, filename)
dir := filename[:strings.LastIndex(filename, "-")]
dirPath := filepath.Join(*root, dir)
path := filepath.Join(dirPath, filename)
} else {
servePkg(w, r, parts[0], parts[1])
}
} else {
servePkg(w, r, parts[0], parts[1])
}
needsRefreshPasswd := make(chan os.Signal, 0)
needsShutdown := make(chan os.Signal, 0)
needsRefreshPasswd := make(chan os.Signal, 0)
needsShutdown := make(chan os.Signal, 0)
signal.Notify(needsRefreshPasswd, syscall.SIGHUP)
signal.Notify(needsShutdown, syscall.SIGTERM, syscall.SIGINT)
go func() {
signal.Notify(needsRefreshPasswd, syscall.SIGHUP)
signal.Notify(needsShutdown, syscall.SIGTERM, syscall.SIGINT)
go func() {
log.Println("Shutting down")
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
log.Println("Shutting down")
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)