๐พ Archived View for source.community โบ ckaznocha โบ cliftons-capsule โบ blob โบ main โบ app.go captured on 2024-02-05 at 09:54:12. Gemini links have been rewritten to link to archived content
โฌ ๏ธ Previous capture (2023-01-29)
-=-=-=-=-=-=-
. ,-. ,-. . . ,-. ,-. ,-. ,-. ,-. ,-,-. ,-,-. . . ,-. . |- . . `-. | | | | | | |-' | | | | | | | | | | | | | | | | | `-' `-' `-^ ' `-' `-' :: `-' `-' ' ' ' ' ' ' `-^ ' ' ' `' `-| /| `-'
git clone https://source.community/ckaznocha/cliftons-capsule.git
View raw contents of /app.go (main)
โโโโโฎ 1โ package main 2โ 3โ import ( 4โ "context" 5โ "fmt" 6โ "io/fs" 7โ "log" 8โ "strings" 9โ "text/template" 10โ "time" 11โ 12โ "source.community/ckaznocha/gemini" 13โ ) 14โ 15โ type app struct { 16โ template *template.Template 17โ gemlog *gemlog 18โ logger *log.Logger 19โ } 20โ 21โ type footerArgs struct { 22โ Date time.Time 23โ Title string 24โ } 25โ 26โ func newApp(logger *log.Logger, gemlogsFS, templatesFS fs.FS) (*app, error) { 27โ t, err := template.New("capsule-templates").Funcs(template.FuncMap{ 28โ "now": time.Now, 29โ "footerArgs": func(title string, date time.Time) footerArgs { return footerArgs{Title: title, Date: date} }, 30โ }).ParseFS(templatesFS, "templates/*.tmpl.gmi") 31โ if err != nil { 32โ return nil, fmt.Errorf("cannot parse templates: %w", err) 33โ } 34โ 35โ gemlog := &gemlog{gemlogFS: gemlogsFS} 36โ if err := gemlog.populate(); err != nil { 37โ return nil, fmt.Errorf("cannot populate gemlog: %w", err) 38โ } 39โ 40โ return &app{ 41โ template: t, 42โ gemlog: gemlog, 43โ logger: logger, 44โ }, nil 45โ } 46โ 47โ func (a *app) indexHandler(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) { 48โ if strings.Trim(r.URI.Path, "/") != "" { 49โ w.Failure(ctx, gemini.StatusNotFound, gemini.StatusNotFound.Description()) 50โ 51โ return 52โ } 53โ 54โ if err := a.template.ExecuteTemplate(w.Success(ctx, ""), "index.tmpl.gmi", nil); err != nil { 55โ a.logger.Panicln(err) 56โ w.Failure(ctx, gemini.StatusServerFailure, gemini.StatusServerFailure.Description()) 57โ 58โ return 59โ } 60โ } 61โ 62โ func (a *app) gemlogHandler(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) { 63โ if r.URI.Path == "/gemlog/" { 64โ gemlogList, err := a.gemlog.list() 65โ if err != nil { 66โ a.logger.Println(err) 67โ w.Failure(ctx, gemini.StatusServerFailure, gemini.StatusServerFailure.Description()) 68โ 69โ return 70โ } 71โ 72โ if err := a.template.ExecuteTemplate(w.Success(ctx, ""), "gemlog.tmpl.gmi", gemlogList); err != nil { 73โ a.logger.Println(err) 74โ w.Failure(ctx, gemini.StatusServerFailure, gemini.StatusServerFailure.Description()) 75โ 76โ return 77โ } 78โ 79โ return 80โ } 81โ 82โ entry, ok := a.gemlog.entry(r.URI.Path) 83โ if !ok { 84โ w.Failure(ctx, gemini.StatusNotFound, gemini.StatusNotFound.Description()) 85โ 86โ return 87โ } 88โ 89โ if err := a.template.ExecuteTemplate(w.Success(ctx, ""), "gemlog.entry.tmpl.gmi", entry); err != nil { 90โ a.logger.Println(err) 91โ w.Failure(ctx, gemini.StatusServerFailure, gemini.StatusServerFailure.Description()) 92โ 93โ return 94โ } 95โ } โโโโโฏ
ยท ยท ยท
ยฉ 2024 source.community