talent/main.go

112 lines
3.0 KiB
Go
Raw Normal View History

2021-12-30 09:16:20 +00:00
package main
import (
2022-01-03 10:28:00 +00:00
"encoding/base64"
2021-12-30 09:16:20 +00:00
"flag"
2022-01-03 10:28:00 +00:00
"fmt"
2022-01-04 07:09:46 +00:00
"net/http"
asset "talent/assets"
2021-12-30 13:39:00 +00:00
"time"
2021-12-30 09:16:20 +00:00
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
var (
2022-01-03 14:50:49 +00:00
flagAddr string
flagDev bool
flagMigrate bool
2022-01-04 07:09:46 +00:00
flagBase string
2021-12-30 09:16:20 +00:00
DB_HOST string
DB_USER string
DB_PASSWD string
DB_NAME string
DB_PORT int
LINE_SECRET string
LINE_TOKEN string
2022-01-03 10:28:00 +00:00
IBM_INSTANCE string
IBM_ASSISTANT string
IBM_API string
2021-12-30 09:16:20 +00:00
Version string
Build string
)
func init() {
flag.StringVar(&flagAddr, "addr", "0.0.0.0:8000", "address to listen on.")
2022-01-04 07:09:46 +00:00
flag.StringVar(&flagBase, "base", "https://bot.ework.tw", "base url.")
2021-12-30 09:16:20 +00:00
flag.BoolVar(&flagDev, "dev", false, "is dev mode")
2022-01-03 14:50:49 +00:00
flag.BoolVar(&flagMigrate, "migrate", false, "migrate database")
2021-12-30 09:16:20 +00:00
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")
2022-01-03 10:28:00 +00:00
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")
2021-12-30 09:16:20 +00:00
}
func main() {
2021-12-30 13:39:00 +00:00
tz, err := time.LoadLocation("Asia/Taipei")
if err == nil {
time.Local = tz
}
2021-12-30 09:16:20 +00:00
flag.Parse()
setupLog()
setupServer()
connectToDB()
2022-01-03 14:50:49 +00:00
if flagMigrate {
migrateDB()
}
2021-12-30 09:16:20 +00:00
setupLine()
2022-01-03 10:28:00 +00:00
IBM_API = fmt.Sprintf("Basic %v", base64.URLEncoding.EncodeToString([]byte(fmt.Sprintf("apikey:%v", IBM_API))))
2021-12-30 09:16:20 +00:00
// setup route
server.GET("/", func(c *gin.Context) { OK(c, "ok") })
2022-01-04 05:01:42 +00:00
server.GET("/check", func(c *gin.Context) {
c.Writer.Write(check_web)
})
2022-01-04 12:39:28 +00:00
server.GET("/profile/:uid", func(c *gin.Context) {
log.Debug().Str("uid", c.Param("uid")).Msg("profile")
2022-01-04 05:01:42 +00:00
c.Writer.Write(profile_web)
})
2022-01-04 12:39:28 +00:00
server.GET("/claim/:oid", func(c *gin.Context) {
log.Debug().Str("oid", c.Param("oid")).Msg("claim")
2022-01-04 05:01:42 +00:00
c.Writer.Write(claim_web)
})
2022-01-04 12:39:28 +00:00
server.GET("/product/:pid", func(c *gin.Context) {
log.Debug().Str("pid", c.Param("pid")).Msg("product")
2022-01-04 05:01:42 +00:00
c.Writer.Write(product_web)
})
2022-01-03 05:06:26 +00:00
// setup api
api := server.Group("api")
api.POST("/email", ApiUpdateEmail)
api.POST("/attach/:oid", ApiPostReceipt)
2021-12-30 09:16:20 +00:00
// setup bot webhook
hook := server.Group("bot")
hook.POST("/", webhook)
2022-01-04 07:09:46 +00:00
// setup static files
server.StaticFS("static", http.FS(asset.FS))
server.Static("attachment", "attachment")
2021-12-30 09:16:20 +00:00
// start server
log.Info().Msgf("serving on %v", flagAddr)
if err := server.Run(flagAddr); err != nil {
panic(err)
}
}