flounder

A simple gemini site builder
git clone git://git.alexwennerberg.com/flounder
Log | Files | Refs | README | LICENSE

finger.go (1628B) - raw


      1 package main
      2 
      3 import (
      4 	"context"
      5 	"io"
      6 	"log"
      7 	"os"
      8 	"path"
      9 	"strings"
     10 	"time"
     11 
     12 	"github.com/mitchellh/go-finger"
     13 )
     14 
     15 func runFingerServer() {
     16 	log.Println("Starting finger server")
     17 	s := &finger.Server{
     18 		Handler: finger.HandlerFunc(func(ctx context.Context, w io.Writer, q *finger.Query) {
     19 			log.Printf("finger request: %v", q)
     20 			if q.Username == "" {
     21 				var hasPlan []string
     22 				users, err := getActiveUserNames(true)
     23 				for _, user := range users {
     24 					plan := path.Join(c.FilesDirectory, user, ".plan")
     25 					_, err := os.Stat(plan)
     26 					if err == nil {
     27 						hasPlan = append(hasPlan, user)
     28 					}
     29 				}
     30 				if err != nil {
     31 					w.Write([]byte("Error\n"))
     32 				}
     33 				w.Write([]byte("Finger users on this server:\n============================\n\n"))
     34 				w.Write([]byte(strings.Join(hasPlan, "\n")))
     35 				return
     36 			}
     37 			userName := cleanPath(q.Username)
     38 			fullPath := path.Join(c.FilesDirectory, userName, ".plan")
     39 			f, err := os.Open(fullPath)
     40 			if err != nil {
     41 				w.Write([]byte("This user has not set up their finger. Check them out on gemini or the web though on flounder.online.\n"))
     42 				return
     43 			}
     44 			// i, err := os.Stat(fullPath)
     45 			if err != nil {
     46 				w.Write([]byte("Error\n"))
     47 				return
     48 			}
     49 			_, err = io.Copy(w, f)
     50 			if err != nil {
     51 				w.Write([]byte("Error\n"))
     52 				return
     53 			}
     54 			// updatedString := i.ModTime().Format("2006-01")
     55 			// w.Write([]byte("\n---\nUpdated: "))
     56 			// w.Write([]byte(updatedString))
     57 		}),
     58 		Addr:          ":7979",
     59 		ReadTimeout:   5 * time.Minute,
     60 		WriteTimeout:  5 * time.Minute,
     61 		MaxQueryBytes: 4096,
     62 	}
     63 
     64 	err := s.ListenAndServe()
     65 	if err != nil {
     66 		panic(err)
     67 	}
     68 }