github.com/aarzilli/tools@v0.0.0-20151123112009-0d27094f75e0/net/http/repo/3_rcv_cmd.go (about)

     1  package repo
     2  
     3  import (
     4  	"encoding/json"
     5  	"io"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"path"
     9  
    10  	"github.com/pbberlin/tools/net/http/loghttp"
    11  	"github.com/pbberlin/tools/net/http/tplx"
    12  	"github.com/pbberlin/tools/stringspb"
    13  	"google.golang.org/appengine"
    14  )
    15  
    16  // fetchCommandReceiver takes http post requests, extracts the JSON commands
    17  // and submits them to FetchHTML
    18  func fetchCommandReceiver(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {
    19  
    20  	lg, lge := loghttp.Logger(w, r)
    21  
    22  	var fcs []FetchCommand
    23  
    24  	// The type of resp.body  <io.Reader> lends itself to using decode.
    25  	// http://stackoverflow.com/ - ... using-json-unmarshal-vs-json-newdecoder-decode
    26  	//
    27  	// Nevertheless, we use Unmarshal here, because we want to inspect the bytes of body.
    28  	var Unmarshal_versus_Decode = true
    29  
    30  	if Unmarshal_versus_Decode {
    31  
    32  		body, err := ioutil.ReadAll(r.Body) // no response write until here !
    33  		lge(err)
    34  
    35  		if len(body) == 0 {
    36  			lg("empty body")
    37  			return
    38  		}
    39  
    40  		err = json.Unmarshal(body, &fcs)
    41  		if err != nil {
    42  			lge(err)
    43  			lg("body is %s", body)
    44  			return
    45  		}
    46  
    47  	} else {
    48  
    49  		dec := json.NewDecoder(r.Body)
    50  		for {
    51  			if err := dec.Decode(&fcs); err == io.EOF {
    52  				break
    53  			} else if err != nil {
    54  				lge(err)
    55  				return
    56  			}
    57  			lg("command loop is: %s", stringspb.IndentedDump(fcs))
    58  		}
    59  
    60  	}
    61  
    62  	FetchHTML(w, r, fcs)
    63  
    64  }
    65  
    66  // FetchHTML executes the fetch commands.
    67  // It creates the configured filesystem and calls the fetcher.
    68  func FetchHTML(w http.ResponseWriter, r *http.Request, fcs []FetchCommand) {
    69  
    70  	lg, lge := loghttp.Logger(w, r)
    71  	var err error
    72  
    73  	fs := GetFS(appengine.NewContext(r))
    74  	// fs = fsi.FileSystem(memMapFileSys)
    75  
    76  	wpf(w, tplx.ExecTplHelper(tplx.Head, map[string]interface{}{"HtmlTitle": "Requesting files"}))
    77  	defer wpf(w, tplx.Foot)
    78  
    79  	wpf(w, "<pre>")
    80  	defer wpf(w, "</pre>")
    81  
    82  	err = fs.WriteFile(path.Join(docRoot, "msg.html"), msg, 0644)
    83  	lge(err)
    84  
    85  	// err = fs.WriteFile(path.Join(docRoot, "index.html"), []byte("content of index.html"), 0644)
    86  	// lge(err)
    87  
    88  	err = fs.MkdirAll(path.Join(docRoot, "testDirX/testDirY"), 0755)
    89  	lge(err)
    90  
    91  	for _, config := range fcs {
    92  		FetchUsingRSS(w, r, fs, config)
    93  	}
    94  
    95  	lg("fetching complete")
    96  
    97  }