Skip to content

Commit d1d4957

Browse files
committed
feat Read Configuration From Ini File
1 parent dba80a8 commit d1d4957

File tree

4 files changed

+59
-14
lines changed

4 files changed

+59
-14
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-

config.ini

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[server]
2+
address = 0.0.0.0
3+
port = 18080
4+
5+
[proxy]
6+
test1.example.com = http://localhost:8001
7+
test2.example.com = http://localhost:9000

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/rosercode/GoHTTPProxy
2+
3+
go 1.19
4+
5+
require github.com/go-ini/ini v1.67.0 // indirect

main.go

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,20 @@ import (
66
"net/http/httputil"
77
"net/url"
88
"strings"
9+
10+
"github.com/go-ini/ini"
911
)
1012

1113
// Define reverse proxy mapping table
12-
var proxyMap = map[string]*url.URL{
13-
"test1.example.com": parseURL("http://localhost:8000"), // Forward test1.example.com to local HTTP server (port 8000)
14-
"test2.example.com": parseURL("http://localhost:9000"), // Forward test2.example.com to local HTTP server (port 9000)
15-
}
14+
var proxyMap = make(map[string]*url.URL)
1615

1716
// Parse URL
18-
func parseURL(rawurl string) *url.URL {
17+
func parseURL(rawurl string) (*url.URL, error) {
1918
parsedURL, err := url.Parse(rawurl)
2019
if err != nil {
21-
log.Fatalf("Error parsing URL: %s", err)
20+
return nil, err
2221
}
23-
return parsedURL
22+
return parsedURL, nil
2423
}
2524

2625
// Custom reverse proxy handler
@@ -31,6 +30,11 @@ func (rph *reverseProxyHandler) ServeHTTP(w http.ResponseWriter, req *http.Reque
3130
hostParts := strings.Split(req.Host, ":")
3231
domain := hostParts[0]
3332

33+
// Print request log
34+
log.Println("Received request for domain:", domain)
35+
log.Println("Request URL:", req.URL.String())
36+
log.Println("Request Method:", req.Method)
37+
3438
// Look up the corresponding target URL in the proxy mapping table
3539
targetURL, ok := proxyMap[domain]
3640
if !ok {
@@ -56,24 +60,52 @@ func (rph *reverseProxyHandler) ServeHTTP(w http.ResponseWriter, req *http.Reque
5660
}
5761

5862
func main() {
59-
// Print the address and port that the server is listening on
60-
addr := ":18080"
61-
log.Printf("Reverse proxy server listening on %s...", addr)
63+
// Load Configuration File
64+
cfg, err := ini.Load("config.ini")
65+
if err != nil {
66+
log.Fatalf("Failed to load configuration file: %v", err)
67+
return
68+
}
6269

63-
// Print the reverse proxy mapping table
70+
// Parse Reverse Proxy Mapping Table from Configuration File
71+
section := cfg.Section("proxy")
72+
if section == nil {
73+
log.Fatal("Proxy section not found in configuration file")
74+
return
75+
}
76+
77+
// Iterate through Configuration Entries and Parse URLs
6478
log.Println("Reverse proxy mapping table:")
65-
for domain, targetURL := range proxyMap {
66-
log.Printf("%s => %s", domain, targetURL.String())
79+
for _, key := range section.Keys() {
80+
targetURL, err := parseURL(key.String())
81+
// Print the reverse proxy mapping table
82+
log.Printf("%s => %s", key.Name(), targetURL.String())
83+
if err != nil {
84+
log.Fatalf("Failed to parse URL for key %s: %v", key, err)
85+
}
86+
proxyMap[key.Name()] = targetURL
6787
}
6888

89+
// Read Server Configuration
90+
serverSection := cfg.Section("server")
91+
if serverSection == nil {
92+
log.Fatal("Server section not found in configuration file")
93+
return
94+
}
95+
96+
address := serverSection.Key("address").String()
97+
port := serverSection.Key("port").String()
98+
6999
// Create an instance of the custom reverse proxy handler
70100
handler := &reverseProxyHandler{}
71101

72102
// Register the reverse proxy handler
73103
http.Handle("/", handler)
74104

75105
// Start the server
76-
err := http.ListenAndServe(addr, nil)
106+
addr := address + ":" + port
107+
log.Printf("Reverse proxy server listening on %s...", addr)
108+
err = http.ListenAndServe(addr, nil)
77109
if err != nil {
78110
log.Fatal(err)
79111
}

0 commit comments

Comments
 (0)