git.alexw.nyc home about git garden
    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
package main

import (
	"context"
	"io"
	"log"
	"os"
	"path"
	"strings"
	"time"

	"github.com/mitchellh/go-finger"
)

func runFingerServer() {
	log.Println("Starting finger server")
	s := &finger.Server{
		Handler: finger.HandlerFunc(func(ctx context.Context, w io.Writer, q *finger.Query) {
			log.Printf("finger request: %v", q)
			if q.Username == "" {
				var hasPlan []string
				users, err := getActiveUserNames(true)
				for _, user := range users {
					plan := path.Join(c.FilesDirectory, user, ".plan")
					_, err := os.Stat(plan)
					if err == nil {
						hasPlan = append(hasPlan, user)
					}
				}
				if err != nil {
					w.Write([]byte("Error\n"))
				}
				w.Write([]byte("Finger users on this server:\n============================\n\n"))
				w.Write([]byte(strings.Join(hasPlan, "\n")))
				return
			}
			userName := cleanPath(q.Username)
			fullPath := path.Join(c.FilesDirectory, userName, ".plan")
			f, err := os.Open(fullPath)
			if err != nil {
				w.Write([]byte("This user has not set up their finger. Check them out on gemini or the web though on flounder.online.\n"))
				return
			}
			// i, err := os.Stat(fullPath)
			if err != nil {
				w.Write([]byte("Error\n"))
				return
			}
			_, err = io.Copy(w, f)
			if err != nil {
				w.Write([]byte("Error\n"))
				return
			}
			// updatedString := i.ModTime().Format("2006-01")
			// w.Write([]byte("\n---\nUpdated: "))
			// w.Write([]byte(updatedString))
		}),
		Addr:          ":7979",
		ReadTimeout:   5 * time.Minute,
		WriteTimeout:  5 * time.Minute,
		MaxQueryBytes: 4096,
	}

	err := s.ListenAndServe()
	if err != nil {
		panic(err)
	}
}