Skip to content

Commit

Permalink
Merge pull request #7 from jmoiron/standard-mode
Browse files Browse the repository at this point in the history
overhaul towards newer more standard packages
  • Loading branch information
jmoiron authored Jan 2, 2025
2 parents 1bf988e + bd76ede commit cd55f0d
Show file tree
Hide file tree
Showing 95 changed files with 6,455 additions and 1,492 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2013, Jason Moiron
Copyright (c) 2020, Jason Moiron

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand Down
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

export CGO_CFLAGS := -g -O2 -Wno-return-local-addr
# -Wno-stringop-overflow

build:
CGO_FLAGS=$CGO_FLAGS go build --tags="fts5"

reload:
./monet --load-posts backup/posts.json
./monet --load-stream backup/stream.json
./monet --load-pages backup/pages.json

60 changes: 2 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,13 @@
The code in this repository has powered [jmoiron.net](http://jmoiron.net)
since Apr 2012.

Writing this application was a learning experience and as such the code in this
repository might not be the best example of idiomatic go, but I try:

* the package is `go get` able
* all code follows `go fmt` default conventions

## Features

`monet` runs a website with:

* a clean blog with search, archive, admin w/ ajax preview
* a clean blog with search, archive, admin
* a simple flatpage system for one-off URLs (ie. `/about/`)
* a front-end for twitter and github statuses (updater is currently in python).

The blog and flatpages are written in markdown and stored in mongodb. The site
itself is rendered via [mandira templates](http://jmoiron.github.com/mandira),
which when the `Debug` configuration option is `false` get cached.

## How do I run this?

First, go get this repos:

go get github.com/jmoiron/monet

This will install the `monet` command, which is a webserver that takes a config
file as an argument (defaults to `./config.json`) and runs a webserver on the
configured port. Here is a sample config.json:

```javascript
{
"SessionSecret": "(long random string here)",
"WebPort": 8000,
"TemplatePreCompile": true,
"TemplatePaths": ["(path to templates)"],
"Debug": false,
"GoogleAnalyticsTrackingID": "UA-(your GA id)",
"Streams": [{
"type": "twitter",
"user_id": "(your twitter user_id)"
}, {
"type": "github",
"username": "jmoiron",
"token": "(your github user token)"
}
],

"Gallery": {
"Type": "picasa",
"UserID": "(your picasa user id)"
}
}
```

If you have mongodb running on the default port and the localhost, you should
now be able to run `monet` and hit your site on port 8000.

### TODO:

* document in a way that go pkgdoc will work
* make styles less monolithic, separate structure from character
* load templates from monet's install path if no template paths are provided
in config, which makes it easier to run monet from anywhere
* take some things like port, config path, etc on command line
* move things like the title & subtitle into the config
* most of the gallery app
The blog and flatpages are written in markdown and stored in an SQL database.

1 change: 1 addition & 0 deletions admin/admin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package admin
35 changes: 35 additions & 0 deletions admin/admin/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/static/fonts.css">
{{if .debug}}
<link rel="stylesheet/less" href="/static/style.less">
<script src="/static/js/less.js"></script>
{{else}}
<link rel="stylesheet" href="/static/style.css">
{{end}}
<script src="/static/js/cash.min.js"></script>
<script src="/static/js/admin.js"></script>
<script src="/static/js/split-grid.min.js"></script>
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/split.js/1.6.0/split.min.js"></script> -->
<title>{{.title}}</title>
</head>
<body>
<div class="content">
<div class="container admin {{.extraContainerClass}}">
{{.body}}
</div>
</div>
<div class="footer">
<div class="container">
<div class="user-controls">
<a href="/admin/users/">users <i class="icon icon-user"></i></a>
</div>
<div class="admin-controls">
<a href="/admin/">home <i class="icon icon-home"></i></a> or
<a href="/logout/">logout <i class="icon icon-signout"></i></a>
</div>
</div>
</div>
</body>
</html>
9 changes: 9 additions & 0 deletions admin/admin/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div class="panels">
{{range $i, $panel := .panels}}
<div class="{{if eq (mod $i 2) (0)}}left{{else}}right{{end}}-panel panel">
{{$panel|safe}}
</div>
{{if ne (mod $i 2) (0)}}<div class="clear panel-sep"></div>{{end}}
{{end}}
</div>
<div class="clear"></div>
102 changes: 102 additions & 0 deletions admin/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package admin

import (
"embed"
"log/slog"
"net/http"

"github.com/go-chi/chi/v5"
"github.com/jmoiron/monet/app"
"github.com/jmoiron/monet/auth"
"github.com/jmoiron/monet/db"
"github.com/jmoiron/monet/mtr"
)

// the admin app attaches all of the admins to itself
// it relies on the auth app, but apps register their admin
// capabilities without importing this app, so it should be ok

type App struct {
db db.DB
sm *auth.SessionManager

BaseURL string
Apps []app.App
Admins []app.Admin
}

//go:embed admin/*
var templates embed.FS

func NewApp(db db.DB, sm *auth.SessionManager) *App {
return &App{db: db, sm: sm}
}

func (a *App) WithBaseURL(url string) *App {
a.BaseURL = url
return a
}

func (a *App) Name() string { return "stream" }

func (a *App) Register(reg *mtr.Registry) {
reg.AddBaseFS("admin-base", "admin/base.html", templates)
reg.AddPathFS("admin/index.html", templates)
}

func (a *App) Migrate() error { return nil }

func (a *App) Collect(apps ...app.App) {
a.Apps = apps
for _, app := range apps {
ad, err := app.GetAdmin()
if err != nil {
slog.Error("error for adminapp", "app", app.Name(), "err", err)
continue
}
if ad == nil {
slog.Debug("no admin for app", "app", app.Name())
continue
}
a.Admins = append(a.Admins, ad)
}
}

// GetAdmin is yo dawg?
func (a *App) GetAdmin() (app.Admin, error) {
return nil, nil
}

func (a *App) Bind(r chi.Router) {
// presumably we are bound to something like `/admin/`
r.Route(a.BaseURL, func(r chi.Router) {
r.Use(a.sm.RequireAuthenticatedRedirect("/admin/"))
for _, ad := range a.Admins {
ad.Bind(r)
}

r.Get("/", a.index)
})
}

func (a *App) index(w http.ResponseWriter, r *http.Request) {
var panels []string
for _, ad := range a.Admins {
ps, err := ad.Panels(r)
if err != nil {
slog.Error("rendering panel", "err", err)
continue
}
panels = append(panels, ps...)
}

reg := mtr.RegistryFromContext(r.Context())

err := reg.RenderWithBase(w, "admin-base", "admin/index.html", mtr.Ctx{
"panels": panels,
})

if err != nil {
slog.Error("rendering template", "err", err)
}
}
82 changes: 0 additions & 82 deletions app/admin.go

This file was deleted.

Loading

0 comments on commit cd55f0d

Please sign in to comment.