github.com/aarzilli/tools@v0.0.0-20151123112009-0d27094f75e0/os/fsi/webapi/register_handlers.go (about)

     1  package webapi
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"net/http"
     8  
     9  	"github.com/pbberlin/tools/net/http/htmlfrag"
    10  	"github.com/pbberlin/tools/net/http/loghttp"
    11  	"github.com/pbberlin/tools/net/http/tplx"
    12  	"github.com/pbberlin/tools/os/fsi/dsfs"
    13  )
    14  
    15  var wpf func(w io.Writer, format string, a ...interface{}) (int, error) = fmt.Fprintf
    16  
    17  const UriSetFSType = "/fsi/set-fs-type"
    18  const UriDeleteSubtree = "/fsi/delete-subtree"
    19  
    20  func InitHandlers() {
    21  	http.HandleFunc(UriSetFSType, loghttp.Adapter(setFSType))
    22  	http.HandleFunc("/fsi/create-objects", loghttp.Adapter(createSys))
    23  	http.HandleFunc("/fsi/retrieve-by-query", loghttp.Adapter(retrieveByQuery))
    24  	http.HandleFunc("/fsi/retrieve-by-read-dir", loghttp.Adapter(retrieveByReadDir))
    25  	http.HandleFunc("/fsi/walk", loghttp.Adapter(walkH))
    26  	http.HandleFunc("/fsi/remove", loghttp.Adapter(removeSubtree))
    27  
    28  	http.HandleFunc("/fsi/delete-all", loghttp.Adapter(deleteAll))
    29  	http.HandleFunc(UriDeleteSubtree, loghttp.Adapter(DeleteSubtree))
    30  
    31  	http.HandleFunc("/fsi/cntr/last", loghttp.Adapter(lastMountPoint))
    32  	http.HandleFunc("/fsi/cntr/reset", loghttp.Adapter(resetMountPoint))
    33  	http.HandleFunc("/fsi/cntr/incr", loghttp.Adapter(incrMountPoint))
    34  	http.HandleFunc("/fsi/cntr/decr", loghttp.Adapter(decrMountPoint))
    35  }
    36  
    37  // userinterface rendered to HTML - not only the strings for title and url
    38  func BackendUIRendered() *bytes.Buffer {
    39  
    40  	var b1 = new(bytes.Buffer)
    41  
    42  	htmlfrag.Wb(b1, "filesystem interface", "")
    43  
    44  	htmlfrag.Wb(b1, "set type", UriSetFSType)
    45  	htmlfrag.Wb(b1, "create", "/fsi/create-objects")
    46  
    47  	htmlfrag.Wb(b1, "query", "/fsi/retrieve-by-query")
    48  	htmlfrag.Wb(b1, "readdir", "/fsi/retrieve-by-read-dir")
    49  	htmlfrag.Wb(b1, "walk", "/fsi/walk")
    50  	htmlfrag.Wb(b1, "remove subset", "/fsi/remove")
    51  
    52  	// htmlfrag.Wb(b1, "delete all", "/fsi/delete-all", "all fs types")
    53  	htmlfrag.Wb(b1, "delete tree", UriDeleteSubtree, "of selected fs")
    54  
    55  	// htmlfrag.Wb(b1, , "")
    56  	htmlfrag.Wb(b1, "dsfs mount", "nobr")
    57  	htmlfrag.Wb(b1, "last", "/fsi/cntr/last")
    58  	htmlfrag.Wb(b1, "decr", "/fsi/cntr/decr")
    59  	htmlfrag.Wb(b1, "incr", "/fsi/cntr/incr")
    60  	htmlfrag.Wb(b1, "reset", "/fsi/cntr/reset")
    61  
    62  	return b1
    63  
    64  }
    65  
    66  func lastMountPoint(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {
    67  
    68  	wpf(w, tplx.ExecTplHelper(tplx.Head, map[string]interface{}{"HtmlTitle": "last Mountpoint"}))
    69  	defer wpf(w, tplx.Foot)
    70  
    71  	wpf(w, "<pre>\n")
    72  	defer wpf(w, "\n</pre>")
    73  
    74  	wpf(w, "reset %v\n", dsfs.MountPointLast())
    75  
    76  }
    77  func resetMountPoint(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {
    78  
    79  	wpf(w, tplx.ExecTplHelper(tplx.Head, map[string]interface{}{"HtmlTitle": "Mountpoint reset"}))
    80  	defer wpf(w, tplx.Foot)
    81  
    82  	wpf(w, "<pre>\n")
    83  	defer wpf(w, "\n</pre>")
    84  
    85  	wpf(w, "reset %v\n", dsfs.MountPointReset())
    86  
    87  }
    88  
    89  func incrMountPoint(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {
    90  
    91  	wpf(w, tplx.ExecTplHelper(tplx.Head, map[string]interface{}{"HtmlTitle": "Mountpoint increment"}))
    92  	defer wpf(w, "\n</pre>")
    93  
    94  	wpf(w, "<pre>\n")
    95  	defer wpf(w, tplx.Foot)
    96  
    97  	xx := r.Header.Get("adapter_01")
    98  	wpf(w, "adapter set %q\n", xx)
    99  
   100  	wpf(w, "counted up %v\n", dsfs.MountPointIncr())
   101  
   102  }
   103  
   104  func decrMountPoint(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {
   105  
   106  	wpf(w, tplx.ExecTplHelper(tplx.Head, map[string]interface{}{"HtmlTitle": "Mountpoint decrement"}))
   107  	defer wpf(w, tplx.Foot)
   108  
   109  	wpf(w, "<pre>\n")
   110  	defer wpf(w, "\n</pre>")
   111  
   112  	wpf(w, "counted down %v\n", dsfs.MountPointDecr())
   113  
   114  }