Browse Source

and it was so...it was very good

jordyn 4 years ago
commit
14e6fcc396
4 changed files with 242 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 26 0
      figtree/fig.go
  3. 153 0
      figtree/figtree.go
  4. 62 0
      main.go

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+*.db

+ 26 - 0
figtree/fig.go

@@ -0,0 +1,26 @@
+package figtree
+
+import (
+	"fmt"
+)
+
+type Fig struct {
+	Id string
+	Name string `json:"name"`
+	Desc string `json:"desc"`
+	Type string `json:"type"`
+	Ext string `json:"ext"`
+	Birth string `json:"birth"`
+}
+
+type FancyFig struct {
+	Name string
+	Path string
+	Desc string
+	Birth string
+}
+
+func (f Fig) Fancy() FancyFig {
+	path := fmt.Sprintf("%s%s/%s.%s", FIGS, f.Type, f.Id, f.Ext)
+	return FancyFig{ f.Name, path, f.Desc, f.Birth }
+}

+ 153 - 0
figtree/figtree.go

@@ -0,0 +1,153 @@
+package figtree
+
+import (
+	"log"
+	"os"
+	"time"
+	"path"
+
+	"math/rand"
+	"database/sql"
+
+	_ "github.com/mattn/go-sqlite3"
+)
+
+var FIGS = "/home/jord/figs/"
+
+var tree *sql.DB
+
+func init() {
+	rand.Seed(time.Now().UnixNano())
+	treePlanted := isPlanted()
+	if ! treePlanted {
+		plant()
+	}
+	mytree, err := sql.Open("sqlite3", "figtree.db")
+	if err != nil {
+		log.Fatal(err.Error())
+	}
+	tree = mytree
+}
+
+func main() {
+	defer tree.Close()
+}
+
+func isPlanted() bool {
+	_, err := os.Stat("figtree.db")
+	isTree := !os.IsNotExist(err)
+	return isTree
+}
+
+func plant() {
+	log.Println("planting fig tree 🌿")
+	file, err := os.Create("figtree.db")
+	if err != nil {
+		log.Fatal(err.Error())
+	}
+	file.Close()
+	log.Println("fig tree planted")
+
+	log.Println("sprouting tree")
+
+	treeSchema := `CREATE TABLE tree (
+		"id" string primary key not null,
+		"name" string,
+		"desc" string,
+		"type" string not null,
+		"ext" string,
+		"birth" string
+	);`
+
+	mytree, err := sql.Open("sqlite3", "figtree.db")
+	defer mytree.Close()
+	_, err = mytree.Exec(treeSchema)
+	if err != nil {
+		log.Fatal(err.Error())
+	}
+	log.Println("tree sprouted :)")
+}
+
+func nameAFig() string {
+	name := newName()
+	for nameExists(name) {
+		name = newName()
+	}
+	return name
+}
+
+func newName() string {
+	NAMELEN := 6
+	CHARS := []rune("abcdefghijklmnopqrstuvwxyz0123456789")
+	figname := make([]rune, NAMELEN)
+	for i := range figname {
+		figname[i] = CHARS[rand.Intn(len(CHARS))]
+	}
+	return string(figname)
+}
+
+func nameExists(figname string) bool {
+	check := "select name from tree where name like '%" + figname + "%'"
+	rows, err := tree.Query(check)
+	if err != nil {
+		log.Fatal(err.Error())
+	}
+	defer rows.Close()
+	return rows.Next()
+}
+
+func DryFig(fig Fig) error {
+	addFig := `insert into tree (id, name, desc, type, ext, birth) values (?, ?, ?, ?, ?, ?);`
+	statement, err := tree.Prepare(addFig)
+	if err != nil {
+		return err
+	}
+
+	_, err = statement.Exec(fig.Id, fig.Name, fig.Desc, fig.Type, fig.Ext, fig.Birth)
+
+	if err != nil {
+		return err
+	}
+	return nil
+}
+
+func AllFigsFancy() ([]FancyFig, error) {
+	var figs []FancyFig
+	row, err := tree.Query("select * from tree")
+	if err != nil {
+		return nil, err
+	}
+	defer row.Close()
+	for row.Next() {
+		var id, name, desc, figtype, ext, birth string
+		row.Scan(&id, &name, &desc, &figtype, &ext, &birth)
+		f := Fig{id, name, desc, figtype, ext, birth}
+		figs = append(figs, f.Fancy())
+	}
+	return figs, nil
+}
+
+func HarvestFig(name, desc, figtype, ext, birth string) (string, Fig, error) {
+	if len(figtype) == 0 {
+		figtype = "untyped"
+	}
+	figBranch := path.Join(FIGS, figtype)
+	_, err := os.Stat(figBranch)
+	if os.IsNotExist(err) {
+		err = os.Mkdir(figBranch, 0774)
+		if err != nil {
+			return "", Fig{}, err
+		}
+	}
+	newfig := Fig{
+		nameAFig(),
+		name,
+		desc,
+		figtype,
+		ext,
+		birth}
+
+	figid := newfig.Id + "." + newfig.Ext
+	figBlossom := path.Join(figBranch, figid)
+	return figBlossom, newfig, nil
+}

+ 62 - 0
main.go

@@ -0,0 +1,62 @@
+package main
+
+import (
+	"fmt"
+	"log"
+	"os"
+	"io"
+
+	"encoding/json"
+	"net/http"
+
+	"git.sr.ht/~jordyn/imagination/figtree"
+)
+
+func main() {
+	http.HandleFunc("/", homeHandler)
+	http.HandleFunc("/figs/", figHandler)
+	http.HandleFunc("/all/", allHandler)
+	log.Fatal(http.ListenAndServe(":8080", nil))
+}
+
+func homeHandler(w http.ResponseWriter, r *http.Request) {
+	fmt.Fprintf(w, "<h1>my beautiful figs</h1>")
+}
+
+func figHandler(w http.ResponseWriter, r *http.Request) {
+	r.ParseMultipartForm(32 << 20)
+	file, _, err := r.FormFile("newfig")
+	if err != nil {
+		panic(err)
+	}
+	defer file.Close()
+
+	fighome, newfig, err := figtree.HarvestFig(
+		r.FormValue("name"),
+		r.FormValue("desc"),
+		r.FormValue("type"),
+		r.FormValue("ext"),
+		r.FormValue("birth"))
+
+	log.Println("blooming " + fighome)
+
+	f, err := os.Create(fighome)
+	if err != nil {
+		panic(err)
+	}
+
+	err = os.Chmod(fighome, 0666)
+
+	if err != nil {
+		panic(err)
+	}
+	defer f.Close()
+	io.Copy(f, file)
+
+	figtree.DryFig(newfig)
+}
+
+func allHandler(w http.ResponseWriter, r *http.Request) {
+	figs, _ := figtree.AllFigsFancy()
+	json.NewEncoder(w).Encode(figs)
+}