2020-09-12 15:23:25 +00:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.jolheiser.com/vanity/flags"
|
|
|
|
"go.jolheiser.com/vanity/service"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/go-chi/chi/middleware"
|
|
|
|
"go.jolheiser.com/beaver"
|
|
|
|
)
|
|
|
|
|
2021-02-21 20:23:06 +00:00
|
|
|
var tmpl *template.Template
|
|
|
|
|
|
|
|
func Init() (*chi.Mux, error) {
|
|
|
|
var err error
|
|
|
|
tmpl, err = Templates()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-12 15:23:25 +00:00
|
|
|
|
|
|
|
r := chi.NewRouter()
|
|
|
|
r.Use(middleware.RedirectSlashes)
|
|
|
|
r.Use(middleware.Recoverer)
|
2021-02-21 20:23:06 +00:00
|
|
|
r.Use(middleware.Timeout(60 * time.Second))
|
2020-09-12 15:23:25 +00:00
|
|
|
|
|
|
|
r.Get("/", doIndex)
|
|
|
|
r.Get("/*", doVanity)
|
2021-02-21 20:23:06 +00:00
|
|
|
r.Mount("/_", apiRoutes())
|
2020-09-12 15:23:25 +00:00
|
|
|
|
|
|
|
svc = service.New()
|
|
|
|
|
|
|
|
beaver.Info("Warming up cache...")
|
2021-02-21 20:23:06 +00:00
|
|
|
updateCache()
|
|
|
|
beaver.Infof("Finished warming up cache: %s", cache.Packages.Names())
|
2020-09-12 15:23:25 +00:00
|
|
|
go cronStart()
|
|
|
|
|
|
|
|
beaver.Infof("Running vanity server at http://localhost:%d", flags.Port)
|
2021-02-21 20:23:06 +00:00
|
|
|
return r, nil
|
2020-09-12 15:23:25 +00:00
|
|
|
}
|
|
|
|
|
2021-02-21 20:23:06 +00:00
|
|
|
func doIndex(res http.ResponseWriter, req *http.Request) {
|
|
|
|
format := "list"
|
|
|
|
if flags.Topics {
|
|
|
|
format = "topics"
|
|
|
|
}
|
|
|
|
q := req.URL.Query().Get("format")
|
|
|
|
if q != "" {
|
|
|
|
format = q
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Lookup("index.tmpl").Execute(res, map[string]interface{}{
|
|
|
|
"Packages": cache.Packages,
|
|
|
|
"Index": true,
|
|
|
|
"Format": format,
|
2020-09-12 15:23:25 +00:00
|
|
|
}); err != nil {
|
2021-02-21 20:23:06 +00:00
|
|
|
beaver.Errorf("could not write response: %v", err)
|
2020-09-12 15:23:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func doVanity(res http.ResponseWriter, req *http.Request) {
|
|
|
|
key := chi.URLParam(req, "*")
|
2021-02-21 20:23:06 +00:00
|
|
|
pkg, ok := cache.Packages[strings.Split(key, "/")[0]]
|
2020-09-12 15:23:25 +00:00
|
|
|
if !ok {
|
|
|
|
http.NotFound(res, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-21 20:23:06 +00:00
|
|
|
ctx := map[string]interface{}{
|
2020-09-12 15:23:25 +00:00
|
|
|
"Package": pkg,
|
|
|
|
"Module": pkg.Module(flags.Domain),
|
|
|
|
"GoSource": fmt.Sprintf("%s %s %s %s", pkg.Module(flags.Domain), pkg.CloneHTTP, svc.GoDir(pkg), svc.GoFile(pkg)),
|
2021-02-21 20:23:06 +00:00
|
|
|
"Index": false,
|
|
|
|
}
|
|
|
|
|
|
|
|
q := req.URL.Query()
|
|
|
|
if q.Get("go-get") != "" || q.Get("git-import") != "" {
|
|
|
|
if err := tmpl.Lookup("import.tmpl").Execute(res, ctx); err != nil {
|
|
|
|
beaver.Errorf("could not write response: %v", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Lookup("vanity.tmpl").Execute(res, ctx); err != nil {
|
|
|
|
beaver.Errorf("could not write response: %v", err)
|
2020-09-12 15:23:25 +00:00
|
|
|
}
|
|
|
|
}
|