main.go 1.04 KB
Newer Older
Janne Koschinski's avatar
Janne Koschinski committed
1 2 3
package main

import (
4
	"database/sql"
5
	"github.com/go-redis/redis"
Janne Koschinski's avatar
Janne Koschinski committed
6
	_ "github.com/lib/pq"
7
	"net/http"
Janne Koschinski's avatar
Janne Koschinski committed
8 9
)

Janne Koschinski's avatar
Janne Koschinski committed
10 11
func main() {
	config := NewConfigFromEnv()
Janne Koschinski's avatar
Janne Koschinski committed
12

13 14 15 16
	db, err := sql.Open(config.Database.Format, config.Database.Url)
	if err != nil {
		panic(err)
	}
Janne Koschinski's avatar
Janne Koschinski committed
17

18 19 20 21 22 23 24 25 26 27
	pageContext := PageContext{
		&config,
		redis.NewClient(&redis.Options{
			Addr:     config.Redis.Address,
			Password: config.Redis.Password,
		}),
		db,
		http.FileServer(http.Dir(config.TargetFolder)),
		http.FileServer(http.Dir("assets")),
	}
Janne Koschinski's avatar
Janne Koschinski committed
28

29
	http.Handle("/upload/", pageUpload(pageContext))
Janne Koschinski's avatar
Janne Koschinski committed
30

31
	http.Handle("/i/", http.StripPrefix("/i/", pageImageDetail(pageContext)))
32
	http.Handle("/a/", http.StripPrefix("/a/", pageAlbumDetail(pageContext)))
Janne Koschinski's avatar
Janne Koschinski committed
33

34
	http.Handle("/me/images/", pageImageList(pageContext))
Janne Koschinski's avatar
Janne Koschinski committed
35
	http.Handle("/assets/", http.StripPrefix("/assets/", pageContext.AssetServer))
36
	http.Handle("/", pageIndex(pageContext))
Janne Koschinski's avatar
Janne Koschinski committed
37

Janne Koschinski's avatar
Janne Koschinski committed
38 39 40 41
	http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("OK"))
	})

42
	err = http.ListenAndServe(":8080", nil)
Janne Koschinski's avatar
Janne Koschinski committed
43 44 45 46
	if err != nil {
		panic(err)
	}
}