-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
95 lines (81 loc) · 2.75 KB
/
main.go
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
package main
// ref/basic: https://golang.org/doc/articles/wiki/
// - form | r.FormValue("key")
// - redirect | http.Redirect(w, r, "/edit/"+title, http.StatusFound)
// - error | http.Error(w, err.Error(), http.StatusInternalServerError) | http.NotFound(w, r)
// - cache | var templates = template.Must(template.ParseFiles("edit.html", "view.html")); templates.ExecuteTemplate(w, tmpl+".html", p)
// ref/https: https://gist.github.com/denji/12b3a568f092ab951456
import (
"fmt"
"log"
"os"
"path"
"strconv"
"net/http"
)
type Server struct {
debug bool
serverStaticDir string
serverHttpsCADir string
serverHttpsCertFilename string
serverHttpsKeyFilename string
serverListen string
}
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/plain")
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}
func _downloadHandler(w http.ResponseWriter, r *http.Request) {
// http.ServeFile(w, r, "/path/to/a/file")
// type P struct { Body []byte }
// // template.html: <div>{{printf "%s" .Body}}</div>
// body, _ := ioutil.ReadFile("/path/to/a/file")
// p := &P{Body: body}
// t, _ := template.ParseFiles("/path/to/template.html")
// t.Execute(w, p)
}
func parseEnv() (s Server) {
s.debug = len(os.Getenv("TINY_DEBUG")) > 0
s.serverStaticDir = os.Getenv("TINY_STATIC_DIR")
s.serverHttpsCADir = os.Getenv("TINY_HTTPS_CA_DIR")
serverHost := os.Getenv("TINY_HOST")
serverPort, _ := strconv.Atoi(os.Getenv("TINY_PORT"))
if serverPort <= 0 {
serverPort = 8080
}
s.serverListen = fmt.Sprintf("%s:%d", serverHost, serverPort)
if len(s.serverHttpsCADir) > 0 {
s.serverHttpsCertFilename = path.Join(s.serverHttpsCADir, "ca.pem")
s.serverHttpsKeyFilename = path.Join(s.serverHttpsCADir, "ca.key")
fileInfo, err := os.Stat(s.serverHttpsCertFilename)
if os.IsNotExist(err) || fileInfo.IsDir() {
s.serverHttpsCertFilename = ""
s.serverHttpsKeyFilename = ""
}
fileInfo, err = os.Stat(s.serverHttpsKeyFilename)
if os.IsNotExist(err) || fileInfo.IsDir() {
s.serverHttpsCertFilename = ""
s.serverHttpsKeyFilename = ""
}
}
if len(s.serverStaticDir) > 0 {
fileInfo, err := os.Stat(s.serverStaticDir)
if os.IsNotExist(err) || !fileInfo.IsDir() {
s.serverStaticDir = ""
}
}
fmt.Printf("Server Config: %q\n", s)
return s
}
func main() {
config := parseEnv()
mux := http.NewServeMux()
mux.HandleFunc("/hello", handler)
fileServer := http.FileServer(http.Dir(config.serverStaticDir))
mux.Handle("/static/", http.StripPrefix("/static", fileServer))
if len(config.serverHttpsCertFilename) > 0 {
log.Fatal(http.ListenAndServeTLS(config.serverListen, config.serverHttpsCertFilename, config.serverHttpsKeyFilename, mux))
} else {
log.Fatal(http.ListenAndServe(config.serverListen, mux))
}
}