Exemplo n.º 1
0
package httputil

import (
	"io"
	"io/ioutil"
	"net/http"
	"os"
	"path/filepath"

	"github.com/johnnylee/goutil/logutil"
)

var log = logutil.New("httputil")

var MaxMemory int64 = 1048576

func SaveFormFileToTmp(r *http.Request, key string) (string, string, error) {
	// Parse the form.
	if err := r.ParseMultipartForm(MaxMemory); err != nil {
		return "", "", err
	}

	// Create a temporary directory.
	tempDir, err := ioutil.TempDir("", "upload-")
	if err != nil {
		log.Err(err, "When creating a temporary directory")
		return "", "", err
	}

	src, header, err := r.FormFile(key)
	if err != nil {
Exemplo n.º 2
0
import (
	"encoding/json"
	"io"
	"io/ioutil"
	"net/http"
	"os"
	"path/filepath"

	"github.com/gorilla/schema"
	"github.com/johnnylee/goutil/logutil"
)

var schemaDecoder *schema.Decoder

var log = logutil.New("httprequest")

func init() {
	log.Msg("Initializing schema decoder...")
	schemaDecoder = schema.NewDecoder()
	schemaDecoder.IgnoreUnknownKeys(true)
}

type Request struct {
	W http.ResponseWriter
	R *http.Request
}

func New(w http.ResponseWriter, r *http.Request) Request {
	return Request{w, r}
}
Exemplo n.º 3
0
package httpmiddleware

import (
	"compress/gzip"
	"io"
	"net/http"
	"strings"
	"sync"
	"time"

	"github.com/johnnylee/goutil/logutil"
)

// We log HTTP requests using the httpLogger.
var httpLogger = logutil.New("http")

// This is a wrapper for the standard http handler that adds request logging,
// including timing.
func Log(handler http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		tStart := time.Now().UnixNano()
		handler.ServeHTTP(w, r)
		tEnd := time.Now().UnixNano()
		httpLogger.Msg("%s %s %s %s (%v ms)",
			r.RemoteAddr, r.Method, r.URL, r.Referer(), (tEnd-tStart)/1000000)
	})
}

type GzResponseWriter struct {
	io.Writer
	http.ResponseWriter