Skip to content

Commit

Permalink
Invoke container handler directly in server.
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-osman committed May 6, 2022
1 parent abaac1a commit 68a9264
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 115 deletions.
13 changes: 7 additions & 6 deletions conman/conman.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"sync"

"github.com/nathan-osman/i5/container"
"github.com/nathan-osman/i5/dbman"
"github.com/nathan-osman/i5/dockmon"
"github.com/nathan-osman/i5/util"
Expand All @@ -26,7 +27,7 @@ type Conman struct {
closedChan chan bool
}

func (c *Conman) initDatabase(con *dockmon.Container) error {
func (c *Conman) initDatabase(con *container.Container) error {
d, err := c.dbman.Get(con.Database.Driver)
if err != nil {
return err
Expand Down Expand Up @@ -84,7 +85,7 @@ func New(cfg *Config) *Conman {
return c
}

func (c *Conman) Add(con *dockmon.Container) {
func (c *Conman) Add(con *container.Container) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.idMap.Insert(con.ID, con)
Expand All @@ -99,7 +100,7 @@ func (c *Conman) Add(con *dockmon.Container) {
}
}

func (c *Conman) Remove(con *dockmon.Container) {
func (c *Conman) Remove(con *container.Container) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.idMap.Remove(con.ID)
Expand All @@ -109,7 +110,7 @@ func (c *Conman) Remove(con *dockmon.Container) {
}
}

func (c *Conman) ToggleState(con *dockmon.Container, running bool) {
func (c *Conman) ToggleState(con *container.Container, running bool) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.idMap.Insert(con.ID, con)
Expand All @@ -120,11 +121,11 @@ func (c *Conman) ToggleState(con *dockmon.Container, running bool) {
}

// Lookup attempts to retrieve the container for the provided domain name.
func (c *Conman) Lookup(name string) (*dockmon.Container, error) {
func (c *Conman) Lookup(name string) (*container.Container, error) {
c.mutex.RLock()
defer c.mutex.RUnlock()
if v, ok := c.domainMap[name]; ok {
return v.(*dockmon.Container), nil
return v.(*container.Container), nil
} else {
return nil, ErrInvalidDomain
}
Expand Down
20 changes: 10 additions & 10 deletions conman/info.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package conman

import (
"github.com/nathan-osman/i5/dockmon"
"github.com/nathan-osman/i5/container"
)

// Info stores information for a specific container.
type Info struct {
ID string `json:"id"`
Name string `json:"name"`
Domains []string `json:"domains"`
Running bool `json:"running"`
ID string `json:"id"`
Name string `json:"name"`
Domains []string `json:"domains"`
Disabled bool `json:"disabled"`
}

// Info returns information about running containers.
Expand All @@ -18,13 +18,13 @@ func (c *Conman) Info() []*Info {
defer c.mutex.RUnlock()
ret := []*Info{}
for _, v := range c.idMap {
con := v.(*dockmon.Container)
con := v.(*container.Container)
if con.ID != "" {
ret = append(ret, &Info{
ID: con.ID,
Name: con.Name,
Domains: con.Domains,
Running: con.Running,
ID: con.ID,
Name: con.Name,
Domains: con.Domains,
Disabled: con.Disabled,
})
}
}
Expand Down
89 changes: 3 additions & 86 deletions container/handler.go
Original file line number Diff line number Diff line change
@@ -1,98 +1,15 @@
package container

import (
"bytes"
"html/template"
"net/http"
"strconv"
)

var disabledTemplate = template.Must(
template.New("").Parse(`<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Page Not Available</title>

<!-- Everything needs to be kept inline -->
<style>
html, body {
height: 100%;
}
body {
align-items: center;
background-color: #f7f7f7;
color: #777;
display: flex;
font-family: sans-serif;
justify-content: center;
margin: 0;
}
a {
color: #ff6600;
}
footer {
border-top: 2px solid #ddd;
display: flex;
padding-top: 12px;
}
h1 {
font-size: 30pt;
font-weight: normal;
margin: 0;
}
p {
color: #aaa;
}
pre {
white-space: pre-wrap;
}
.content {
max-width: 800px;
}
.logo {
margin-right: 8px;
width: 30pt;
}
@media (max-width: 839px) {
.content {
margin: 20px;
}
}
</style>
</head>
<body>
<div class="content">
<h1>Page Not Available</h1>
<p>{{.Message}}</p>
<footer>
<!-- EVERYTHING needs to be kept inline :P -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</footer>
</div>
</body>
</html>
`))
"github.com/nathan-osman/i5/util"
)

type disabledHandler struct {
Message string
}

func (d *disabledHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
buff := &bytes.Buffer{}
disabledTemplate.Execute(buff, d)
b := buff.Bytes()
w.Header().Set("Content-Length", strconv.Itoa(len(b)))
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
w.Write(b)
util.RenderError(w, r, http.StatusInternalServerError, d.Message)
}
16 changes: 3 additions & 13 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"path"

"github.com/nathan-osman/i5/conman"
"github.com/nathan-osman/i5/dockmon"
"github.com/nathan-osman/i5/proxy"
"github.com/nathan-osman/i5/util"
"github.com/sirupsen/logrus"
Expand All @@ -18,8 +17,7 @@ import (
)

const (
errContainerNotRunning = "container serving this domain is not running"
errInvalidDomainName = "invalid domain name specified"
errInvalidDomainName = "invalid domain name specified"

letsEncryptStagingURL = "https://acme-staging-v02.api.letsencrypt.org/directory"
)
Expand All @@ -37,18 +35,10 @@ func (s *Server) decide(ctx context.Context, host string) error {
return err
}

func (s *Server) handleRequest(con *dockmon.Container, w http.ResponseWriter, r *http.Request) {
if con.Running {
con.Handler.ServeHTTP(w, r)
} else {
util.RenderError(w, r, http.StatusInternalServerError, errContainerNotRunning)
}
}

func (s *Server) handleHTTP(w http.ResponseWriter, r *http.Request) {
if con, err := s.conman.Lookup(r.Host); err == nil {
if con.Insecure {
s.handleRequest(con, w, r)
con.Handler.ServeHTTP(w, r)
} else {
http.Redirect(
w, r,
Expand All @@ -68,7 +58,7 @@ func (s *Server) handleHTTP(w http.ResponseWriter, r *http.Request) {

func (s *Server) handleHTTPS(w http.ResponseWriter, r *http.Request) {
if con, err := s.conman.Lookup(r.Host); err == nil {
s.handleRequest(con, w, r.WithContext(
con.Handler.ServeHTTP(w, r.WithContext(
context.WithValue(r.Context(), proxy.ContextSecure, true),
))
} else {
Expand Down

0 comments on commit 68a9264

Please sign in to comment.