Exemplo n.º 1
0
func replyWithPlaceholder(req *http.Request, w http.ResponseWriter, err Error, o ServerOptions) error {
	image := o.PlaceholderImage

	// Resize placeholder to expected output
	buf, _err := bimg.Resize(o.PlaceholderImage, bimg.Options{
		Force:   true,
		Crop:    true,
		Enlarge: true,
		Width:   parseInt(req.URL.Query().Get("width")),
		Height:  parseInt(req.URL.Query().Get("height")),
		Type:    ImageType(req.URL.Query().Get("type")),
	})

	if _err != nil {
		w.Header().Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte(fmt.Sprintf("{\"error\":\"%s\", \"code\": %d}", _err.Error(), BadRequest)))
		return _err
	}

	// Use final response body image
	image = buf

	// Placeholder image response
	w.Header().Set("Content-Type", GetImageMimeType(bimg.DetermineImageType(image)))
	w.Header().Set("Error", string(err.JSON()))
	w.WriteHeader(err.HTTPCode())
	w.Write(image)
	return err
}
Exemplo n.º 2
0
func main() {
	flag.Usage = func() {
		fmt.Fprint(os.Stderr, fmt.Sprintf(usage, Version, runtime.NumCPU()))
	}
	flag.Parse()

	if *aHelp || *aHelpl {
		showUsage()
	}
	if *aVers || *aVersl {
		showVersion()
	}

	// Only required in Go < 1.5
	runtime.GOMAXPROCS(*aCpus)

	port := getPort(*aPort)
	opts := ServerOptions{
		Port:              port,
		Address:           *aAddr,
		Gzip:              *aGzip,
		CORS:              *aCors,
		AuthForwarding:    *aAuthForwarding,
		EnableURLSource:   *aEnableURLSource,
		EnablePlaceholder: *aEnablePlaceholder,
		PathPrefix:        *aPathPrefix,
		ApiKey:            *aKey,
		Concurrency:       *aConcurrency,
		Burst:             *aBurst,
		Mount:             *aMount,
		CertFile:          *aCertFile,
		KeyFile:           *aKeyFile,
		Placeholder:       *aPlaceholder,
		HttpCacheTtl:      *aHttpCacheTtl,
		HttpReadTimeout:   *aReadTimeout,
		HttpWriteTimeout:  *aWriteTimeout,
		Authorization:     *aAuthorization,
		AlloweOrigins:     parseOrigins(*aAlloweOrigins),
	}

	// Create a memory release goroutine
	if *aMRelease > 0 {
		memoryRelease(*aMRelease)
	}

	// Check if the mount directory exists, if present
	if *aMount != "" {
		checkMountDirectory(*aMount)
	}

	// Validate HTTP cache param, if present
	if *aHttpCacheTtl != -1 {
		checkHttpCacheTtl(*aHttpCacheTtl)
	}

	// Read placeholder image, if required
	if *aPlaceholder != "" {
		buf, err := ioutil.ReadFile(*aPlaceholder)
		if err != nil {
			exitWithError("cannot start the server: %s", err)
		}

		imageType := bimg.DetermineImageType(buf)
		if !bimg.IsImageTypeSupportedByVips(imageType) {
			exitWithError("Placeholder image type is not supported. Only JPEG, PNG or WEBP are supported")
		}

		opts.PlaceholderImage = buf
	} else if *aEnablePlaceholder {
		// Expose default placeholder
		opts.PlaceholderImage = placeholder
	}

	debug("imaginary server listening on port :%d/%s", opts.Port, strings.TrimPrefix(opts.PathPrefix, "/"))

	// Load image source providers
	LoadSources(opts)

	// Start the server
	err := Server(opts)
	if err != nil {
		exitWithError("cannot start the server: %s", err)
	}
}