1
1
package server
2
2
3
3
import (
4
+ "fmt"
4
5
"github.com/thomiceli/opengist/internal/validator"
5
6
"net"
6
7
"net/http"
7
8
"os"
9
+ "path/filepath"
10
+ "strconv"
11
+ "strings"
8
12
9
13
"github.com/labstack/echo/v4"
10
14
"github.com/rs/zerolog/log"
@@ -47,7 +51,19 @@ func NewServer(isDev bool, sessionsPath string, ignoreCsrf bool) *Server {
47
51
return s
48
52
}
49
53
54
+ func isSocketPath (host string ) bool {
55
+ return strings .Contains (host , "/" ) || strings .Contains (host , "\\ " )
56
+ }
57
+
50
58
func (s * Server ) Start () {
59
+ if isSocketPath (config .C .HttpHost ) {
60
+ s .startUnixSocket ()
61
+ } else {
62
+ s .startHTTP ()
63
+ }
64
+ }
65
+
66
+ func (s * Server ) startHTTP () {
51
67
addr := config .C .HttpHost + ":" + config .C .HttpPort
52
68
53
69
log .Info ().Msg ("Starting HTTP server on http://" + addr )
@@ -56,19 +72,44 @@ func (s *Server) Start() {
56
72
}
57
73
}
58
74
59
- func (s * Server ) StartUnixSocket () {
60
- socketPath := "/tmp/opengist.sock"
75
+ func (s * Server ) startUnixSocket () {
76
+ socketPath := config .C .HttpHost
77
+ if socketPath == "" {
78
+ socketPath = "/tmp/opengist.sock"
79
+ }
80
+
81
+ if dir := filepath .Dir (socketPath ); dir != "." {
82
+ if err := os .MkdirAll (dir , 0755 ); err != nil {
83
+ log .Warn ().Err (err ).Str ("dir" , dir ).Msg ("Failed to create socket directory" )
84
+ }
85
+ }
61
86
if err := os .Remove (socketPath ); err != nil && ! os .IsNotExist (err ) {
62
87
log .Warn ().Err (err ).Str ("socket" , socketPath ).Msg ("Failed to remove existing socket file" )
63
88
}
64
89
90
+ pidPath := strings .TrimSuffix (socketPath , filepath .Ext (socketPath )) + ".pid"
91
+ if err := s .createPidFile (pidPath ); err != nil {
92
+ log .Warn ().Err (err ).Str ("pid-file" , pidPath ).Msg ("Failed to create PID file" )
93
+ }
94
+
65
95
listener , err := net .Listen ("unix" , socketPath )
66
96
if err != nil {
67
97
log .Fatal ().Err (err ).Msg ("Failed to start Unix socket server" )
68
98
}
69
99
s .echo .Listener = listener
70
100
71
- log .Info ().Msgf ("Starting Unix socket server on " + socketPath )
101
+ if config .C .UnixSocketPermissions != "" {
102
+ if perm , err := strconv .ParseUint (config .C .UnixSocketPermissions , 8 , 32 ); err == nil {
103
+ if err := os .Chmod (socketPath , os .FileMode (perm )); err != nil {
104
+ log .Warn ().Err (err ).Str ("socket" , socketPath ).Str ("permissions" , config .C .UnixSocketPermissions ).Msg ("Failed to set socket permissions" )
105
+ }
106
+ } else {
107
+ log .Warn ().Err (err ).Str ("permissions" , config .C .UnixSocketPermissions ).Msg ("Invalid socket permissions format" )
108
+ }
109
+ }
110
+
111
+ log .Info ().Str ("socket" , socketPath ).Msg ("Starting Unix socket server" )
112
+ log .Info ().Str ("pid-file" , pidPath ).Msg ("PID file created" )
72
113
server := new (http.Server )
73
114
if err := s .echo .StartServer (server ); err != nil && err != http .ErrServerClosed {
74
115
log .Fatal ().Err (err ).Msg ("Failed to start Unix socket server" )
@@ -101,9 +142,27 @@ func (s *Server) StopUnixSocket() {
101
142
} else {
102
143
log .Info ().Str ("socket" , socketPath ).Msg ("Socket file removed" )
103
144
}
145
+
146
+ pidPath := strings .TrimSuffix (socketPath , filepath .Ext (socketPath )) + ".pid"
147
+ if err := os .Remove (pidPath ); err != nil && ! os .IsNotExist (err ) {
148
+ log .Error ().Err (err ).Str ("pid-file" , pidPath ).Msg ("Failed to remove PID file" )
149
+ } else {
150
+ log .Info ().Str ("pid-file" , pidPath ).Msg ("PID file removed" )
151
+ }
104
152
}
105
153
}
106
154
155
+ func (s * Server ) createPidFile (pidPath string ) error {
156
+ pid := os .Getpid ()
157
+ pidContent := fmt .Sprintf ("%d\n " , pid )
158
+
159
+ if err := os .WriteFile (pidPath , []byte (pidContent ), 0644 ); err != nil {
160
+ return err
161
+ }
162
+
163
+ return nil
164
+ }
165
+
107
166
func (s * Server ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
108
167
s .echo .ServeHTTP (w , r )
109
168
}
0 commit comments