aboutsummaryrefslogtreecommitdiff
path: root/cmd/web/main.go
blob: f215d1ecf759e0599f9c7c82683610b1d45566cd (plain)
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
package main

import (
	"log"
	"net/http"
	"os"
	"path"

	"github.com/go-chi/chi"

	"git.ofmax.li/iserv/internal/db/redis"
	"git.ofmax.li/iserv/internal/fs"
	"git.ofmax.li/iserv/internal/image"
	"go.ofmax.li/tmpl"
)

func main() {
	connPool := redis.CreatePool("localhost:6379")
	db := redis.NewRedisImageRepo(connPool)

	renderer, err := tmpl.NewHTMLTmpl("templates")
	if err != nil {
		log.Fatal(err)
	}

	storagePath, err := os.Getwd()
	if err != nil {
		log.Fatal("couldn't find directory to write images to")
	}

	// Image
	imageService := image.NewService(db, storagePath, renderer)
	imageHandler := image.NewHandler(imageService)
	imageFile := fs.NewHandler(storagePath)

	// Static Files
	staticFiles := fs.NewHandler(path.Join(storagePath, "static"))
	r := chi.NewRouter()

	r.Get("/i/{fileName}", imageHandler.GetImage)
	r.Post("/u", imageHandler.PostImage)
	r.Get("/f/*", imageFile)
	r.Get("/static/*", staticFiles)
	log.Print("starting imageserv")
	log.Fatal(http.ListenAndServe(":8080", r))
}