Compare commits

..

No commits in common. "e20dc64d0d68108a8a899a4744d4d1dd0aff055e" and "2fa72b5cf46c905a0782938e54cc8260dbed2bbc" have entirely different histories.

7 changed files with 26 additions and 54 deletions

@ -1,4 +1,4 @@
# 📜 pages # pages
`pages` provides web application / static site hosting with built-in support for simple analytics via [Prometheus][] and `pages` provides web application / static site hosting with built-in support for simple analytics via [Prometheus][] and
[Grafana][]. [Grafana][].

@ -18,7 +18,6 @@ package commands
import ( import (
"context" "context"
"mime"
"net/http" "net/http"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -52,20 +51,8 @@ var (
Action: func(ctx *cli.Context) error { Action: func(ctx *cli.Context) error {
log := zaputil.Extract(ctx.Context) log := zaputil.Extract(ctx.Context)
// additional mime-types that need to be explicitly registered gitService := git.NewService(hostConfig.Git)
_ = mime.AddExtensionType(".woff2", "application/font-woff2") err := gitService.Load(ctx.Context)
_ = mime.AddExtensionType(".woff", "application/font-woff")
_ = mime.AddExtensionType(".ttf", "font/ttf")
_ = mime.AddExtensionType(".yaml", "application/yaml")
_ = mime.AddExtensionType(".yml", "application/yaml")
_ = mime.AddExtensionType(".json", "application/json")
gitService, err := git.NewService(hostConfig.Git)
if err != nil {
return err
}
err = gitService.Load(ctx.Context)
if err != nil { if err != nil {
return err return err
} }

@ -58,10 +58,6 @@ type httpFile struct {
file billy.File file billy.File
} }
func (f *httpFile) Seek(offset int64, whence int) (int64, error) {
return f.file.Seek(offset, whence)
}
func (f *httpFile) Stat() (fs.FileInfo, error) { func (f *httpFile) Stat() (fs.FileInfo, error) {
return f.fileInfo, nil return f.fileInfo, nil
} }

@ -18,10 +18,9 @@ package git
import ( import (
"context" "context"
"os"
"github.com/go-git/go-billy/v5" "github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/osfs" "github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/http" "github.com/go-git/go-git/v5/plumbing/transport/http"
@ -42,14 +41,11 @@ type Config struct {
} }
// NewService constructs a Service that manages the underlying git repository. // NewService constructs a Service that manages the underlying git repository.
func NewService(config Config) (*Service, error) { func NewService(config Config) *Service {
temp, err := os.MkdirTemp(os.TempDir(), "pages-*")
if err != nil {
return nil, err
}
options := &git.CloneOptions{ options := &git.CloneOptions{
URL: config.URL, URL: config.URL,
Depth: 1,
SingleBranch: true,
} }
if config.Username != "" && config.Password != "" { if config.Username != "" && config.Password != "" {
@ -69,8 +65,8 @@ func NewService(config Config) (*Service, error) {
return &Service{ return &Service{
options: options, options: options,
Store: memory.NewStorage(), Store: memory.NewStorage(),
FS: osfs.New(temp), FS: memfs.New(),
}, nil }
} }
// Service encapsulates operations that can be performed against the target git repository. // Service encapsulates operations that can be performed against the target git repository.
@ -102,7 +98,7 @@ func (s *Service) Sync(ctx context.Context) error {
return errors.Wrap(err, "failed to obtain worktree") return errors.Wrap(err, "failed to obtain worktree")
} }
err = wt.PullContext(ctx, &git.PullOptions{ _ = wt.PullContext(ctx, &git.PullOptions{
ReferenceName: s.options.ReferenceName, ReferenceName: s.options.ReferenceName,
SingleBranch: s.options.SingleBranch, SingleBranch: s.options.SingleBranch,
Depth: s.options.Depth, Depth: s.options.Depth,
@ -110,11 +106,5 @@ func (s *Service) Sync(ctx context.Context) error {
Force: true, Force: true,
}) })
switch {
case errors.Is(err, git.NoErrAlreadyUpToDate):
case err != nil:
zaputil.Extract(ctx).Error("failed to pull", zap.Error(err))
}
return nil return nil
} }

@ -20,6 +20,7 @@ import (
"context" "context"
"net" "net"
"net/http" "net/http"
"path"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
@ -94,15 +95,9 @@ func NewServer(ctx context.Context, config ServerConfig) (*Server, error) {
public.Use( public.Use(
session.Middleware( session.Middleware(
session.Exclusions(exclusions...), session.Exclusions(exclusions...),
session.JavaScriptPath("/pages.js"), session.JavaScriptPath(path.Join(config.Session.Prefix, "pages.js")),
), ),
) )
var handler http.Handler = session.Handler()
handler = http.StripPrefix(config.Session.Prefix, handler)
public.Handle("/pages.js", web.Handler()).Methods(http.MethodGet)
public.PathPrefix(config.Session.Prefix).Handler(handler)
} }
admin := public.PathPrefix(config.Admin.Prefix).Subrouter() admin := public.PathPrefix(config.Admin.Prefix).Subrouter()
@ -116,6 +111,15 @@ func NewServer(ctx context.Context, config ServerConfig) (*Server, error) {
}) })
} }
{
var handler http.Handler = session.Handler()
handler = http.StripPrefix(config.Session.Prefix, handler)
session := public.PathPrefix(config.Session.Prefix).Subrouter()
session.HandleFunc("/pages.js", web.Handler()).Methods(http.MethodGet)
session.Handle("/", handler)
}
return &Server{ return &Server{
AdminMux: admin, AdminMux: admin,

@ -21,9 +21,7 @@ import (
"time" "time"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"go.uber.org/zap"
"github.com/mjpitz/myago/zaputil"
"github.com/mjpitz/pages/internal/geoip" "github.com/mjpitz/pages/internal/geoip"
"github.com/mjpitz/pages/internal/metrics" "github.com/mjpitz/pages/internal/metrics"
) )
@ -47,11 +45,9 @@ type Handle struct {
} }
func (h *Handle) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *Handle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log := zaputil.Extract(r.Context())
conn, err := h.upgrader.Upgrade(w, r, nil) conn, err := h.upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
log.Error("failed to upgrade connection", zap.Error(err)) // log
return return
} }
@ -74,6 +70,7 @@ func (h *Handle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
err = conn.ReadJSON(&req) err = conn.ReadJSON(&req)
if err != nil { if err != nil {
// log
return return
} }
} }

@ -18,13 +18,11 @@
import {ulid} from 'ulid'; import {ulid} from 'ulid';
const secure = window.location.protocol === "https:";
const protocol = secure ? "wss:" : "ws:";
const sessionPrefix = "/_session"; const sessionPrefix = "/_session";
const domain = window.location.host + (secure ? ":443" : ""); const domain = window.location.host;
async function main() { async function main() {
const ws = new WebSocket(`${protocol}//${domain}${sessionPrefix}${window.location.pathname}`); const ws = new WebSocket(`ws://${domain}${sessionPrefix}${window.location.pathname}`);
const message = `{"ID":"${ulid()}","FullName":"ping"}` const message = `{"ID":"${ulid()}","FullName":"ping"}`
let id = null; let id = null;