-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
326 lines (278 loc) · 8.06 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package main
import (
"encoding/json"
"fmt"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/spf13/viper"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"io"
"net/http"
"os"
"strings"
"time"
)
var dbConn *gorm.DB
func main() {
// Parse command-line arguments
args := os.Args
if len(args) < 2 {
fmt.Println("Usage: ./app [call|serve]")
return
}
// Initialize configuration
initConfig()
// Initialize database
err := initDatabase()
if err != nil {
fmt.Println("Failed to connect to database", err)
return
}
// Check command-line arguments
command := args[1]
switch command {
case "call":
// Check for keyword argument
if len(args) < 3 {
fmt.Println("Usage: ./app call <keyword>")
return
}
keyword := args[2]
// Call OpenAI API and log the request
requestId := uuid.New()
// Print the UUID
response, err := callOpenAIAndLog(keyword, requestId.String())
if err != nil {
fmt.Printf("Failed to call OpenAI API: %v\n", err)
return
}
// Print response
fmt.Println(string(response))
case "serve":
// Initialize router
router := initRouter()
// Start HTTP server
port := viper.GetString("server.port")
err := http.ListenAndServe(":"+port, router)
if err != nil {
fmt.Printf("Failed to start server: %v\n", err)
return
}
default:
fmt.Println("Usage: ./app [call|serve]")
}
}
func initConfig() {
fmt.Println(os.Getenv("CONFIG_FILE"))
// If a CONFIG_FILE environment variable is set, read configuration from that file
if configPath := os.Getenv("CONFIG_FILE"); configPath != "" {
viper.SetConfigFile(configPath)
if err := viper.ReadInConfig(); err != nil {
fmt.Printf("Failed to read configuration file %s: %v\n", configPath, err)
os.Exit(1)
}
}
// Set default values for configuration properties
viper.SetDefault("server.port", "8080")
viper.SetDefault("db.host", "localhost")
viper.SetDefault("db.port", "3306")
viper.SetDefault("db.username", "root")
viper.SetDefault("db.password", "")
viper.SetDefault("db.database", "mydb")
// Set configuration file search paths and file name
viper.AddConfigPath(".")
viper.SetConfigName("config")
viper.SetConfigType("yaml")
// Attempt to read configuration from file
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
fmt.Println("Config file not found; using default configuration values.")
} else {
fmt.Printf("Failed to read configuration file: %v\n", err)
os.Exit(1)
}
}
// Bind environment variables to configuration properties
viper.AutomaticEnv()
// Print the final configuration
fmt.Println("Using the following configuration:")
fmt.Printf("Server port: %s\n", viper.GetString("server.port"))
fmt.Printf("Database host: %s\n", viper.GetString("db.host"))
fmt.Printf("Database port: %s\n", viper.GetString("db.port"))
fmt.Printf("Database username: %s\n", viper.GetString("db.username"))
fmt.Printf("Database password: %s\n", viper.GetString("db.password"))
fmt.Printf("Database name: %s\n", viper.GetString("db.database"))
}
func initDatabase() error {
// Read database configuration from the config file
dbConfig := viper.GetStringMapString("database")
dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
dbConfig["host"], dbConfig["port"], dbConfig["user"], dbConfig["password"], dbConfig["dbname"])
// Connect to the database
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
return err
}
// Perform database migrations
err = Up(db)
if err != nil {
return err
}
// Save the database connection to the global variable
dbConn = db
return nil
}
func initRouter() *mux.Router {
// Initialize router
router := mux.NewRouter()
// Define routes
router.HandleFunc("/api/openai", openAIHandler)
return router
}
func getIpAddress(req *http.Request) string {
ipAddress := req.Header.Get("X-Real-IP")
if ipAddress == "" {
ipAddress = req.Header.Get("X-Forwarded-For")
if ipAddress == "" {
ipAddress = req.RemoteAddr
}
}
return ipAddress
}
type RequestBody struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
Temperature float32 `json:"temperature"`
MaxTokens int `json:"max_tokens"`
}
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
}
func callOpenAIAndLog(keyword string, requestId string) ([]byte, error) {
start := time.Now()
if requestId == "" {
requestId = uuid.New().String()
}
// Load OpenAI API configuration from Viper configuration
openaiUrl := viper.GetString("openai.url")
openaiModel := viper.GetString("openai.model")
openaiApiKey := viper.GetString("openai.api_key")
orgApiKey := viper.GetString("openai.org_key")
openaiSystem := viper.GetString("openai.system")
openaiTemp := viper.GetFloat64("openai.temp")
openaiMaxToken := viper.GetInt("openai.max_token")
// Construct the request body
requestBody := RequestBody{
Model: openaiModel,
Messages: []Message{
{
Role: "system",
Content: openaiSystem,
},
{
Role: "user",
Content: keyword,
},
},
MaxTokens: openaiMaxToken,
Temperature: float32(openaiTemp),
}
requestBodyJSON, err := json.Marshal(requestBody)
// Send the request to OpenAI API
req, err := http.NewRequest("POST", openaiUrl, strings.NewReader(string(requestBodyJSON)))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", openaiApiKey))
req.Header.Set("OpenAI-Organization", fmt.Sprintf("%s", orgApiKey))
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(resp.Body)
// Read the response body
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// Log the request and response
requestHeaders := fmt.Sprintf("%v", req.Header)
responseHeaders := fmt.Sprintf("%v", resp.Header)
userAgent := req.UserAgent()
ipAddress := getIpAddress(req)
err = logRequest(
req.Method,
req.URL.String(),
string(requestBodyJSON),
requestHeaders,
responseHeaders,
string(responseBody),
resp.StatusCode,
"",
userAgent,
ipAddress,
time.Since(start),
start,
time.Now(),
req.ContentLength,
resp.ContentLength,
requestId,
)
if err != nil {
return nil, err
}
return responseBody, nil
}
func logRequest(httpMethod string, requestUrl string, requestBody string, requestHeaders string, responseHeaders string, responseBody string, statusCode int, errorMessage string, userAgent string, ipAddress string, duration time.Duration, requestTimestamp time.Time, responseTimestamp time.Time, requestSize int64, responseSize int64, requestId string) error {
// Create a new Log struct with the provided data
log := Log{
HttpMethod: httpMethod,
RequestUrl: requestUrl,
RequestBody: requestBody,
RequestHeaders: requestHeaders,
ResponseHeaders: responseHeaders,
ResponseBody: responseBody,
StatusCode: statusCode,
ErrorMessage: errorMessage,
UserAgent: userAgent,
IPAddress: ipAddress,
Duration: duration,
RequestTimestamp: requestTimestamp,
ResponseTimestamp: responseTimestamp,
RequestSize: requestSize,
ResponseSize: responseSize,
RequestId: requestId,
}
// Save the Log struct to the database
result := dbConn.Create(&log)
if result.Error != nil {
return result.Error
}
return nil
}
func openAIHandler(w http.ResponseWriter, r *http.Request) {
// Parse the keyword from the request query string
keyword := r.URL.Query().Get("keyword")
requestId := r.URL.Query().Get("requestId")
fmt.Printf("Call openai, keyword:%s\n", keyword)
// Call the callOpenAIAndLog function with the keyword
response, err := callOpenAIAndLog(keyword, requestId)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Set the Content-Type header and write the response to the client
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(response)
if err != nil {
return
}
}