在 Golang 中文本生成图片

我创建此应用程序的原因是为了在 WhatsApp 或其他消息传递应用程序中共享 Linux 配置和源代码片段等文本内容。另一种是为 Twitter、Facebook 或 LinkedIn 中的社交媒体帖子生成特色图片。

 

项目结构

源代码

完整的源代码可以在 github 上找到:https://github.com/johnpili/go-text-to-image

文件 main.go 包含运行此应用程序的初始化和配置代码。

 

package main

import (
    "flag"
    "github.com/johnpili/go-text-to-image/controllers"
    "github.com/johnpili/go-text-to-image/models"
    "io/ioutil"
    "log"
    "net/http"
    "os"
    "strconv"
    "time"

    rice "github.com/GeertJohan/go.rice"
    "github.com/go-zoo/bone"
    "github.com/psi-incontrol/go-sprocket/sprocket"
)

var (
    configuration models.Config
)

func main() {
    pid := os.Getpid()
    err := ioutil.WriteFile("application.pid", []byte(strconv.Itoa(pid)), 0666)
    if err != nil {
        log.Fatal(err)
    }

    var configLocation string
    flag.StringVar(&configLocation, "config", "config.yml", "Set the location of configuration file")
    flag.Parse()

    sprocket.LoadYAML(configLocation, &configuration)

    viewBox := rice.MustFindBox("views")
    staticBox := rice.MustFindBox("static")
    controllersHub := controllers.New(viewBox, nil, nil, &configuration)
    staticFileServer := http.StripPrefix("/static/", http.FileServer(staticBox.HTTPBox()))

    router := bone.New()
    router.Get("/static/", staticFileServer)
    controllersHub.BindRequestMapping(router)

    // CODE FROM https://medium.com/@mossila/running-go-behind-iis-ce1a610116df
    port := strconv.Itoa(configuration.HTTP.Port)
    if os.Getenv("ASPNETCORE_PORT") != "" { // get enviroment variable that set by ACNM
        port = os.Getenv("ASPNETCORE_PORT")
    }

    httpServer := &http.Server{
        Addr:         ":" + port,
        Handler:      router,
        ReadTimeout:  120 * time.Second,
        WriteTimeout: 120 * time.Second,
    }

    if configuration.HTTP.IsTLS {
        log.Printf("Server running at https://localhost/tools:%s/\n", port)
        log.Fatal(httpServer.ListenAndServeTLS(configuration.HTTP.ServerCert, configuration.HTTP.ServerKey))
        return
    }
    log.Printf("Server running at http://localhost/tools:%s/\n", port)
    log.Fatal(httpServer.ListenAndServe())
}

page_controller.go 处理页面渲染和图像生成。

原文地址 How to Generate text to image in Golang - Golang Libraries, Apps, Golang Jobs and Go Tutorials

posted @ 2022-11-18 16:16  泛艺舟  阅读(673)  评论(0)    收藏  举报