Exemplo n.º 1
0
func main() {

	logger = log.New(os.Stdout, fmt.Sprintf("[%s] ", config.AppName()), 0)

	r := marmoset.NewRouter()

	// API
	r.GET("/status", controllers.Status)
	r.POST("/base64", controllers.Base64)
	r.POST("/file", controllers.FileUpload)

	r.Static("/assets", "./assets")

	// Sample Page
	r.GET("/", controllers.Index)

	chain := marmoset.NewFilter(r)
	if os.Getenv("OCRSERVER_LOG_ENABLED") == "1" {
		chain.Add(&filters.LogFilter{Logger: logger})
	}
	server := chain.Server()

	logger.Printf("listening on port %s", config.Port())
	err := http.ListenAndServe(config.Port(), server)
	logger.Println(err)
}
Exemplo n.º 2
0
// Base64 ...
func Base64(w http.ResponseWriter, r *http.Request) {

	render := marmoset.Render(w, true)

	var body = new(struct {
		Base64    string `json:"base64"`
		Trim      string `json:"trim"`
		Languages string `json:"languages"`
		Whitelist string `json:"whitelist"`
	})

	err := json.NewDecoder(r.Body).Decode(body)
	if err != nil {
		render.JSON(http.StatusBadRequest, err)
		return
	}

	tempfile, err := ioutil.TempFile("", config.AppName()+"-")
	if err != nil {
		render.JSON(http.StatusInternalServerError, err)
		return
	}
	defer func() {
		tempfile.Close()
		os.Remove(tempfile.Name())
	}()

	if len(body.Base64) == 0 {
		render.JSON(http.StatusBadRequest, fmt.Errorf("base64 string required"))
		return
	}
	body.Base64 = regexp.MustCompile("data:image\\/png;base64,").ReplaceAllString(body.Base64, "")
	b, err := base64.StdEncoding.DecodeString(body.Base64)
	if err != nil {
		render.JSON(http.StatusBadRequest, err)
		return
	}
	tempfile.Write(b)

	// TODO: refactor gosseract
	body.Languages = "eng"

	result := gosseract.Must(gosseract.Params{
		Src:       tempfile.Name(),
		Languages: body.Languages,
		Whitelist: body.Whitelist,
	})
	render.JSON(http.StatusOK, map[string]interface{}{
		"result":  strings.Trim(result, body.Trim),
		"version": config.Version(),
	})
}
Exemplo n.º 3
0
// Index ...
func Index(w http.ResponseWriter, r *http.Request) {
	tmpl, err := template.New("").Parse(view)
	if err != nil {
		m.Render(w, true).JSON(http.StatusInternalServerError, m.P{
			"message": err.Error(),
		})
		return
	}
	tmpl.Execute(w, map[string]interface{}{
		"AppName": config.AppName(),
	})
	/* TODO: marmoset.View doesn't work in Heroku instance :(
	marmoset.Render(w).HTML("index", map[string]interface{}{
		"AppName": config.AppName(),
	})
	*/
}
Exemplo n.º 4
0
// FileUpload ...
func FileUpload(w http.ResponseWriter, r *http.Request) {

	render := marmoset.Render(w, true)

	whitelist := r.FormValue("whitelist")
	trim := r.FormValue("trim")
	// Get uploaded file
	r.ParseMultipartForm(32 << 20)
	// upload, h, err := r.FormFile("file")
	upload, _, err := r.FormFile("file")
	if err != nil {
		render.JSON(http.StatusBadRequest, err)
		return
	}
	defer upload.Close()

	// Create physical file
	tempfile, err := ioutil.TempFile("", config.AppName()+"-")
	if err != nil {
		render.JSON(http.StatusBadRequest, err)
		return
	}
	defer func() {
		tempfile.Close()
		os.Remove(tempfile.Name())
	}()

	// Make uploaded physical
	if _, err := io.Copy(tempfile, upload); err != nil {
		render.JSON(http.StatusInternalServerError, err)
		return
	}

	result := gosseract.Must(gosseract.Params{
		Src:       tempfile.Name(),
		Languages: "eng",
		Whitelist: whitelist,
	})

	render.JSON(http.StatusOK, map[string]interface{}{
		"result":  strings.Trim(result, trim),
		"version": config.Version(),
	})
}
Exemplo n.º 5
0
func main() {

	logger = log.New(os.Stdout, fmt.Sprintf("[%s] ", config.AppName()), 0)

	r := marmoset.NewRouter()

	// API
	r.GET("/status", controllers.Status)
	r.POST("/base64", controllers.Base64)
	r.POST("/file", controllers.FileUpload)

	r.StaticRelative("/assets", "./assets")
	marmoset.LoadViews("./views")

	// Sample Page
	r.GET("/", controllers.Index)

	server := marmoset.NewFilter(r).Add(&filters.LogFilter{Logger: logger}).Server()

	logger.Printf("listening on port %s", config.Port())
	err := http.ListenAndServe(config.Port(), server)
	logger.Println(err)
}
Exemplo n.º 6
0
// Index ...
func Index(w http.ResponseWriter, r *http.Request) {
	marmoset.Render(w).HTML("index", map[string]interface{}{
		"AppName": config.AppName(),
	})
}