-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
49 lines (40 loc) · 1.32 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/agungdwiprasetyo/reverse-proxy/config"
"github.com/agungdwiprasetyo/reverse-proxy/helper"
"github.com/agungdwiprasetyo/reverse-proxy/middleware"
"github.com/agungdwiprasetyo/reverse-proxy/src/handler"
"github.com/agungdwiprasetyo/reverse-proxy/src/proxy"
)
func main() {
appPath, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(helper.StringRed(err))
}
// init config
conf := config.Init(appPath)
// register all service proxy from config
for _, service := range conf.Services {
service.Root = fmt.Sprintf("/%s/", strings.Trim(service.Root, "/"))
prx := proxy.NewProxy(service.Root, service.Host)
http.HandleFunc(service.Root, prx.Handle)
fmt.Fprintf(os.Stdout, "%s[GATEWAY]%s %s %s %s\n",
helper.White, helper.Reset, helper.StringRed(fmt.Sprintf(":%d%s", conf.GatewayPort, service.Root)),
helper.StringYellow("|===>"), helper.StringGreen(service.Host),
)
}
// Handle root gateway
root := middleware.BasicAuth(handler.Root, conf.Key.Username, conf.Key.Password)
http.HandleFunc("/", middleware.Logger(root))
port := fmt.Sprintf(":%d", conf.GatewayPort)
log.Println("Server running on port", port)
if err := http.ListenAndServe(port, nil); err != nil {
log.Fatal(helper.StringRed(err))
}
}