From f1a2a53310cb1c9afb6369d3aff6af3958ec128a Mon Sep 17 00:00:00 2001 From: LV Mingze <18231950177@163.com> Date: Mon, 9 Dec 2024 22:05:11 +0800 Subject: [PATCH] Update Entry Point --- Entry Point | 122 ++++++++++++++++++++++++++-------------------------- 1 file changed, 61 insertions(+), 61 deletions(-) diff --git a/Entry Point b/Entry Point index 265b7cb..a8ce6ac 100644 --- a/Entry Point +++ b/Entry Point @@ -2,89 +2,89 @@ package main import ( - "log" - "net/http" - "net/http/httputil" - "net/url" + "log" + "net/http" + "net/http/httputil" + "net/url" ) // feat: Define configuration struct type Config struct { - ListenAddr string - TargetURL string - AuthServiceURL string + ListenAddr string + TargetURL string + AuthServiceURL string } // feat: Define entry point struct with core dependencies type EntryPoint struct { - config *Config - proxy *httputil.ReverseProxy - authClient *http.Client + config *Config + proxy *httputil.ReverseProxy + authClient *http.Client } // feat: Implement EntryPoint constructor func NewEntryPoint(config *Config) (*EntryPoint, error) { - targetURL, err := url.Parse(config.TargetURL) - if err != nil { - return nil, err + targetURL, err := url.Parse(config.TargetURL) + if err != nil { + return nil, err + } + + return &EntryPoint{ + config: config, + proxy: httputil.NewSingleHostReverseProxy(targetURL), + authClient: &http.Client{}, + }, nil } - - return &EntryPoint{ - config: config, - proxy: httputil.NewSingleHostReverseProxy(targetURL), - authClient: &http.Client{}, - }, nil - } // feat: Add authentication verification method func (ep *EntryPoint) verifyAuth(token string) (bool, error) { - req, err := http.NewRequest("POST", ep.config.AuthServiceURL, nil) - if err != nil { - return false, err - } - - req.Header.Set("Authorization", token) - resp, err := ep.authClient.Do(req) - if err != nil { - return false, err + req, err := http.NewRequest("POST", ep.config.AuthServiceURL, nil) + if err != nil { + return false, err + } + + req.Header.Set("Authorization", token) + resp, err := ep.authClient.Do(req) + if err != nil { + return false, err + } + defer resp.Body.Close() + + return resp.StatusCode == http.StatusOK, nil } - defer resp.Body.Close() - - return resp.StatusCode == http.StatusOK, nil - } // feat: Implement proxy handler with authentication func (ep *EntryPoint) ProxyHandler(w http.ResponseWriter, r *http.Request) { - token := r.Header.Get("Authorization") - - authorized, err := ep.verifyAuth(token) - if err != nil { - http.Error(w, "Authorization service error", http.StatusInternalServerError) - return - } - - if !authorized { - http.Error(w, "Unauthorized", http.StatusUnauthorized) - return + token := r.Header.Get("Authorization") + + authorized, err := ep.verifyAuth(token) + if err != nil { + http.Error(w, "Authorization service error", http.StatusInternalServerError) + return + } + + if !authorized { + http.Error(w, "Unauthorized", http.StatusUnauthorized) + return + } + + ep.proxy.ServeHTTP(w, r) } - - ep.proxy.ServeHTTP(w, r) - } // feat: Set up main function with configuration and server initialization func main() { - config := &Config{ - ListenAddr: ":8080", - TargetURL: "http://target-service:8000", - AuthServiceURL: "http://auth-service:8001/verify", - } - - entryPoint, err := NewEntryPoint(config) - if err != nil { - log.Fatal(err) + config := &Config{ + ListenAddr: ":8080", + TargetURL: "http://target-service:8000", + AuthServiceURL: "http://auth-service:8001/verify", + } + + entryPoint, err := NewEntryPoint(config) + if err != nil { + log.Fatal(err) + } + + http.HandleFunc("/", entryPoint.ProxyHandler) + log.Printf("Starting entry point on %s", config.ListenAddr) + log.Fatal(http.ListenAndServe(config.ListenAddr, nil)) } - - http.HandleFunc("/", entryPoint.ProxyHandler) - log.Printf("Starting entry point on %s", config.ListenAddr) - log.Fatal(http.ListenAndServe(config.ListenAddr, nil)) - }