Serge Bazanski | b9ed123 | 2021-05-12 21:05:05 +0000 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "flag" |
| 5 | "fmt" |
| 6 | "mime" |
| 7 | "net/http" |
| 8 | "strings" |
| 9 | |
| 10 | "github.com/golang/glog" |
| 11 | |
| 12 | "code.hackerspace.pl/hscloud/hswaw/cebulacamp/landing/static" |
| 13 | ) |
| 14 | |
| 15 | var ( |
| 16 | flagBind string |
| 17 | ) |
| 18 | |
| 19 | func init() { |
| 20 | flag.Set("logtostderr", "true") |
| 21 | } |
| 22 | |
| 23 | func main() { |
| 24 | flag.StringVar(&flagBind, "bind", "0.0.0.0:8080", "Address at which to serve HTTP requests") |
| 25 | flag.Parse() |
| 26 | http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 27 | path := r.URL.Path |
| 28 | if path == "/" { |
| 29 | path = "/index.html" |
| 30 | } |
| 31 | path = strings.TrimPrefix(path, "/") |
| 32 | staticPath := fmt.Sprintf("hswaw/cebulacamp/landing/%s", path) |
| 33 | if data, ok := static.Data[staticPath]; ok { |
| 34 | parts := strings.Split(path, ".") |
| 35 | ext := fmt.Sprintf(".%s", parts[len(parts)-1]) |
| 36 | t := mime.TypeByExtension(ext) |
| 37 | w.Header().Set("Content-Type", t) |
| 38 | w.Write(data) |
| 39 | } else { |
| 40 | http.NotFound(w, r) |
| 41 | } |
| 42 | }) |
| 43 | |
| 44 | glog.Infof("Starting up at %v", flagBind) |
| 45 | err := http.ListenAndServe(flagBind, nil) |
| 46 | if err != nil { |
| 47 | glog.Exit(err) |
| 48 | } |
| 49 | } |