main.go (3926B)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | package main
import ("os"; "io"; "fmt"; "log")
import ("os/exec"; "time"; "bufio")
import "strings"
import "path/filepath"
import "text/template"
import "net/http"
import "github.com/pitr/gig"
var GEMINI_CERT = os.Getenv("GEMINI_CERT")
var GEMINI_KEY = os.Getenv("GEMINI_KEY")
type DefaultTemplate struct {
Date time.Time
Fortune string
Copyright string
Footer string
}
func NewDefaultTemplate() (t DefaultTemplate) {
t = DefaultTemplate{}
t.Date = time.Now()
t.Copyright = "\U0001F12F Copyleft, all wrongs reserved."
t.Fortune = "Failed to run `fortune`."
fortune := exec.Command("fortune", "-n", "short")
stdout, _ := fortune.StdoutPipe()
err := fortune.Start()
if err == nil {
t.Fortune = ""
buf := bufio.NewReader(stdout)
for {
line, _, err := buf.ReadLine()
if err != nil {
if err == io.EOF { break }
log.Println("Error reading line.", err)
}
l := string(line)
l = strings.ReplaceAll(l, "--", "\u2014")
t.Fortune += "\n> " + l
}
}
t.Footer = fmt.Sprintf("\n" +
"## \u2E3B\n" +
"%s\n" +
"```The date.\n" +
"~%04d.%02d.%02d\n" +
"```\n\n" +
"%s",
t.Fortune,
t.Date.Year(), t.Date.Month(), t.Date.Day(),
t.Copyright)
return
}
func walkTemplates(roots ...string) map[string] *template.Template {
templates := make(map[string] *template.Template)
for _, root := range roots {
err := filepath.Walk(root,
func(path string, info os.FileInfo, err error) error {
if path[len(path) - 4:] == ".gmi" {
templates[path] = template.Must(template.ParseFiles(path))
if err != nil {
log.Println(err)
}
}
return err
})
if err != nil {
panic(err)
}
}
for path, t := range templates {
log.Printf("<Template name=%s> %s\n", t.Name(), path)
}
return templates
}
func Template(c gig.Context, templates map[string] *template.Template,
path string, data interface{}) (err error) {
res := c.Response()
t, ok := templates[path]
if !ok { return gig.ErrNotFound }
err = res.WriteHeader(gig.StatusSuccess, gig.MIMETextGemini);
if err != nil { return }
return t.ExecuteTemplate(res, t.Name(), data)
}
func main() {
fmt.Println("[~] CERT:", GEMINI_CERT)
fmt.Println("[~] KEY:", GEMINI_KEY)
s := gig.Default()
templates := walkTemplates("./views", "./users")
defaultView := func (file string) (func(gig.Context) error) {
return func(c gig.Context) error {
defaultTemplate := NewDefaultTemplate()
return Template(c, templates, "views/" + file + ".gmi",
defaultTemplate);
}
}
userView := func (name string, file string) (func(gig.Context) error) {
return func(c gig.Context) error {
defaultTemplate := NewDefaultTemplate()
return Template(c, templates, "users/" + name + "/" + file + ".gmi",
defaultTemplate);
}
}
s.Static("/static", "static")
s.Handle("/static", func(c gig.Context) error {
return c.NoContent(gig.StatusRedirectPermanent, "/static/")
})
s.Handle("/", defaultView("index"))
s.Handle("weather", func(c gig.Context) error {
query, err := c.QueryString()
if err != nil { return err }
if query == "" {
return c.NoContent(gig.StatusInput, "Enter your location.")
}
loc := strings.Join(strings.Split(query, " "), "+")
res, err := http.Get("https://wttr.in/" + loc + "?AF0")
if err != nil || res.StatusCode != http.StatusOK {
return gig.ErrProxyError
}
defer res.Body.Close()
return c.Stream("text/html", res.Body)
})
user := s.Group("/~:name")
{
fileView := func(file string) (func(gig.Context) error) {
return func(c gig.Context) error {
return userView(c.Param("name"), file)(c)
}
}
user.Handle("", fileView("index"))
user.Handle("/", fileView("index"))
user.Handle("/:path", func(c gig.Context) error {
return fileView(c.Param("path"))(c)
})
}
s.Handle("/:path", func(c gig.Context) error {
return defaultView(c.Param("path"))(c)
})
s.Run(GEMINI_CERT, GEMINI_KEY)
}
|