-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
139 lines (112 loc) · 3.37 KB
/
main.go
File metadata and controls
139 lines (112 loc) · 3.37 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"embed"
"flag"
"fmt"
"io"
"io/fs"
"net"
"net/http"
"strings"
"os"
"runtime"
"strconv"
"templrjs/pkg/router"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/natefinch/lumberjack"
log "github.com/sirupsen/logrus"
)
var (
stage string
flagPort int
flagSource string
flagEnv string
flagStaticDirPath string
flagIndexFileName string
)
func init() {
log.Info("Application init function start")
log.Info("Initialize Logger")
initLogger()
log.Info("Initialized Logger")
log.Info("Initialize command line args")
flag.IntVar(&flagPort, "p", 8080, "port number for the api server")
flag.StringVar(&flagStaticDirPath, "d", "./.dist", "Website directory path") // Updated to reflect .dist directory
flag.StringVar(&flagIndexFileName, "f", "index.html", "Index document")
flag.StringVar(&flagEnv, "e", "dev", "Development or Production stage")
flag.StringVar(&flagSource, "s", "embed", "Host site from embedded source or local filesystem")
log.Info("Initialized command line args")
log.Info("Application init function end.")
}
//go:embed all:.dist
var content embed.FS
func main() {
flag.Parse()
startServer()
}
func initLogger() {
logfilepath := AppExecutionPath() + "/" + os.Args[0] + ".log"
log.Info("logfilepath = " + logfilepath)
ljack := &lumberjack.Logger{
Filename: logfilepath,
MaxSize: 1,
MaxBackups: 3,
MaxAge: 3,
LocalTime: true,
}
log.SetLevel(log.InfoLevel)
mWriter := io.MultiWriter(os.Stdout, ljack)
log.SetOutput(mWriter)
log.SetReportCaller(true)
log.SetFormatter(&log.JSONFormatter{})
log.WithFields(log.Fields{
"app": os.Args[0],
"Runtime Version": runtime.Version(),
"Number of CPUs": runtime.NumCPU(),
"Arch": runtime.GOARCH,
}).Info("Application Initializing")
}
func Assets() (fs.FS, error) {
return fs.Sub(content, ".dist") // Updated to match .dist directory
}
func startServer() {
r := router.Setup()
config := cors.DefaultConfig()
config.AllowOrigins = []string{"*", "http://localhost:3000"}
config.AllowHeaders = []string{"Origin", "Content-Type"}
r.Use(cors.New(config))
if strings.ToLower(flagSource) == "embed" {
assets, _ := Assets()
r.Use(func(c *gin.Context) {
fs := http.StripPrefix("/", http.FileServer(http.FS(assets)))
fs.ServeHTTP(c.Writer, c.Request)
c.Abort()
})
} else {
log.Info("Serving from local filesystem")
r.GET("/", func(c *gin.Context) { c.File("./.dist/index.html") }) // Updated paths to match .dist directory
r.GET("/sql-ide", func(c *gin.Context) { c.File("./.dist/sql-ide/index.html") }) // Updated paths
r.Static("/_nuxt", "./.dist/_nuxt") // Updated paths
r.StaticFile("/favicon.ico", "./.dist/favicon.ico") // Updated paths
r.StaticFile("/logo.svg", "./.dist/logo.svg") // Updated paths
}
listener, err := net.Listen("tcp", ":"+strconv.Itoa(flagPort))
if err != nil {
log.Error(err)
log.Fatal(err)
}
log.Info("Starting the server " + strconv.Itoa(flagPort))
done := make(chan bool)
go http.Serve(listener, r)
log.Info("Server started at port " + strconv.Itoa(flagPort))
<-done
}
func AppExecutionPath() string {
mydir, err := os.Getwd()
if err != nil {
fmt.Println(err)
}
fmt.Println("App path = " + mydir)
return mydir
}