grim/hgkeeper

Split the server pieces out of hgweb.go

2022-03-07, Gary Kramlich
334e63a51155
Parents d30d528a29fa
Children 578a90d306a3
Split the server pieces out of hgweb.go
--- a/hgweb/hgweb.go Mon Mar 07 01:34:20 2022 -0600
+++ b/hgweb/hgweb.go Mon Mar 07 01:38:28 2022 -0600
@@ -3,44 +3,15 @@
import (
"embed"
"io/ioutil"
- "net/http"
- "net/http/cgi"
- "os"
"path/filepath"
"text/template"
- log "github.com/sirupsen/logrus"
-
"keep.imfreedom.org/grim/hgkeeper/access"
- "keep.imfreedom.org/grim/hgkeeper/hg"
- hgkHttp "keep.imfreedom.org/grim/hgkeeper/http"
)
//go:embed files/*
var files embed.FS
-type Server struct {
- listenAddr string
- server *http.Server
-
- configPath string
- cgiPath string
-
- externalHostname string
- externalPort string
-}
-
-func NewServer(listenAddr string, externalHostname, externalPort string) (*Server, error) {
- return &Server{
- listenAddr: listenAddr,
- server: &http.Server{
- Addr: listenAddr,
- },
- externalHostname: externalHostname,
- externalPort: externalPort,
- }, nil
-}
-
func (s *Server) createConfig() error {
configPath, err := ioutil.TempFile("", "hgkeeper-hgweb-*.config")
if err != nil {
@@ -102,49 +73,3 @@
return nil
}
-
-func (s *Server) Listen() error {
- if err := s.createConfig(); err != nil {
- return err
- }
-
- if err := s.createCGI(); err != nil {
- return err
- }
-
- templatesDir, err := hg.TemplatesDir()
- if err != nil {
- return err
- }
- staticPath := filepath.Join(templatesDir, "static")
- log.Infof("serving static files from %s", staticPath)
- fileServer := http.FileServer(http.Dir(staticPath))
-
- mux := http.NewServeMux()
-
- if s.externalHostname != "" {
- mux.Handle("/ssh/authorized_keys", hgkHttp.AuthorizedKeysHandler(s.externalHostname, s.externalPort))
- log.Infof("added /ssh/authorized_keys endpoint with external hostname %s and export port %s", s.externalHostname, s.externalPort)
- } else {
- log.Infof("no external hostname specified, not adding /ssh/authorized_keys endpoint")
- }
-
- mux.Handle("/static/", http.StripPrefix("/static", fileServer))
- mux.Handle("/", &cgi.Handler{Path: s.cgiPath})
-
- s.server.Handler = Logger(mux)
-
- log.Infof("http listening on %s", s.listenAddr)
-
- return s.server.ListenAndServe()
-}
-
-func (s *Server) Close() {
- if err := os.Remove(s.cgiPath); err != nil {
- log.Warnf("failed to remove temporary cgi file %q: %v", s.cgiPath, err)
- }
-
- if err := s.server.Close(); err != nil {
- log.Warnf("failed to shutdown http server: %v", err)
- }
-}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/hgweb/server.go Mon Mar 07 01:38:28 2022 -0600
@@ -0,0 +1,80 @@
+package hgweb
+
+import (
+ "net/http"
+ "net/http/cgi"
+ "os"
+ "path/filepath"
+
+ log "github.com/sirupsen/logrus"
+ "keep.imfreedom.org/grim/hgkeeper/hg"
+ hgkHttp "keep.imfreedom.org/grim/hgkeeper/http"
+)
+
+type Server struct {
+ listenAddr string
+ server *http.Server
+
+ configPath string
+ cgiPath string
+
+ externalHostname string
+ externalPort string
+}
+
+func NewServer(listenAddr string, externalHostname, externalPort string) (*Server, error) {
+ return &Server{
+ listenAddr: listenAddr,
+ server: &http.Server{
+ Addr: listenAddr,
+ },
+ externalHostname: externalHostname,
+ externalPort: externalPort,
+ }, nil
+}
+
+func (s *Server) Listen() error {
+ if err := s.createConfig(); err != nil {
+ return err
+ }
+
+ if err := s.createCGI(); err != nil {
+ return err
+ }
+
+ templatesDir, err := hg.TemplatesDir()
+ if err != nil {
+ return err
+ }
+ staticPath := filepath.Join(templatesDir, "static")
+ log.Infof("serving static files from %s", staticPath)
+ fileServer := http.FileServer(http.Dir(staticPath))
+
+ mux := http.NewServeMux()
+
+ if s.externalHostname != "" {
+ mux.Handle("/ssh/authorized_keys", hgkHttp.AuthorizedKeysHandler(s.externalHostname, s.externalPort))
+ log.Infof("added /ssh/authorized_keys endpoint with external hostname %s and export port %s", s.externalHostname, s.externalPort)
+ } else {
+ log.Infof("no external hostname specified, not adding /ssh/authorized_keys endpoint")
+ }
+
+ mux.Handle("/static/", http.StripPrefix("/static", fileServer))
+ mux.Handle("/", &cgi.Handler{Path: s.cgiPath})
+
+ s.server.Handler = Logger(mux)
+
+ log.Infof("http listening on %s", s.listenAddr)
+
+ return s.server.ListenAndServe()
+}
+
+func (s *Server) Close() {
+ if err := os.Remove(s.cgiPath); err != nil {
+ log.Warnf("failed to remove temporary cgi file %q: %v", s.cgiPath, err)
+ }
+
+ if err := s.server.Close(); err != nil {
+ log.Warnf("failed to shutdown http server: %v", err)
+ }
+}