-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (81 loc) · 2.6 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Author: Vignesh Kumar
* Copyright: vigneshuvi
* Date: 01/07/2019
* This file contains the Main.
*/
package main
import (
"fmt"
"log"
"net/http"
"os"
"strings"
)
import user "./services/v1/user"
import services "./services"
import middleware "./middleware"
import config "./config"
import model "./config/model"
var defaultEnv = "DEV"
// Project Main function and selected environment
func main() {
start(false)
}
// Get the argument from run & test commend
func getArgument(env string) string {
argsWithProg := os.Args
var envName = env
if len(argsWithProg) > 1 {
envName = os.Args[1]
if strings.Contains(envName, "test") {
envName = env
}
}
return envName
}
// Get the Environment Config based on go run arguments
func getEnvironmentConfig(env string) model.ConfigModel {
envName := getArgument(env)
services.SetServiceEnv(envName)
log.Println("2.", envName, "- Set up the cofiguration.",)
config := config.SetConfig(envName)
log.Println("3.", config.Env.Title)
return config.Env
}
// Start the HTTP server and register service
func start(debug bool) {
log.Println("1. Application started..")
services.SetProfileStatus(false)
envConfig := getEnvironmentConfig(defaultEnv)
log.Println(envConfig)
registerWebService(envConfig)
startHTTPServer(debug, envConfig.HTTP.Port)
}
// Start the HtTP Server
func startHTTPServer(debug bool, port int) {
log.Println("5. Application running on port ", port)
if !debug {
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != http.ErrServerClosed {
log.Fatalf("Could not start server: %s\n", err.Error())
}
}
services.SetProfileStatus(true)
}
// Register Web services.
func registerWebService(envConfig model.ConfigModel) {
log.Println("4. Register Web Services!")
version := "/"+envConfig.Version
// Router Bypass Authentication
http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("./public"))))
rh := http.RedirectHandler("/", 307)
middleware.RegisterWSWithMiddleWareHandler("/home",false, rh)
//middleware.RegisterWSWithMiddleWare("/", false, services.Handler)
middleware.RegisterWSWithMiddleWare(version+"/u1v2i3C4o5n6f7i8g9", false, services.Handler)
middleware.RegisterWSWithMiddleWare(version+"/test", false, services.Handler)
middleware.RegisterWSWithMiddleWare(version+"/getProfile", false, services.GetProfileHandler)
middleware.RegisterWSWithMiddleWare(version+"/user", false, user.UserHandleRequest)
middleware.RegisterWSWithMiddleWare(version+"/user?", false, user.UserHandleRequest)
// Router Authentication
middleware.RegisterWSWithMiddleWare(version+"/AuthUser", true, user.Create)
}