talent/main.go

112 lines
3.0 KiB
Go

package main
import (
"encoding/base64"
"flag"
"fmt"
"net/http"
asset "talent/assets"
"time"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
var (
flagAddr string
flagDev bool
flagMigrate bool
flagBase string
DB_HOST string
DB_USER string
DB_PASSWD string
DB_NAME string
DB_PORT int
LINE_SECRET string
LINE_TOKEN string
IBM_INSTANCE string
IBM_ASSISTANT string
IBM_API string
Version string
Build string
)
func init() {
flag.StringVar(&flagAddr, "addr", "0.0.0.0:8000", "address to listen on.")
flag.StringVar(&flagBase, "base", "https://bot.ework.tw", "base url.")
flag.BoolVar(&flagDev, "dev", false, "is dev mode")
flag.BoolVar(&flagMigrate, "migrate", false, "migrate database")
flag.StringVar(&DB_HOST, "db-host", "postgres.ework.tw", "postgress db host.")
flag.StringVar(&DB_USER, "db-user", "talent", "postgress db user.")
flag.StringVar(&DB_PASSWD, "db-passwd", "show", "postgress db pqssword.")
flag.StringVar(&DB_NAME, "db-name", "talent", "postgress db name.")
flag.IntVar(&DB_PORT, "db-port", 5432, "postgress db port.")
flag.StringVar(&LINE_SECRET, "line-secret", "cf406aa7577569ba2b211bc04f51630e", "line channel secret.")
flag.StringVar(&LINE_TOKEN, "line-token", "IhP4dLM91boFQlRzHa/Iv9el2xXm2X6ByiDuWQXm6ndQL1LA+yA+O8x2OeuJkpSWlf5IE8cwkA+Mca18EjO42Q6vRm0T5cRdTLkO+42SX9HAx2GdJnhWu+S4IplOt38YktfmpodCyk6bXDBJp9YVwgdB04t89/1O/w1cDnyilFU=", "channel access token")
flag.StringVar(&IBM_INSTANCE, "ibm-instance", "d9329221-40fb-4911-ba9f-69098349b8ab", "watson instance id")
flag.StringVar(&IBM_ASSISTANT, "ibm-assistant", "13cb9242-722a-4644-ba9b-7a761ffe807e", "watson assistant id")
flag.StringVar(&IBM_API, "ibm-api", "tLHR-Fhob0UideN1HUQdQ5grtl338X_VrrT31yy8FbKB", "watson api token")
}
func main() {
tz, err := time.LoadLocation("Asia/Taipei")
if err == nil {
time.Local = tz
}
flag.Parse()
setupLog()
setupServer()
connectToDB()
if flagMigrate {
migrateDB()
}
setupLine()
IBM_API = fmt.Sprintf("Basic %v", base64.URLEncoding.EncodeToString([]byte(fmt.Sprintf("apikey:%v", IBM_API))))
// setup route
server.GET("/", func(c *gin.Context) { OK(c, "ok") })
server.GET("/check", func(c *gin.Context) {
c.Writer.Write(check_web)
})
server.GET("/profile/:uid", func(c *gin.Context) {
log.Debug().Str("uid", c.Param("uid")).Msg("profile")
c.Writer.Write(profile_web)
})
server.GET("/claim/:oid", func(c *gin.Context) {
log.Debug().Str("oid", c.Param("oid")).Msg("claim")
c.Writer.Write(claim_web)
})
server.GET("/product/:pid", func(c *gin.Context) {
log.Debug().Str("pid", c.Param("pid")).Msg("product")
c.Writer.Write(product_web)
})
// setup api
api := server.Group("api")
api.POST("/email", ApiUpdateEmail)
api.POST("/attach/:oid", ApiPostReceipt)
// setup bot webhook
hook := server.Group("bot")
hook.POST("/", webhook)
// setup static files
server.StaticFS("static", http.FS(asset.FS))
server.Static("attachment", "attachment")
// start server
log.Info().Msgf("serving on %v", flagAddr)
if err := server.Run(flagAddr); err != nil {
panic(err)
}
}