Merge Go frontend (apiserver + xcecli) into meson build
This commit is contained in:
parent
43dc12a36c
commit
c9dc8c4676
205 changed files with 30793 additions and 328 deletions
121
src/frontend/apiserver/middleware/startserver.go
Normal file
121
src/frontend/apiserver/middleware/startserver.go
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"apiserver/utils"
|
||||
logger "apiserver/utils"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout server.key -out server.crt -config openssl.cnf -extensions v3_req
|
||||
func StartServer(r *gin.Engine) error {
|
||||
logger.Debug("Enter StartServer() function")
|
||||
|
||||
if runtime.GOOS != "windows" {
|
||||
currentUser, err := user.Current()
|
||||
if err != nil {
|
||||
logger.Error("Failed to get current user:", zap.Error(err))
|
||||
os.Exit(1)
|
||||
}
|
||||
if currentUser.Uid != "0" {
|
||||
logger.Warn("Not running as root — PAM authentication will not work. OK for dev/testing.")
|
||||
}
|
||||
}
|
||||
|
||||
ips, err := GetIPAddresses()
|
||||
if err != nil {
|
||||
logger.Error("Failed to get server IP address:", zap.Error(err))
|
||||
os.Exit(1)
|
||||
}
|
||||
ip := ips[0]
|
||||
logger.Debug("API Server IP address:", zap.String("ip", ip))
|
||||
|
||||
httpsConfig := utils.HttpsYamlConfig //get only once
|
||||
if httpsConfig.Enabled {
|
||||
addr := ":" + httpsConfig.Port
|
||||
logger.Info("Starting HTTPS server on " + addr)
|
||||
|
||||
fmt.Println("[GIN-debug] The API server will be run on https://" + ip + addr)
|
||||
if err := r.RunTLS(
|
||||
addr,
|
||||
filepath.Join(utils.Global_TopDir, httpsConfig.CertFile),
|
||||
filepath.Join(utils.Global_TopDir, httpsConfig.KeyFile),
|
||||
); err != nil {
|
||||
logger.Error("HTTPS Server fails to start:", zap.Error(err))
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
addr := ":" + utils.AppYamlConfig.HTTPPort
|
||||
logger.Info("Starting HTTP server on " + addr)
|
||||
|
||||
fmt.Println("[GIN-debug] The API server will be run on http://" + ip + addr)
|
||||
if err := r.Run(addr); err != nil {
|
||||
logger.Error("API Server fails to start:", zap.Error(err))
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getServerIP() (string, error) {
|
||||
// get the server IP address
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
logger.Error("Failed to get server IP address:", zap.Error(err))
|
||||
return "", err
|
||||
}
|
||||
|
||||
for _, addr := range addrs {
|
||||
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && ipnet.IP.To4() != nil {
|
||||
return ipnet.IP.String(), nil // real ip address
|
||||
}
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func GetIPAddresses() ([]string, error) {
|
||||
var ips []string
|
||||
|
||||
interfaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, iface := range interfaces {
|
||||
if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
|
||||
// Skip down or loopback interfaces
|
||||
continue
|
||||
}
|
||||
|
||||
addrs, err := iface.Addrs()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, addr := range addrs {
|
||||
var ip net.IP
|
||||
switch v := addr.(type) {
|
||||
case *net.IPNet:
|
||||
ip = v.IP
|
||||
case *net.IPAddr:
|
||||
ip = v.IP
|
||||
}
|
||||
|
||||
// Skip IPv6 link-local or loopback addresses
|
||||
if ip == nil || ip.IsLoopback() || ip.IsLinkLocalUnicast() {
|
||||
continue
|
||||
}
|
||||
|
||||
ips = append(ips, ip.String())
|
||||
}
|
||||
}
|
||||
|
||||
return ips, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue