168 lines
9.2 KiB
Plaintext
168 lines
9.2 KiB
Plaintext
prompts_server.txt
|
||
|
||
|
||
我们在golang程序中使用了mysql,因此引入了这个库 github.com/go-sql-driver/mysql 来进行操作。现在我们需要开启一个事务,它需要操作两条sql进行更新数据库。请你帮我写个事务的示例。
|
||
|
||
|
||
我在使用 mysql8.0 时发现了一个奇怪的问题。
|
||
我创建了一个user表,它包含一个字段 RegTime,它是 timestamp 类型,默认值是 CURRENT_TIMESTAMP; 我写入数据时,不指定 RegTime 的值,让它取默认值,然后读取出来的时候,它会显示当前时区(东八区)的值;
|
||
我又创建了一个vip表,它包含一个字段 PurchaseDate ,它是 timestamp 类型,默认值是 CURRENT_TIMESTAMP;我写入数据时,会在程序中给他赋值,golang的代码如下:
|
||
currentTime := time.Now()
|
||
写入之后,读取数据时,它变成了UTC时间,也就是比当前的东八区少了八个小时!
|
||
|
||
已知mysql的 time_zone 设置为了 SYSTEM, 而服务器的设置 timedatectl
|
||
Local time: Sun 2024-08-25 15:21:10 CST 也是东八区的。
|
||
那么,问题出在哪里呢? 为什么我在golang中给 PurchaseDate 赋值,它的显示,不是东八区的时间?
|
||
|
||
|
||
请帮我写一个shell脚本,它用来管理服务端程序的启动和退出。它接收一个参数:
|
||
1,当参数取值为 start 时,执行命令
|
||
cd /usr/local/aigrammar/
|
||
nohup ./aigrammar > output.log 2>&1 &
|
||
|
||
2, 当参数取值为 stop 时,执行命令
|
||
killall aigrammar
|
||
|
||
3, 当参数取值为 restart 时,执行命令
|
||
killall aigrammar
|
||
sleep(2)
|
||
cd /usr/local/aigrammar/
|
||
nohup ./aigrammar > output.log 2>&1 &
|
||
|
||
|
||
|
||
我的服务器是 ubuntu的,它的时区设置是: timedatectl
|
||
Local time: Sat 2024-08-17 12:02:29 CST
|
||
Universal time: Sat 2024-08-17 04:02:29 UTC
|
||
RTC time: Sat 2024-08-17 04:02:29
|
||
Time zone: Asia/Shanghai (CST, +0800)
|
||
然后我的mysql的时区,是跟随系统的:SHOW VARIABLES LIKE 'time_zone'; SYSTEM。
|
||
|
||
然后我在golang中,获取时间
|
||
currentTime := time.Now()
|
||
nextDay = time.Unix(transantion.ExpiresDate/1000, 0).In(time.Local)
|
||
并写入到mysql的 timestamp 字段中,通过mysql客户端读取时发现它的时间不对,变成了 UTC时间,而不是 CST时间。这是怎么回事呢?
|
||
|
||
|
||
|
||
现在我们需要增加一个函数 QueryUserBenefits,它的输入参数与 QueryUserBenefits 类似,ID保持一致,增加一个 datestr 参数,它的格式类似于 '20240705', 默认值是'*'。函数的作用是删除 ID:datastr的redis key,如果datestr为默认值的话,则表示全部。请完成你的代码
|
||
|
||
|
||
到现在为止,我们的代码中包含了多个.go文件,并且每个文件完成了不同的功能,它还使用了 echo框架来处理http请求。
|
||
我们想在工程里面加入日志,以更好的记录发生了什么。go.uber.org/zap 是一个高性能的日志库,github.com/natefinch/lumberjack 可以进行日志文件的切割,所以我们想把这两个引入到我们的代码中,并且对日志格式做一定的调整,比如json格式,加入日期,日志级别,打印日志的文件和代码行等等。
|
||
现在,请你给出相关的代码。
|
||
|
||
根据我们使用的全局变量的logger.go版本,我们现在需要在init()时,传入一些变量,使得日志可以配置。这些变量包括lumberjack.Logger中使用的Filename, MaxSize, MaxBackups, MaxAge, Compress ,以及 atomicLevel.SetLevel 使用的日志级别。
|
||
配置项会在config.toml中,使用我们已经实现的config.go来完成配置的读取,并且在 main.go 中获取配置,调用init()时传入。
|
||
请注意,这里面可能会涉及到类型转换,比如把配置项中的 日志级别字符串,转换成 atomicLevel.SetLevel 所需要的枚举值。
|
||
|
||
|
||
type LoggerConfig struct {
|
||
// 在 Go 中,只有首字母大写的字段才能被外部包(如 viper)访问。
|
||
LogFile string `mapstructure:"log_file"`
|
||
MaxSize int `mapstructure:"max_size"`
|
||
MaxBackups int `mapstructure:"max_backups"`
|
||
MaxAge int `mapstructure:"max_age"`
|
||
Compress bool `mapstructure:"compress"`
|
||
Level string `mapstructure:"level"`
|
||
}
|
||
我们在config.go 中解析toml文件的上面一段,报错了,显示 toml: incomplete number 这是什么问题,应该如何解决?
|
||
|
||
|
||
|
||
for _, response := range responses {
|
||
transantions, err := client.ParseSignedTransactions(response.SignedTransactions)
|
||
if err != nil {
|
||
logger.Error("ParseSignedTransactions error.", zap.Error(err))
|
||
return c.JSON(http.StatusInternalServerError, "ParseSignedTransactions error")
|
||
}
|
||
logger.Debug("transantions", zap.Any("transantions", transantions)) // 打印
|
||
//buff, _ := json.Marshal(&transantions)
|
||
//fmt.Println(string(buff))
|
||
}
|
||
transantions 的类型是 []*api.JWSTransaction。现在我们想定义一个变量,它把每次for循环所产生的 transantions ,全部放到一起,形成一个大的数组。请问应该怎么写
|
||
|
||
|
||
|
||
|
||
我们在使用golang编写服务端程序,使用echo框架处理http请求并返回给客户端。对于注册到echo上的每个处理函数,它的返回有两种,一是出错了,会返回
|
||
echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
||
二是正常处理了请求,返回 c.JSON(http.StatusOK, map[string]string{"translation": translation})
|
||
|
||
现在我们想统一返回格式,以便于客户端进行正确的处理。我们希望返回的http状态码都是 200,但返回结果是 json 数据: {"ret": %d, "message":"%s", "data":%v} 其中ret为错误吗,它是一个数字,data也是json格式的数据。
|
||
|
||
要实现以上的功能,我们需要怎么在echo框架上注册函数?并且在每个接口处理中,应该怎么返回?
|
||
|
||
|
||
我用了这种方法,然后在接口函数中返回 return echo.NewHTTPError(http.StatusBadRequest, "no benifits left."),可是最后的输出确是这样:
|
||
{
|
||
"message": "no benifits left."
|
||
}
|
||
{
|
||
"ret": 400,
|
||
"message": "no benifits left.",
|
||
"data": null
|
||
}
|
||
为什么,是哪里出错了吗?
|
||
|
||
|
||
|
||
timezone Asia/Shanghai
|
||
secondsfromgmt 28800
|
||
|
||
|
||
// 从苹果校验订单后,插入vip表中
|
||
func UpdateOrderByVerify(ID int, AppAcountToken string, OriginTransID string, transantion *api.JWSTransaction) error {
|
||
// 写入vip表,如果ID对应记录不存在,则插入,否则更新
|
||
|
||
}
|
||
这是我们需要完成的函数,需要把数据写入到VIP表,如果ID对应的记录不存在,则插入;否则更新;写入时,IsVIP为1,Appstore 为'Apple',ProductID 从transantion中获取,
|
||
|
||
CREATE TABLE aigrammar.vip (
|
||
ID INT UNSIGNED NOT NULL,
|
||
IsVIP INT DEFAULT 0 NULL COMMENT '1-VIP; 0-not vip',
|
||
AppStore varchar(100) DEFAULT 'apple' NULL COMMENT 'apple;google',
|
||
ProductID varchar(100) NULL,
|
||
ProductType varchar(100) NULL COMMENT 'yearly;monthly;weekly;',
|
||
Environment varchar(100) NULL COMMENT 'prod;sandbox',
|
||
PurchaseDate TIMESTAMP NULL,
|
||
Price INT NULL,
|
||
Currency varchar(100) NULL,
|
||
Storefront varchar(100) NULL COMMENT 'USA',
|
||
ExpDate TIMESTAMP NULL,
|
||
AutoRenew INT NULL COMMENT '1-yes;0-no',
|
||
OriginalTransactionID varchar(100) NULL COMMENT 'applestore originalTransactionId',
|
||
CONSTRAINT vip_pk PRIMARY KEY (ID)
|
||
)
|
||
ENGINE=InnoDB
|
||
DEFAULT CHARSET=utf8mb4
|
||
COLLATE=utf8mb4_0900_ai_ci;
|
||
|
||
type JWSTransaction struct {
|
||
TransactionID string `json:"transactionId,omitempty"`
|
||
OriginalTransactionId string `json:"originalTransactionId,omitempty"`
|
||
WebOrderLineItemId string `json:"webOrderLineItemId,omitempty"`
|
||
BundleID string `json:"bundleId,omitempty"`
|
||
ProductID string `json:"productId,omitempty"`
|
||
SubscriptionGroupIdentifier string `json:"subscriptionGroupIdentifier,omitempty"`
|
||
PurchaseDate int64 `json:"purchaseDate,omitempty"`
|
||
OriginalPurchaseDate int64 `json:"originalPurchaseDate,omitempty"`
|
||
ExpiresDate int64 `json:"expiresDate,omitempty"`
|
||
Quantity int32 `json:"quantity,omitempty"`
|
||
Type IAPType `json:"type,omitempty"`
|
||
AppAccountToken string `json:"appAccountToken,omitempty"`
|
||
InAppOwnershipType string `json:"inAppOwnershipType,omitempty"`
|
||
SignedDate int64 `json:"signedDate,omitempty"`
|
||
OfferType int32 `json:"offerType,omitempty"`
|
||
OfferIdentifier string `json:"offerIdentifier,omitempty"`
|
||
RevocationDate int64 `json:"revocationDate,omitempty"`
|
||
RevocationReason *int32 `json:"revocationReason,omitempty"`
|
||
IsUpgraded bool `json:"isUpgraded,omitempty"`
|
||
Storefront string `json:"storefront,omitempty"`
|
||
StorefrontId string `json:"storefrontId,omitempty"`
|
||
TransactionReason TransactionReason `json:"transactionReason,omitempty"`
|
||
Environment Environment `json:"environment,omitempty"`
|
||
Price int64 `json:"price,omitempty"`
|
||
Currency string `json:"currency,omitempty"`
|
||
OfferDiscountType OfferDiscountType `json:"offerDiscountType,omitempty"`
|
||
} |