2015-07-22 19:41:13 +00:00
|
|
|
package main
|
|
|
|
|
2015-07-23 15:05:21 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// {{{ Package Struct
|
|
|
|
type Package struct {
|
|
|
|
Repo string
|
|
|
|
Path string
|
|
|
|
Packages []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p Package) GetRoutes() (ret []string) {
|
|
|
|
ret = append(ret, p.Path)
|
|
|
|
for _, pkg := range p.Packages {
|
|
|
|
ret = append(ret, p.Path+"/"+pkg)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
|
|
|
|
// {{{ JSON Functions
|
|
|
|
|
|
|
|
func writePage(w http.ResponseWriter, data string, code int) error {
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
|
|
|
|
w.WriteHeader(code)
|
|
|
|
w.Write([]byte(data))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func writePageError(w http.ResponseWriter, message string, code int) error {
|
|
|
|
return writePage(w, message, code)
|
|
|
|
}
|
|
|
|
|
2015-07-23 16:07:47 +00:00
|
|
|
func writePageGo(w http.ResponseWriter, path string, pkg Package, code int) error {
|
2015-07-23 15:05:21 +00:00
|
|
|
return writePage(w, fmt.Sprintf(`<!DOCTYPE html><html>
|
|
|
|
<head><meta charset="utf-8"><title>%s</title><meta name="go-import" content="%s %s %s"></head>
|
|
|
|
<body>%s</body>
|
|
|
|
</html>`, "Package!", path, "git", pkg.Repo, "Package!"), code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
|
|
|
|
// {{{ Config Functions
|
|
|
|
|
|
|
|
func loadConfig() (ret []Package, err error) {
|
|
|
|
content, err := ioutil.ReadFile("packages.json")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(content, &ret)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-07-23 16:07:47 +00:00
|
|
|
func loadRoutes(packages []Package) (routes map[string]Package) {
|
|
|
|
routes = map[string]Package{}
|
2015-07-23 15:05:21 +00:00
|
|
|
|
|
|
|
for _, pkg := range packages {
|
|
|
|
for _, route := range pkg.GetRoutes() {
|
2015-07-23 16:04:54 +00:00
|
|
|
routes[route] = pkg
|
2015-07-23 15:05:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
|
2015-07-22 19:41:13 +00:00
|
|
|
func main() {
|
2015-07-23 15:05:21 +00:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
packages, err := loadConfig()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Errorf("%s\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
routes := loadRoutes(packages)
|
|
|
|
|
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
route := req.URL.Path
|
|
|
|
if val, ok := routes[route]; ok {
|
2015-07-28 16:27:34 +00:00
|
|
|
writePageGo(w, "pault.ag"+val.Path, val, 200)
|
2015-07-23 15:05:21 +00:00
|
|
|
} else {
|
2015-07-28 16:13:16 +00:00
|
|
|
writePageError(w, ":(", 404)
|
2015-07-23 15:05:21 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
http.ListenAndServe(":8000", mux)
|
2015-07-22 19:41:13 +00:00
|
|
|
}
|
2015-07-23 15:05:21 +00:00
|
|
|
|
|
|
|
// vim: foldmethod=marker
|