github.com/bhojpur/cache@v0.0.4/pkg/debugger/handler.go (about)

     1  package debugger
     2  
     3  // Copyright (c) 2018 Bhojpur Consulting Private Limited, India. All rights reserved.
     4  
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy
     6  // of this software and associated documentation files (the "Software"), to deal
     7  // in the Software without restriction, including without limitation the rights
     8  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  // copies of the Software, and to permit persons to whom the Software is
    10  // furnished to do so, subject to the following conditions:
    11  
    12  // The above copyright notice and this permission notice shall be included in
    13  // all copies or substantial portions of the Software.
    14  
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    21  // THE SOFTWARE.
    22  
    23  import (
    24  	"net/http"
    25  	"strconv"
    26  	"strings"
    27  
    28  	memcache "github.com/bhojpur/cache/pkg/memory"
    29  	templates "github.com/bhojpur/cache/templates"
    30  )
    31  
    32  // NewHandler returns a new root HTTP handler.
    33  func NewHandler(db *memcache.DB) http.Handler {
    34  	h := &handler{db}
    35  	mux := http.NewServeMux()
    36  	mux.HandleFunc("/", h.index)
    37  	mux.HandleFunc("/page", h.page)
    38  	return mux
    39  }
    40  
    41  type handler struct {
    42  	db *memcache.DB
    43  }
    44  
    45  func (h *handler) index(w http.ResponseWriter, r *http.Request) {
    46  	templates.Index(w)
    47  }
    48  
    49  func (h *handler) page(w http.ResponseWriter, r *http.Request) {
    50  	err := h.db.View(func(tx *memcache.Tx) error {
    51  		showUsage := (r.FormValue("usage") == "true")
    52  
    53  		// Use the direct page id, if available.
    54  		if r.FormValue("id") != "" {
    55  			id, _ := strconv.Atoi(r.FormValue("id"))
    56  			return templates.Page(w, r, tx, nil, id, showUsage)
    57  		}
    58  
    59  		// Otherwise extract the indexes and traverse.
    60  		indexes, err := indexes(r)
    61  		if err != nil {
    62  			return err
    63  		}
    64  
    65  		return templates.Page(w, r, tx, indexes, 0, showUsage)
    66  	})
    67  	if err != nil {
    68  		templates.Error(w, err)
    69  	}
    70  }
    71  
    72  // parses and returns all indexes from a request.
    73  func indexes(r *http.Request) ([]int, error) {
    74  	var a = []int{0}
    75  	if len(r.FormValue("index")) > 0 {
    76  		for _, s := range strings.Split(r.FormValue("index"), ":") {
    77  			i, err := strconv.Atoi(s)
    78  			if err != nil {
    79  				return nil, err
    80  			}
    81  			a = append(a, i)
    82  		}
    83  	}
    84  	return a, nil
    85  }