package main
import (
"io"
"net/http"
)const (
REPO = "repo" // repo where the file is located
ID = "id" // a sha in Git, nodeid in Mercurial, etc.
)// Sends a response containing the file blob, if it exists// Currently uses request params - to change to path params?// i.e. /file/repo/id instead of// /file?repo=repo1&id=sha1shafunc GetFile(w http.ResponseWriter, r *http.Request) {
repoName := r.URL.Query().Get(REPO)
id := r.URL.Query().Get(ID)
if len(repoName) != 0 && len(id) != 0 {
//GetRepository(repoName).GetFile(id)
repo := GetRepository(repoName)
w.Header().Set("Content-Type", "application/octet-stream")
w.Write(repo.GetFile(id))
}
}// Sends a true or false response depending on whether the file exists// Currently uses request params - to change to path params?// i.e. /file/repo/name/id instead of// /file?repo=repo1&id=sha1func FileExists(w http.ResponseWriter, r *http.Request
Web Service
| Repository | |
|---|---|
| 1 | package main |
| 2 | |
| 3 | // Base Repository "class" |
| 4 | |
| 5 | type RepositoryInfo struct { |
| 6 | Name string |
| 7 | Path string |
| 8 | } |
| 9 | |
| 10 | type Repository interface { |
| 11 | GetName() string |
| 12 | GetPath() string |
| 13 | GetFile(id string) []byte |
| 14 | GetFileExists(id string) bool |
| 15 | } |