proxy repo files
This commit is contained in:
parent
62e9b3a220
commit
e769bcd3e1
38
handler.go
38
handler.go
@ -1,8 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
url2 "net/url"
|
||||||
"path"
|
"path"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func modulesHandler(w http.ResponseWriter, r *http.Request) {
|
func modulesHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -19,14 +25,44 @@ func modulesHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
m, ok := modules.Find(path.Base(r.URL.Path))
|
mPath := strings.Split(r.URL.Path, "/")[1]
|
||||||
|
m, ok := modules.Find(path.Base(mPath))
|
||||||
if !ok {
|
if !ok {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if parts := strings.Split(r.URL.Path, "/"); len(parts) > 2 && parts[2] != ""{
|
||||||
|
url, err := url2.ParseRequestURI(m.Readme)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Errorf("parse readme url: %v", err)
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
baseParts := strings.Split(url.Path, "/")
|
||||||
|
url.Path = strings.Join(baseParts[:len(baseParts)-1], "/") + "/" + strings.Join(parts[2:], "/")
|
||||||
|
res, err := http.Get(url.String())
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(res.StatusCode)
|
||||||
|
copy(w, res)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !strings.HasSuffix(r.URL.Path, "/") {
|
||||||
|
http.Redirect(w, r, r.URL.Path+"/", http.StatusSeeOther)
|
||||||
|
return
|
||||||
|
}
|
||||||
w.Header().Set("content-type", "text/html")
|
w.Header().Set("content-type", "text/html")
|
||||||
if err := moduleTemplate.Execute(w, m); err != nil {
|
if err := moduleTemplate.Execute(w, m); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func copy(w http.ResponseWriter, res *http.Response) {
|
||||||
|
defer res.Body.Close()
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
io.Copy(buf, res.Body)
|
||||||
|
w.Write(buf.Bytes())
|
||||||
|
}
|
||||||
|
@ -70,6 +70,9 @@ func NewModules(path string) (Modules, error) {
|
|||||||
type Modules []*Module
|
type Modules []*Module
|
||||||
|
|
||||||
func (m *Modules) Find(name string) (*Module, bool) {
|
func (m *Modules) Find(name string) (*Module, bool) {
|
||||||
|
if parts := strings.Split(name, "/"); len(parts) > 1 {
|
||||||
|
name = parts[0]
|
||||||
|
}
|
||||||
for _, v := range *m {
|
for _, v := range *m {
|
||||||
mod := path.Base(v.Import)
|
mod := path.Base(v.Import)
|
||||||
if mod == name {
|
if mod == name {
|
||||||
|
Loading…
Reference in New Issue
Block a user