~evanj/e3

ref: b7e169e7716b0b0fbceb9322f3d45e557422a964 e3/e3.go -rw-r--r-- 2.3 KiB
b7e169e7Evan J Fix(.build.yml): Remove static dir ref from build file (was copied from 1 year, 9 months ago
                                                                                
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// package handler // for serverless deploy
package main // for regular deploy

import (
	"bytes"
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"strings"

	"git.sr.ht/~evanj/e3/internal/cache"
	"git.sr.ht/~evanj/e3/internal/db"
	"git.sr.ht/~evanj/e3/internal/send"
	"git.sr.ht/~evanj/e3/internal/upload"
	"git.sr.ht/~evanj/e3/internal/user"
	"git.sr.ht/~evanj/security"
)

type App struct {
	log    *log.Logger
	upload http.Handler
	send   http.Handler
	user   http.Handler
}

var app App

func init() {
	var w io.Writer
	switch os.Getenv("LOG_LEVEL") {
	case "ALL":
		w = os.Stdout
	default:
		w = bytes.NewBufferString(os.DevNull)
	}

	sec := security.Default(os.Getenv("SECRET"))

	db, err := db.New(os.Getenv("MYSQL"), sec)
	if err != nil {
		log.Fatal(err)
	}

	cached := cache.New(
		log.New(w, "[e3:cache] ", 0),
		os.Getenv("CACHE_KEY"),
		db,
		os.Getenv("MEMCACHE"),
	)

	u := user.New(
		log.New(w, "[e3:user] ", 0),
		cached,
	)

	app = App{
		log.New(w, "[e3] ", 0),
		upload.New(
			os.Getenv("URL"),
			cached,
			u,
		),
		send.New(
			log.New(w, "[e3:send] ", 0),
			cached,
		),
		u,
	}
}

func (app App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// TODO: Refactor routes to namespace as user is.

	urlParts := strings.Split(r.URL.Path, "/")
	if len(urlParts) > 1 {
		switch urlParts[1] {
		case "user":
			app.user.ServeHTTP(w, r)
			return
		case "invite":
			app.user.ServeHTTP(w, r)
			return
		}
	}

	switch r.Method {

	case "GET":
		switch r.URL.Path {
		case "/":
			app.user.ServeHTTP(w, r)
			return
		case "/favicon.ico":
			http.NotFound(w, r)
			return
		default:
			app.send.ServeHTTP(w, r)
			return
		}

	case "POST":
		switch r.URL.Path {
		case "/":
			app.user.ServeHTTP(w, r)
			return
		default:
			app.upload.ServeHTTP(w, r)
			return
		}

	case "OPTIONS":
		w.Header().Add("Access-Control-Allow-Origin", "*")
		w.Header().Add("Access-Control-Allow-Headers", "Authorization")
		w.WriteHeader(http.StatusOK)
		fmt.Fprintf(w, "pong")
		return

	default:
		http.NotFound(w, r)

	}
}

// for serverless deploy
// func ServeHTTP(w http.ResponseWriter, r *http.Request) {
// 	app.ServeHTTP(w, r)
// }

// for regular deploy
func main() {
	port := fmt.Sprintf(":%s", os.Getenv("PORT"))
	app.log.Println("Server has been started:", os.Getenv("URL"))
	log.Fatal(http.ListenAndServe(port, app))
}