~eliasnaur/gio

ref: bf991992877b6f1f3f074c98fed5cadf455adde5 gio/website/main.go -rw-r--r-- 1.3 KiB View raw
bf991992Elias Naur apps/go.mod: update gio version 10 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
// SPDX-License-Identifier: Unlicense OR MIT

package main

import (
	"fmt"
	"log"
	"net/http"
	"os"
	"strings"
)

func main() {
	http.HandleFunc("/", vanityHandler)

	port := os.Getenv("PORT")
	if port == "" {
		port = "8080"
	}

	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}

// vanityHandler serves git location meta headers for the go tool.
func vanityHandler(w http.ResponseWriter, r *http.Request) {
	if www := "www."; strings.HasPrefix(r.URL.Host, www) {
		r.URL.Host = r.URL.Host[len(www):]
		http.Redirect(w, r, r.URL.String(), http.StatusMovedPermanently)
		return
	}
	if r.URL.Query().Get("go-get") == "1" {
		fmt.Fprintf(w, `<html><head>
<meta name="go-import" content="gioui.org git https://git.sr.ht/~eliasnaur/gio">
<meta name="go-source" content="gioui.org https://git.sr.ht/~eliasnaur/gio https://git.sr.ht/~eliasnaur/gio/tree/master{/dir} https://git.sr.ht/~eliasnaur/gio/tree/master{/dir}/{file}#L{line}">
</head></html>`)
		return
	}
	p := r.URL.Path
	switch {
	case p == "/":
		http.Redirect(w, r, "https://git.sr.ht/~eliasnaur/gio", http.StatusFound)
	case strings.HasPrefix(p, "/cmd"), strings.HasPrefix(p, "/ui"), strings.HasPrefix(p, "/apps"):
		http.Redirect(w, r, "https://godoc.org/gioui.org"+p, http.StatusFound)
	default:
		http.NotFound(w, r)
	}
}