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