Skip to content

Commit

Permalink
fix: 环境变量名称
Browse files Browse the repository at this point in the history
  • Loading branch information
lenye committed Sep 12, 2023
1 parent 5deb38e commit dac3025
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 41 deletions.
58 changes: 58 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2023 The chatgpt_reverse_proxy Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package config

import (
"fmt"
"log/slog"
"os"
"strconv"

"github.com/lenye/chatgpt_reverse_proxy/internal/env"
)

var (
Target = "https://api.openai.com"
Port = "9000"
)

func Read() {
// Target
if val, ok := os.LookupEnv(env.Target); ok {
if val != "" {
Target = val
}
}
slog.Info(fmt.Sprintf("target: %s", Target))

// Port
if val, ok := os.LookupEnv(env.Port); ok {
if val != "" {
uInt, err := strconv.ParseUint(val, 10, 0)
if err != nil {
slog.Error(fmt.Sprintf("invalid %s=%s", env.Port, val), "error", err)
os.Exit(1)
return
}
if uInt > 65535 {
slog.Error(fmt.Sprintf("invalid %s=%s, max=65535", env.Port, val))
os.Exit(1)
return
}
Port = val
}
}
slog.Info(fmt.Sprintf("serve on port %s", Port))
}
9 changes: 7 additions & 2 deletions internal/target/url.go → internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package target
package env

var Url = "https://api.openai.com"
const Prefix = "OXY_"

const (
Port = Prefix + "PORT"
Target = Prefix + "TARGET"
)
59 changes: 20 additions & 39 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@ package main

import (
"context"
"errors"
"flag"
"fmt"
"log"
"log/slog"
"net/http"
"net/url"
"os"
"os/signal"
"syscall"
"time"
"unicode"

"github.com/lenye/chatgpt_reverse_proxy/internal/target"
"github.com/lenye/chatgpt_reverse_proxy/internal/config"
"github.com/lenye/chatgpt_reverse_proxy/internal/env"
"github.com/lenye/chatgpt_reverse_proxy/pkg/proxy"
"github.com/lenye/chatgpt_reverse_proxy/pkg/version"
)
Expand All @@ -42,68 +43,48 @@ func main() {
fmt.Print(version.Print())
return
}
start := time.Now()

if envTarget, ok := os.LookupEnv("REVERSE_PROXY_TARGET"); ok {
if envTarget != "" {
target.Url = envTarget
}
}
log.Printf("reverse proxy target: %s", target.Url)
config.Read()

oxyTarget, err := url.Parse(target.Url)
// Target
oxyTarget, err := url.Parse(config.Target)
if err != nil {
log.Fatalf("invalid REVERSE_PROXY_TARGET: %s")
slog.Error(fmt.Sprintf("invalid %s=%s", env.Target, config.Target), "error", err)
os.Exit(1)
return
}
oxy := proxy.BuildSingleHostProxy(oxyTarget)

port := "9000"
if envPort, ok := os.LookupEnv("REVERSE_PROXY_PORT"); ok {
if envPort != "" {
if !isDigit(envPort) {
log.Fatalf("invalid REVERSE_PROXY_PORT: %s", envPort)
}
port = envPort
}
}
log.Printf("reverse proxy serve on port %s", port)

srv := &http.Server{Addr: ":" + port, Handler: oxy}
srv := &http.Server{Addr: ":" + config.Port, Handler: oxy}

idleConnClosed := make(chan struct{})

go func() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, syscall.SIGINT, syscall.SIGTERM)
s := <-sigint
log.Printf("received signal: %v", s)
slog.Debug(fmt.Sprintf("received signal: %v", s))

// We received an interrupt signal, shut down.
if err := srv.Shutdown(context.Background()); err != nil {
// Error from closing listeners, or context timeout:
log.Printf("server shutdown failed: %v", err)
slog.Error("server shutdown failed", "error", err)
}
close(idleConnClosed)
}()

if err := srv.ListenAndServe(); err != nil {
if err == http.ErrServerClosed {
log.Println("server closed")
if errors.Is(err, http.ErrServerClosed) {
slog.Info("server closed")
} else {
log.Printf("server failed, err: %v", err)
slog.Error("server failed", "error", err)
}
}

<-idleConnClosed

log.Printf("%s exit, start: %s, uptime: %s",
version.AppName, version.StartTime, time.Since(version.StartTime))
}

func isDigit(s string) bool {
for _, c := range s {
if !unicode.IsDigit(c) {
return false
}
}
return true
slog.Info(version.AppName+" exit",
"start", start, "uptime", time.Since(start),
)
}

0 comments on commit dac3025

Please sign in to comment.