github.com/bosssauce/ponzu@v0.11.1-0.20200102001432-9bc41b703131/system/admin/server.go (about)

     1  package admin
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  	"os"
     8  	"path/filepath"
     9  
    10  	"github.com/ponzu-cms/ponzu/system/cfg"
    11  
    12  	"github.com/ponzu-cms/ponzu/system"
    13  	"github.com/ponzu-cms/ponzu/system/admin/user"
    14  	"github.com/ponzu-cms/ponzu/system/api"
    15  	"github.com/ponzu-cms/ponzu/system/db"
    16  )
    17  
    18  // Run adds Handlers to default http listener for Admin
    19  func Run() {
    20  	http.HandleFunc("/admin", user.Auth(adminHandler))
    21  
    22  	http.HandleFunc("/admin/init", initHandler)
    23  
    24  	http.HandleFunc("/admin/login", loginHandler)
    25  	http.HandleFunc("/admin/logout", logoutHandler)
    26  
    27  	http.HandleFunc("/admin/recover", forgotPasswordHandler)
    28  	http.HandleFunc("/admin/recover/key", recoveryKeyHandler)
    29  
    30  	http.HandleFunc("/admin/addons", user.Auth(addonsHandler))
    31  	http.HandleFunc("/admin/addon", user.Auth(addonHandler))
    32  
    33  	http.HandleFunc("/admin/configure", user.Auth(configHandler))
    34  	http.HandleFunc("/admin/configure/users", user.Auth(configUsersHandler))
    35  	http.HandleFunc("/admin/configure/users/edit", user.Auth(configUsersEditHandler))
    36  	http.HandleFunc("/admin/configure/users/delete", user.Auth(configUsersDeleteHandler))
    37  
    38  	http.HandleFunc("/admin/uploads", user.Auth(uploadContentsHandler))
    39  	http.HandleFunc("/admin/uploads/search", user.Auth(uploadSearchHandler))
    40  
    41  	http.HandleFunc("/admin/contents", user.Auth(contentsHandler))
    42  	http.HandleFunc("/admin/contents/search", user.Auth(searchHandler))
    43  	http.HandleFunc("/admin/contents/export", user.Auth(exportHandler))
    44  
    45  	http.HandleFunc("/admin/edit", user.Auth(editHandler))
    46  	http.HandleFunc("/admin/edit/delete", user.Auth(deleteHandler))
    47  	http.HandleFunc("/admin/edit/approve", user.Auth(approveContentHandler))
    48  	http.HandleFunc("/admin/edit/upload", user.Auth(editUploadHandler))
    49  	http.HandleFunc("/admin/edit/upload/delete", user.Auth(deleteUploadHandler))
    50  
    51  	staticDir := cfg.AdminStaticDir()
    52  
    53  	http.Handle("/admin/static/", db.CacheControl(http.StripPrefix("/admin/static", http.FileServer(restrict(http.Dir(staticDir))))))
    54  
    55  	// API path needs to be registered within server package so that it is handled
    56  	// even if the API server is not running. Otherwise, images/files uploaded
    57  	// through the editor will not load within the admin system.
    58  	uploadsDir := cfg.UploadDir()
    59  	http.Handle("/api/uploads/", api.Record(api.CORS(db.CacheControl(http.StripPrefix("/api/uploads/", http.FileServer(restrict(http.Dir(uploadsDir))))))))
    60  
    61  	// Database & uploads backup via HTTP route registered with Basic Auth middleware.
    62  	http.HandleFunc("/admin/backup", system.BasicAuth(backupHandler))
    63  }
    64  
    65  // Docs adds the documentation file server to the server, accessible at
    66  // http://localhost:1234 by default
    67  func Docs(port int) {
    68  	pwd, err := os.Getwd()
    69  	if err != nil {
    70  		log.Fatalln("Couldn't find current directory for file server.")
    71  	}
    72  
    73  	docsDir := filepath.Join(pwd, "docs", "build")
    74  
    75  	addr := fmt.Sprintf(":%d", port)
    76  	url := fmt.Sprintf("http://localhost%s", addr)
    77  
    78  	fmt.Println("")
    79  	fmt.Println("View documentation offline at:", url)
    80  	fmt.Println("")
    81  
    82  	go http.ListenAndServe(addr, http.FileServer(http.Dir(docsDir)))
    83  }