Browse Source

TLS support

master
parent
commit
2919b0406f
  1. 18
      src/main.go
  2. 2
      src/server/attachmentHandler.go
  3. 4
      src/server/page.go
  4. 35
      src/server/server.go

18
src/main.go

@ -18,7 +18,6 @@ package main
import ( import (
"flag" "flag"
"fmt"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
@ -27,7 +26,13 @@ import (
"unbewohnte/gochat/server" "unbewohnte/gochat/server"
) )
const version string = "0.1.0" const version string = "0.1.1"
var (
port *uint = flag.Uint("port", 8080, "Set working port")
tlsKeyFile *string = flag.String("tlsKeyFile", "", "Specify tls key file")
tlsCertFile *string = flag.String("tlsCertFile", "", "Specify tls cert file")
)
func main() { func main() {
// set up logging // set up logging
@ -55,18 +60,13 @@ func main() {
log.SetOutput(os.Stdout) log.SetOutput(os.Stdout)
} }
// work out launch flags // parse flags
var port uint
flag.UintVar(&port, "port", 8080, "Set working port")
flag.Usage = func() {
fmt.Printf("gochat v%s\n\nFlags\nport [uint] -> specify a port number (default: 8080)\n\n(c) Unbewohnte (Kasyanov Nikolay Alexeyevich)\n", version)
}
flag.Parse() flag.Parse()
const dbFilename string = "gochat.db" const dbFilename string = "gochat.db"
dbPath := filepath.Join(exeDirPath, dbFilename) dbPath := filepath.Join(exeDirPath, dbFilename)
server, err := server.New(exeDirPath, dbPath, port) server, err := server.New(exeDirPath, dbPath, *port, *tlsKeyFile, *tlsCertFile)
if err != nil { if err != nil {
log.Error("could not create a new server instance: %s", err) log.Error("could not create a new server instance: %s", err)
} }

2
src/server/attachmentHandler.go

@ -102,7 +102,7 @@ func manageAttachmentsStorage(attachmentsDirPath string, sizeLimit uint64, check
} }
if dirSize > sizeLimit { if dirSize > sizeLimit {
// A cleanup ! // cleanup !
os.Remove(oldestAttachmentPath) os.Remove(oldestAttachmentPath)
log.Info( log.Info(
"removed %s during attachments storage management. Cleared %d bytes", "removed %s during attachments storage management. Cleared %d bytes",

4
src/page/page.go → src/server/page.go

@ -14,7 +14,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package page package server
import ( import (
"html/template" "html/template"
@ -22,7 +22,7 @@ import (
) )
// Parse files in pagesDir and return a ready-to-render template // Parse files in pagesDir and return a ready-to-render template
func Get(pagesDir string, base string, pagename string) (*template.Template, error) { func GetPage(pagesDir string, base string, pagename string) (*template.Template, error) {
page, err := template.ParseFiles(filepath.Join(pagesDir, base), filepath.Join(pagesDir, pagename)) page, err := template.ParseFiles(filepath.Join(pagesDir, base), filepath.Join(pagesDir, pagename))
if err != nil { if err != nil {
return nil, err return nil, err

35
src/server/server.go

@ -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
err := s.http.ListenAndServe() // fire up either a TLS or non-TLS server
if err != nil { if s.keyFile != "" && s.certFile != "" {
log.Error("FATAL server error: %s", err) 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()
if err != nil {
log.Error("Fatal server error: %s", err)
}
} }
} }

Loading…
Cancel
Save