Skip to content

Commit 6e47f8b

Browse files
authored
Merge pull request #231 from smsohan/optional-api-key
makes API_KEY optional
2 parents 3f15a9b + 90afb10 commit 6e47f8b

File tree

1 file changed

+4
-15
lines changed

1 file changed

+4
-15
lines changed

Demos/Gemma-on-Cloudrun/proxy.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,9 @@ func modifyStreamResponse(resp *http.Response) error {
7272
return nil
7373
}
7474

75-
func getApiKey() (string, error) {
76-
apiKey := os.Getenv("API_KEY")
77-
if apiKey == "" {
78-
return "", fmt.Errorf("environment variable 'API_KEY' is required")
79-
}
80-
return apiKey, nil
81-
}
82-
8375
func main() {
8476
// --- Configuration ---
85-
envApiKey, err := getApiKey()
86-
if err != nil {
87-
log.Fatalf("Error reading API_KEY env var: %v", err)
88-
}
77+
envApiKey := os.Getenv("API_KEY")
8978

9079
port := os.Getenv("PORT")
9180
targetURLStr := os.Getenv("OLLAMA_HOST")
@@ -111,7 +100,7 @@ func main() {
111100
matches := re.FindStringSubmatch(r.URL.Path)
112101

113102
var (
114-
isCurrentRequestGeminiStyle bool = false
103+
isCurrentRequestGeminiStyle bool = false
115104
actionForResponse string // Stores action for response modification
116105
)
117106

@@ -141,7 +130,7 @@ func main() {
141130
if requestApiKey == "" {
142131
requestApiKey = queryParams.Get("key")
143132
}
144-
if requestApiKey != envApiKey {
133+
if envApiKey != "" && requestApiKey != envApiKey {
145134
log.Printf("Invalid API key provided in the request.")
146135
http.Error(w, "Permission denied. Invalid API Key. Configure your SDK to use this URL, and use the API key provided at deployment (https://github.com/google-gemini/gemma-cookbook/blob/main/Demos/Gemma-on-Cloudrun/README.md)", http.StatusForbidden)
147136
return
@@ -191,7 +180,7 @@ func main() {
191180
apiKey = r.URL.Query().Get("key")
192181
expectedPrefix = ""
193182
}
194-
if apiKey != expectedPrefix + envApiKey {
183+
if envApiKey != "" && apiKey != expectedPrefix+envApiKey {
195184
log.Printf("Invalid API key provided in the request.")
196185
http.Error(w, "Permission denied. Invalid API Key. Configure your SDK to use this URL, and use the API key provided at deployment (https://github.com/google-gemini/gemma-cookbook/blob/main/Demos/Gemma-on-Cloudrun/README.md)", http.StatusForbidden)
197186
return

0 commit comments

Comments
 (0)