Permalink
Browse files

Move static directory to /admin

  • Loading branch information...
dsamarin committed May 21, 2018
1 parent 27d57a3 commit 922c1f227023fc4a5337db66a85e79b51618efcb
Showing with 34 additions and 22 deletions.
  1. +20 −12 admin.go
  2. +4 −2 main.go
  3. +1 −1 templates/footer.tmpl
  4. +1 −1 typescript/Zerodrop.ts
  5. +8 −6 zerodrop.go
@@ -8,6 +8,7 @@ import (
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
"strings"
@@ -27,33 +28,40 @@ type AdminHandler struct {

// NewAdminHandler creates a new admin handler with the specified configuration
// and loads the template files into cache.
func NewAdminHandler(app *ZerodropApp) *AdminHandler {
func NewAdminHandler(app *ZerodropApp) (*AdminHandler, error) {
templateDirectory := "./templates"
staticDirectory := "./static"

handler := &AdminHandler{DB: app.DB, Config: app.Config}

// Load templates
var allFiles []string
files, err := ioutil.ReadDir("./templates")
templateFiles := []string{}
files, err := ioutil.ReadDir(templateDirectory)
if err != nil {
log.Fatal(err)
return nil, err
}
for _, file := range files {
filename := file.Name()
if strings.HasSuffix(filename, ".tmpl") {
allFiles = append(allFiles, "./templates/"+filename)
templateFiles = append(templateFiles,
path.Join(templateDirectory, filename))
}
}

handler.Templates, err = template.ParseFiles(allFiles...)
handler.Templates, err = template.ParseFiles(templateFiles...)
if err != nil {
log.Fatal(err)
return nil, err
}

// Create ServeMux
handler.Mux = http.NewServeMux()
handler.Mux.HandleFunc("/new", handler.ServeNew)
handler.Mux.HandleFunc("/", handler.ServeMain)

return handler
mux := http.NewServeMux()
mux.Handle("/admin/static/",
http.StripPrefix("/admin/static", http.FileServer(http.Dir(staticDirectory))))
mux.HandleFunc("/admin/new", handler.ServeNew)
mux.HandleFunc("/admin/", handler.ServeMain)
handler.Mux = mux

return handler, nil
}

// AdminPageData represents the data served to the admin templates.
@@ -24,11 +24,13 @@ func main() {
configor.Load(&config, configFile)
log.Printf("Loaded configuration: %#v", config)

app := NewZerodropApp(&config)
err := app.Start()
app, err := NewZerodropApp(&config)
if err != nil {
log.Fatal(err)
}
if err := app.Start(); err != nil {
log.Fatal(err)
}

signalChan := make(chan os.Signal)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
@@ -1,6 +1,6 @@
{{define "footer"}}
</div>
<script src="{{$.Config.Base}}static/zerodrop.js"></script>
<script src="{{$.Config.Base}}admin/static/zerodrop.js"></script>
</body>
</html>
{{end}}
@@ -20,8 +20,8 @@ function humanFileSize(bytes: number, si: boolean): string {
return bytes.toFixed(1)+' '+units[u];
}

// Check for a "Generate UUID" button on the page.
$(() => {
// Check for a "Generate UUID" button on the page.
$('.zerodrop-uuid').click((event: JQuery.Event) => {
const element = $(event.currentTarget);
$(element.data('field')).val('xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
@@ -47,14 +47,17 @@ type ZerodropApp struct {
NotFound *NotFoundHandler
}

func NewZerodropApp(config *ZerodropConfig) *ZerodropApp {
app := &ZerodropApp{
func NewZerodropApp(config *ZerodropConfig) (app *ZerodropApp, err error) {
app = &ZerodropApp{
Config: config,
Server: &http.Server{},
DB: &ZerodropDB{},
}

app.AdminHandler = NewAdminHandler(app)
app.AdminHandler, err = NewAdminHandler(app)
if err != nil {
return nil, err
}
app.AuthHandler = &AuthHandler{
Credentials: AuthCredentials{
Digest: config.AuthDigest,
@@ -70,7 +73,7 @@ func NewZerodropApp(config *ZerodropConfig) *ZerodropApp {
app.ShotHandler = NewShotHandler(app)
app.NotFound = &NotFoundHandler{}

return app
return app, nil
}

func (z *ZerodropApp) Start() error {
@@ -88,8 +91,7 @@ func (z *ZerodropApp) Start() error {
}

mux := http.NewServeMux()
mux.Handle("/admin/", http.StripPrefix("/admin", z.AuthHandler))
mux.Handle("/static/", http.StripPrefix("/static", http.FileServer(http.Dir("static"))))
mux.Handle("/admin/", z.AdminHandler)
mux.Handle("/", z.ShotHandler)

z.Server.Handler = mux

0 comments on commit 922c1f2

Please sign in to comment.