Skip to content

Commit 7d20a01

Browse files
committed
feat!: support listen to the unix (close AlistGo#4671)
Starting from this commit, the HTTP server related config all move to the scheme
1 parent 59dbf44 commit 7d20a01

4 files changed

Lines changed: 53 additions & 24 deletions

File tree

cmd/server.go

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"context"
55
"fmt"
6+
"net"
67
"net/http"
78
"os"
89
"os/signal"
@@ -42,26 +43,40 @@ the address is defined in config file`,
4243
r := gin.New()
4344
r.Use(gin.LoggerWithWriter(log.StandardLogger().Out), gin.RecoveryWithWriter(log.StandardLogger().Out))
4445
server.Init(r)
45-
var httpSrv, httpsSrv *http.Server
46-
if !conf.Conf.Scheme.DisableHttp {
47-
httpBase := fmt.Sprintf("%s:%d", conf.Conf.Address, conf.Conf.Port)
46+
var httpSrv, httpsSrv, unixSrv *http.Server
47+
if conf.Conf.Scheme.HttpPort != -1 {
48+
httpBase := fmt.Sprintf("%s:%d", conf.Conf.Scheme.Address, conf.Conf.Scheme.HttpPort)
4849
utils.Log.Infof("start HTTP server @ %s", httpBase)
4950
httpSrv = &http.Server{Addr: httpBase, Handler: r}
5051
go func() {
5152
err := httpSrv.ListenAndServe()
5253
if err != nil && err != http.ErrServerClosed {
53-
utils.Log.Fatalf("failed to start: %s", err.Error())
54+
utils.Log.Fatalf("failed to start http: %s", err.Error())
5455
}
5556
}()
5657
}
57-
if conf.Conf.Scheme.Https {
58-
httpsBase := fmt.Sprintf("%s:%d", conf.Conf.Address, conf.Conf.HttpsPort)
58+
if conf.Conf.Scheme.HttpsPort != -1 {
59+
httpsBase := fmt.Sprintf("%s:%d", conf.Conf.Scheme.Address, conf.Conf.Scheme.HttpPort)
5960
utils.Log.Infof("start HTTPS server @ %s", httpsBase)
6061
httpsSrv = &http.Server{Addr: httpsBase, Handler: r}
6162
go func() {
6263
err := httpsSrv.ListenAndServeTLS(conf.Conf.Scheme.CertFile, conf.Conf.Scheme.KeyFile)
6364
if err != nil && err != http.ErrServerClosed {
64-
utils.Log.Fatalf("failed to start: %s", err.Error())
65+
utils.Log.Fatalf("failed to start https: %s", err.Error())
66+
}
67+
}()
68+
}
69+
if conf.Conf.Scheme.UnixFile != "" {
70+
utils.Log.Infof("start unix server @ %s", conf.Conf.Scheme.UnixFile)
71+
unixSrv = &http.Server{Handler: r}
72+
go func() {
73+
listener, err := net.Listen("unix", conf.Conf.Scheme.UnixFile)
74+
if err != nil {
75+
utils.Log.Fatalf("failed to listen unix: %+v", err)
76+
}
77+
err = unixSrv.Serve(listener)
78+
if err != nil && err != http.ErrServerClosed {
79+
utils.Log.Fatalf("failed to start unix: %s", err.Error())
6580
}
6681
}()
6782
}
@@ -78,21 +93,30 @@ the address is defined in config file`,
7893
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
7994
defer cancel()
8095
var wg sync.WaitGroup
81-
if !conf.Conf.Scheme.DisableHttp {
96+
if conf.Conf.Scheme.HttpPort != -1 {
8297
wg.Add(1)
8398
go func() {
8499
defer wg.Done()
85100
if err := httpSrv.Shutdown(ctx); err != nil {
86-
utils.Log.Fatal("HTTP server shutdown:", err)
101+
utils.Log.Fatal("HTTP server shutdown err: ", err)
87102
}
88103
}()
89104
}
90-
if conf.Conf.Scheme.Https {
105+
if conf.Conf.Scheme.HttpsPort != -1 {
91106
wg.Add(1)
92107
go func() {
93108
defer wg.Done()
94109
if err := httpsSrv.Shutdown(ctx); err != nil {
95-
utils.Log.Fatal("HTTPS server shutdown:", err)
110+
utils.Log.Fatal("HTTPS server shutdown err: ", err)
111+
}
112+
}()
113+
}
114+
if conf.Conf.Scheme.UnixFile != "" {
115+
wg.Add(1)
116+
go func() {
117+
defer wg.Done()
118+
if err := unixSrv.Shutdown(ctx); err != nil {
119+
utils.Log.Fatal("Unix server shutdown err: ", err)
96120
}
97121
}()
98122
}

internal/conf/config.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ type Database struct {
2020
}
2121

2222
type Scheme struct {
23-
DisableHttp bool `json:"disable_http" env:"DISABLE_HTTP"`
24-
Https bool `json:"https" env:"HTTPS"`
25-
ForceHttps bool `json:"force_https" env:"FORCE_HTTPS"`
26-
CertFile string `json:"cert_file" env:"CERT_FILE"`
27-
KeyFile string `json:"key_file" env:"KEY_FILE"`
23+
Address string `json:"address" env:"ADDR"`
24+
HttpPort int `json:"http_port" env:"HTTP_PORT"`
25+
HttpsPort int `json:"https_port" env:"HTTPS_PORT"`
26+
ForceHttps bool `json:"force_https" env:"FORCE_HTTPS"`
27+
CertFile string `json:"cert_file" env:"CERT_FILE"`
28+
KeyFile string `json:"key_file" env:"KEY_FILE"`
29+
UnixFile string `json:"unix_file" env:"UNIX_FILE"`
2830
}
2931

3032
type LogConfig struct {
@@ -38,9 +40,6 @@ type LogConfig struct {
3840

3941
type Config struct {
4042
Force bool `json:"force" env:"FORCE"`
41-
Address string `json:"address" env:"ADDR"`
42-
Port int `json:"port" env:"PORT"`
43-
HttpsPort int `json:"https_port" env:"HTTPS_PORT"`
4443
SiteURL string `json:"site_url" env:"SITE_URL"`
4544
Cdn string `json:"cdn" env:"CDN"`
4645
JwtSecret string `json:"jwt_secret" env:"JWT_SECRET"`
@@ -61,9 +60,15 @@ func DefaultConfig() *Config {
6160
logPath := filepath.Join(flags.DataDir, "log/log.log")
6261
dbPath := filepath.Join(flags.DataDir, "data.db")
6362
return &Config{
64-
Address: "0.0.0.0",
65-
Port: 5244,
66-
HttpsPort: 5245,
63+
Scheme: Scheme{
64+
Address: "0.0.0.0",
65+
UnixFile: "",
66+
HttpPort: 5244,
67+
HttpsPort: -1,
68+
ForceHttps: false,
69+
CertFile: "",
70+
KeyFile: "",
71+
},
6772
JwtSecret: random.String(16),
6873
TokenExpiresIn: 48,
6974
TempDir: tempDir,

server/middlewares/https.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func ForceHttps(c *gin.Context) {
1212
if c.Request.TLS == nil {
1313
host := c.Request.Host
1414
// change port to https port
15-
host = strings.Replace(host, fmt.Sprintf(":%d", conf.Conf.Port), fmt.Sprintf(":%d", conf.Conf.HttpsPort), 1)
15+
host = strings.Replace(host, fmt.Sprintf(":%d", conf.Conf.Scheme.HttpPort), fmt.Sprintf(":%d", conf.Conf.Scheme.HttpsPort), 1)
1616
c.Redirect(302, "https://"+host+c.Request.RequestURI)
1717
c.Abort()
1818
return

server/router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func Init(e *gin.Engine) {
2121
}
2222
Cors(e)
2323
g := e.Group(conf.URL.Path)
24-
if conf.Conf.Scheme.Https && conf.Conf.Scheme.ForceHttps && !conf.Conf.Scheme.DisableHttp {
24+
if conf.Conf.Scheme.HttpPort != -1 && conf.Conf.Scheme.HttpsPort != -1 && conf.Conf.Scheme.ForceHttps {
2525
g.Use(middlewares.ForceHttps)
2626
}
2727
g.Any("/ping", func(c *gin.Context) {

0 commit comments

Comments
 (0)