package ctrl import ( "crypto/md5" "encoding/base32" . "github.com/mickael-kerjean/nuage/server/common" "io" "text/template" "mime" "net/http" "os" "path" "path/filepath" "strings" ) func StaticHandler(_path string, ctx App) http.Handler { return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { header := res.Header() header.Set("Content-Type", mime.TypeByExtension(filepath.Ext(req.URL.Path))) header.Set("Cache-Control", "max-age=2592000") SecureHeader(&header) if strings.HasSuffix(req.URL.Path, "/") { http.NotFound(res, req) return } absPath := GetAbsolutePath(_path) fsrv := http.FileServer(http.Dir(absPath)) _, err := os.Open(path.Join(absPath, req.URL.Path+".gz")) if err == nil && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") { res.Header().Set("Content-Encoding", "gzip") req.URL.Path += ".gz" } fsrv.ServeHTTP(res, req) }) } func DefaultHandler(_path string, ctx App) http.Handler { return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { header := res.Header() header.Set("Content-Type", "text/html") header.Set("Cache-Control", "no-cache, no-store, must-revalidate") SecureHeader(&header) // Redirect to the admin section on first boot to setup the stuff if req.URL.String() != URL_SETUP && Config.Get("auth.admin").String() == "" { http.Redirect(res, req, URL_SETUP, 307) return } p := _path if _, err := os.Open(path.Join(GetCurrentDir(), p+".gz")); err == nil && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") { res.Header().Set("Content-Encoding", "gzip") p += ".gz" } http.ServeFile(res, req, GetAbsolutePath(p)) }) } func AboutHandler(ctx App) http.Handler { return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { header := res.Header() header.Set("Content-Type", "text/html") SecureHeader(&header) hash := func(path string) string { f, err := os.OpenFile(path, os.O_RDONLY, os.ModePerm) if err != nil { return "__" } defer f.Close() h := md5.New() if _, err := io.Copy(h, f); err != nil { return "__" } return base32.HexEncoding.EncodeToString(h.Sum(nil))[:6] } page := `
{{range .Plugins}}
{{ index . 0 }} ({{ index . 1 }})
{{end}}