|
|
@ -26,12 +26,13 @@ import ( |
|
|
|
|
|
|
|
|
|
|
|
"unbewohnte/gochat/api" |
|
|
|
"unbewohnte/gochat/api" |
|
|
|
"unbewohnte/gochat/log" |
|
|
|
"unbewohnte/gochat/log" |
|
|
|
"unbewohnte/gochat/page" |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// Server structure that glues api logic and http/websocket server together
|
|
|
|
// Server structure that glues api logic and http/websocket server together
|
|
|
|
type Server struct { |
|
|
|
type Server struct { |
|
|
|
workingDir string |
|
|
|
workingDir string |
|
|
|
|
|
|
|
keyFile string |
|
|
|
|
|
|
|
certFile string |
|
|
|
http *http.Server |
|
|
|
http *http.Server |
|
|
|
db *api.DB |
|
|
|
db *api.DB |
|
|
|
websockets *api.WSHolder |
|
|
|
websockets *api.WSHolder |
|
|
@ -46,8 +47,10 @@ const ( |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// Create a new configured and ready-to-launch server
|
|
|
|
// Create a new configured and ready-to-launch server
|
|
|
|
func New(workingDir string, dbPath string, port uint) (*Server, error) { |
|
|
|
func New(workingDir string, dbPath string, port uint, keyFile string, certFile string) (*Server, error) { |
|
|
|
var server = Server{ |
|
|
|
var server = Server{ |
|
|
|
|
|
|
|
keyFile: keyFile, |
|
|
|
|
|
|
|
certFile: certFile, |
|
|
|
workingDir: workingDir, |
|
|
|
workingDir: workingDir, |
|
|
|
websockets: &api.WSHolder{}, |
|
|
|
websockets: &api.WSHolder{}, |
|
|
|
incomingMessages: make(chan api.Message), |
|
|
|
incomingMessages: make(chan api.Message), |
|
|
@ -85,7 +88,7 @@ func New(workingDir string, dbPath string, port uint) (*Server, error) { |
|
|
|
serveMux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { |
|
|
|
serveMux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { |
|
|
|
switch req.URL.Path { |
|
|
|
switch req.URL.Path { |
|
|
|
case "/": |
|
|
|
case "/": |
|
|
|
requestedPage, err := page.Get(pagesDirPath, "base.html", "index.html") |
|
|
|
requestedPage, err := GetPage(pagesDirPath, "base.html", "index.html") |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Error("error getting page on route %s: %s", req.URL.Path, err) |
|
|
|
log.Error("error getting page on route %s: %s", req.URL.Path, err) |
|
|
|
http.Error(w, "internal server error", http.StatusInternalServerError) |
|
|
|
http.Error(w, "internal server error", http.StatusInternalServerError) |
|
|
@ -100,7 +103,7 @@ func New(workingDir string, dbPath string, port uint) (*Server, error) { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
requestedPage, err := page.Get(pagesDirPath, "base.html", req.URL.Path[1:]+".html") |
|
|
|
requestedPage, err := GetPage(pagesDirPath, "base.html", req.URL.Path[1:]+".html") |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Error("error getting page on route %s: %s", req.URL.Path, err) |
|
|
|
log.Error("error getting page on route %s: %s", req.URL.Path, err) |
|
|
|
http.Error(w, "internal server error", http.StatusInternalServerError) |
|
|
|
http.Error(w, "internal server error", http.StatusInternalServerError) |
|
|
@ -124,6 +127,8 @@ func New(workingDir string, dbPath string, port uint) (*Server, error) { |
|
|
|
} |
|
|
|
} |
|
|
|
server.http = &httpServer |
|
|
|
server.http = &httpServer |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.Info("Created server instance") |
|
|
|
|
|
|
|
|
|
|
|
return &server, nil |
|
|
|
return &server, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -135,9 +140,23 @@ func (s *Server) Start() { |
|
|
|
// clean attachments storage from time to time
|
|
|
|
// clean attachments storage from time to time
|
|
|
|
// max attachment filesize * 50 is the limit, check every 5 sec
|
|
|
|
// max attachment filesize * 50 is the limit, check every 5 sec
|
|
|
|
go manageAttachmentsStorage(filepath.Join(s.workingDir, attachmentsDirName), api.MaxAttachmentSize*50, time.Second*5) |
|
|
|
go manageAttachmentsStorage(filepath.Join(s.workingDir, attachmentsDirName), api.MaxAttachmentSize*50, time.Second*5) |
|
|
|
// fire up a server
|
|
|
|
|
|
|
|
|
|
|
|
// fire up either a TLS or non-TLS server
|
|
|
|
|
|
|
|
if s.keyFile != "" && s.certFile != "" { |
|
|
|
|
|
|
|
log.Info("Using TLS") |
|
|
|
|
|
|
|
log.Info("Working on %s", s.http.Addr) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err := s.http.ListenAndServeTLS(s.certFile, s.keyFile) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Error("Fatal server error: %s", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
log.Info("Not using TLS") |
|
|
|
|
|
|
|
log.Info("Working on %s", s.http.Addr) |
|
|
|
|
|
|
|
|
|
|
|
err := s.http.ListenAndServe() |
|
|
|
err := s.http.ListenAndServe() |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Error("FATAL server error: %s", err) |
|
|
|
log.Error("Fatal server error: %s", err) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|