-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.go
71 lines (61 loc) · 2.11 KB
/
webserver.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
package main
import (
"fmt"
"net/http"
"regexp"
"strings"
"log"
"flag"
)
func main() {
portNr := flag.Int("http", 0, "http port")
portNrTLS := flag.Int("https", 8443, "https port")
flag.Parse()
http.HandleFunc("/", MainServer)
http.HandleFunc("/git/", GitServer)
fmt.Println(*portNr, ",", *portNrTLS)
if *portNr != 0 {
fmt.Printf("Listening on localhost:%d\n", *portNr)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *portNr), nil))
} else {
fmt.Println("Listening on secure localhost:%d\n", *portNrTLS)
log.Fatal(http.ListenAndServeTLS(fmt.Sprintf(":%d", *portNrTLS), "./sslforfree/certificate.crt", "./sslforfree/private.key", nil))
}
}
func MainServer(w http.ResponseWriter, r *http.Request) {
fmt.Print("Tried to access ", r.URL.Path, " ...")
var p string
r1 := regexp.MustCompile("(?:^/(?P<path>(?:uio))?(?P<subpath>/?.*/)?(?P<site>.+\\.(?P<filetype>html|css|js)?)?$)?")
match := r1.FindStringSubmatch(r.URL.Path)
fullmatch, path, subpath, site, filetype := match[0], match[1], match[2], match[3], match[4]
fmt.Printf("Path: (%s), Subpath: (%s), Site: (%s) with (%s)\n", path, subpath, site, filetype)
if site == "" {
site = "index.html"
}
if fullmatch == "/" {
p = "./content/index.html"
} else if filetype == "css" {
p = "./assets/" + site
} else {
p = fmt.Sprintf("./content/%s%s", subpath, site)
}
fmt.Println("Serving:", p)
http.ServeFile(w, r, p)
}
func GitServer(w http.ResponseWriter, r *http.Request) {
fmt.Print("Git tried to access ", r.URL.Path, " ...")
var p string
orderedPath := strings.Split(r.URL.Path[1:], "/") // [git, f, <repo>, ...<path>]
fmt.Println(orderedPath)
if orderedPath[1] == "f" {
p = "https://github.com/mazunki/" + orderedPath[2] + "/blob/master/" + strings.Join(orderedPath[3:],"/") + "?raw=True"
} else if len(orderedPath)>2 {
p = "https://github.com/mazunki/" + orderedPath[1] + "/blob/master/" + strings.Join(orderedPath[2:],"/")
} else if len(orderedPath) == 2 {
p = "https://github.com/mazunki/" + orderedPath[1]
} else {
p = "https://github.com/mazunki/"
}
fmt.Println("Redirecting to", p)
http.Redirect(w, r, p, http.StatusSeeOther)
}