// Handler handles all requests to plotocrat.com
// TODO: using a framework would be nice...
Handler handles all requests to plotocrat.com
TODO: using a framework would be nice
// Handler handles all requests to plotocrat . com // TODO : using a framework would be nice ...
func Handler(w http.ResponseWriter, r *http.Request) {
	if r.Method == http.MethodPost && r.URL.Path == "/" {
		name, data, err := readSinglePart(r)
		if err != nil {
			w.WriteHeader(http.StatusBadRequest)
			w.Write([]byte("bad-request: " + err.Error() + "\n\n"))
			w.Write([]byte("Usage: curl -F \"x-axis-title=@filename\" plotocrat.com\n"))
			return
		}
		chart := &Chart{
			ID: newID(),
			XAxis: name,
			Data: data,
		}
		if _, err := env.DB.NamedExec("INSERT INTO charts (id, x_axis, data) VALUES (:id, :x_axis, :data)", chart); err != nil {
			panic(err)
		}
		fmt.Fprintln(w, env.Config.ServerOrigin + "/" + chart.ID)
		return
	}
	if r.Method != http.MethodGet {
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte("bad-request: GET is only supported method\n\n"))
		w.Write([]byte("Usage: curl -F \"x-axis-title=@filename\" plotocrat.com\n"))
		return 
	}
	if r.URL.Path == "/" {
		w.Header().Add("Content-Type", "text/html; charset=utf-8")
		w.Write(getIndexRendered())
		return
	}
	path := strings.TrimPrefix(r.URL.Path, "/")
	if !isValidID(path) {
		w.WriteHeader(http.StatusNotFound)
		w.Write([]byte("not found"))
		return
	}
	chart := Chart{}
	err := env.DB.Get(&chart, "SELECT * FROM charts WHERE id = $1;", path)
	if err == sql.ErrNoRows {
		w.WriteHeader(http.StatusNotFound)
		w.Write([]byte("not found"))
		return
	}
	if err != nil {
		fmt.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		w.Write([]byte("internal server error"))
		return
	}
	renderChart(w, &chart)
}
func Handler ( w http . ResponseWriter , r * http . Request ) { if r . Method == http . MethodPost && r . URL . Path == " " { name , data , err := readSinglePart ( r ) if err != nil { w . WriteHeader ( http . StatusBadRequest ) w . Write ( [ ] byte ( " " + err . Error ( ) + " \n \n " ) ) w . Write ( [ ] byte ( " \" \" \n " ) ) return } chart := & Chart { ID : newID ( ) , XAxis : name , Data : data , } if _ , err := env . DB . NamedExec ( " " , chart ) ; err != nil { panic ( err ) } fmt . Fprintln ( w , env . Config . ServerOrigin + " " + chart . ID ) return } if r . Method != http . MethodGet { w . WriteHeader ( http . StatusBadRequest ) w . Write ( [ ] byte ( " \n \n " ) ) w . Write ( [ ] byte ( " \" \" \n " ) ) return } if r . URL . Path == " " { w . Header ( ) . Add ( " " , " " ) w . Write ( getIndexRendered ( ) ) return } path := strings . TrimPrefix ( r . URL . Path , " " ) if ! isValidID ( path ) { w . WriteHeader ( http . StatusNotFound ) w . Write ( [ ] byte ( " " ) ) return } chart := Chart { } err := env . DB . Get ( & chart , " " , path ) if err == sql . ErrNoRows { w . WriteHeader ( http . StatusNotFound ) w . Write ( [ ] byte ( " " ) ) return } if err != nil { fmt . Println ( err ) w . WriteHeader ( http . StatusInternalServerError ) w . Write ( [ ] byte ( " " ) ) return } renderChart ( w , & chart ) }
Handler handles all requests to plotocrat.com
TODO: using a framework would be nice
Handler handles all requests to plotocrat . com TODO : using a framework would be nice