2020-10-16 19:12:53 +02:00
|
|
|
package web
|
2020-10-16 11:41:56 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2020-10-16 19:12:53 +02:00
|
|
|
"os"
|
2020-10-18 02:46:25 +02:00
|
|
|
"path"
|
2020-10-16 11:41:56 +02:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// RoutedService defines the interface that is needed for any service to
|
|
|
|
// register themself in the web server to provide new endpoints. (see
|
|
|
|
// AddRoutes).
|
2020-10-16 11:41:56 +02:00
|
|
|
type RoutedService interface {
|
|
|
|
RegisterRoutes(*mux.Router)
|
|
|
|
}
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// Server is the structure responsible for managing our http web server.
|
|
|
|
type Server struct {
|
2020-10-16 16:21:42 +02:00
|
|
|
router *mux.Router
|
|
|
|
rootPath string
|
|
|
|
port int
|
|
|
|
ssl bool
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// NewServer creates a new instance of the webserver.
|
|
|
|
func NewServer(rootPath string, port int, ssl bool) *Server {
|
2020-10-16 11:41:56 +02:00
|
|
|
r := mux.NewRouter()
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
ws := &Server{
|
2020-10-16 16:21:42 +02:00
|
|
|
router: r,
|
|
|
|
rootPath: rootPath,
|
|
|
|
port: port,
|
|
|
|
ssl: ssl,
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
2020-10-16 16:21:42 +02:00
|
|
|
|
|
|
|
return ws
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// AddRoutes allows services to register themself in the webserver router and provide new endpoints.
|
|
|
|
func (ws *Server) AddRoutes(rs RoutedService) {
|
2020-10-16 11:41:56 +02:00
|
|
|
rs.RegisterRoutes(ws.router)
|
|
|
|
}
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
func (ws *Server) registerRoutes() {
|
2020-10-18 02:46:25 +02:00
|
|
|
ws.router.PathPrefix("/static").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(filepath.Join(ws.rootPath, "static")))))
|
|
|
|
ws.router.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
http.ServeFile(w, r, path.Join(ws.rootPath, "index.html"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// Start runs the web server and start listening for charsetnnections.
|
|
|
|
func (ws *Server) Start() error {
|
2020-10-18 02:46:25 +02:00
|
|
|
ws.registerRoutes()
|
2020-10-16 11:41:56 +02:00
|
|
|
http.Handle("/", ws.router)
|
|
|
|
|
|
|
|
urlPort := fmt.Sprintf(`:%d`, ws.port)
|
2020-10-22 15:22:36 +02:00
|
|
|
isSSL := ws.ssl && fileExists("./cert/cert.pem") && fileExists("./cert/key.pem")
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
if isSSL {
|
|
|
|
log.Println("https server started on ", urlPort)
|
|
|
|
err := http.ListenAndServeTLS(urlPort, "./cert/cert.pem", "./cert/key.pem", nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
return nil
|
|
|
|
}
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
log.Println("http server started on ", urlPort)
|
|
|
|
err := http.ListenAndServe(urlPort, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// fileExists returns true if a file exists at the path.
|
2020-10-16 19:12:53 +02:00
|
|
|
func fileExists(path string) bool {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-16 19:12:53 +02:00
|
|
|
return err == nil
|
|
|
|
}
|