2015-07-22 19:41:13 +00:00
|
|
|
package main
|
|
|
|
|
2015-07-23 15:05:21 +00:00
|
|
|
import (
|
2015-08-08 16:01:44 +00:00
|
|
|
"bytes"
|
2015-07-23 15:05:21 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-08-08 16:01:44 +00:00
|
|
|
"html/template"
|
2015-07-23 15:05:21 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// {{{ Package Struct
|
|
|
|
type Package struct {
|
|
|
|
Repo string
|
|
|
|
Path string
|
|
|
|
Packages []string
|
2015-08-08 16:01:44 +00:00
|
|
|
Url string
|
2015-07-23 15:05:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-08-08 16:01:44 +00:00
|
|
|
t, err := template.ParseFiles("template.html")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pkg.Path = path
|
|
|
|
var page bytes.Buffer
|
|
|
|
err = t.Execute(&page, pkg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return writePage(w, page.String(), code)
|
2015-07-23 15:05:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
|
|
|
|
// {{{ 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
|
2015-08-08 16:01:44 +00:00
|
|
|
var err error
|
2015-07-23 15:05:21 +00:00
|
|
|
if val, ok := routes[route]; ok {
|
2015-08-08 16:01:44 +00:00
|
|
|
err = writePageGo(w, "pault.ag"+val.Path, val, 200)
|
2015-07-23 15:05:21 +00:00
|
|
|
} else {
|
2015-08-08 16:01:44 +00:00
|
|
|
err = writePageError(w, ":(", 404)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
writePageError(w, fmt.Sprintf("error: %v", err), 400)
|
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
|