~blowry/sourcehut-vanity

67bcd9feda0ad2d4d7ddac35d38c1cc0f0608d98 — Benjamin Lowry 1 year, 2 months ago 924adc0 master
spaces -> tabs

I was not aware at the time I wrote this code that tabs were the Golang
norm.
2 files changed, 59 insertions(+), 59 deletions(-)

M main.go
M vanityserver/vanityserver.go
M main.go => main.go +25 -25
@@ 1,35 1,35 @@
// sourcehut-vanity hosts an HTTP server that redirects HTTP requests to projects hosted on sourcehut. Example usage:
// 
//    $ sourcehut-vanity -u blowry -p 0.0.0.0:8080
//    # localhost:8080/sourcehut-vanity redirects to https://git.sr.ht/~blowry/sourcehut-vanity
//    # localhost:8080/sourcehut-vanity/README.md redirects to https://git.sr.ht/~blowry/sourcehut-vanity/tree/master/README.md
//    # localhost:8080/sourcehut-vanity?go-get=1 returns an HTML page with meta tags so go get can find the correct repository
// 
//
//	$ sourcehut-vanity -u blowry -p 0.0.0.0:8080
//	# localhost:8080/sourcehut-vanity redirects to https://git.sr.ht/~blowry/sourcehut-vanity
//	# localhost:8080/sourcehut-vanity/README.md redirects to https://git.sr.ht/~blowry/sourcehut-vanity/tree/master/README.md
//	# localhost:8080/sourcehut-vanity?go-get=1 returns an HTML page with meta tags so go get can find the correct repository
//
// Compatible with go-get(1) and standard browsers.
// SPDX-License-Identifier: GPL-3.0-or-later
package main

import (
    "fmt"
    "net/http"
    "git.sr.ht/~sircmpwn/getopt"
    "ben.gmbh/sourcehut-vanity/vanityserver"
    "os"
	"ben.gmbh/sourcehut-vanity/vanityserver"
	"fmt"
	"git.sr.ht/~sircmpwn/getopt"
	"net/http"
	"os"
)

func main() {
    var port string
    var user string
    getopt.StringVar(&port, "p", "0.0.0.0:8080", "address and port to bind to")
    getopt.StringVar(&user, "u", "", "username to redirect to")
    if e := getopt.Parse(); e != nil {
        panic(e)
    }
    if (user == "" || port == "") {
        fmt.Fprintf(os.Stderr, "usage: sourcehut-vanity -u username [-p 0.0.0.0:8080]\n")
        getopt.PrintDefaults()
        os.Exit(1)
    }
    fmt.Printf("Serving %v's projects on %v\n", user, port)
    fmt.Println(http.ListenAndServe(port, vanityserver.VanityServer{Username: user}))
	var port string
	var user string
	getopt.StringVar(&port, "p", "0.0.0.0:8080", "address and port to bind to")
	getopt.StringVar(&user, "u", "", "username to redirect to")
	if e := getopt.Parse(); e != nil {
		panic(e)
	}
	if user == "" || port == "" {
		fmt.Fprintf(os.Stderr, "usage: sourcehut-vanity -u username [-p 0.0.0.0:8080]\n")
		getopt.PrintDefaults()
		os.Exit(1)
	}
	fmt.Printf("Serving %v's projects on %v\n", user, port)
	fmt.Println(http.ListenAndServe(port, vanityserver.VanityServer{Username: user}))
}

M vanityserver/vanityserver.go => vanityserver/vanityserver.go +34 -34
@@ 1,33 1,33 @@
// VanityServer implements an HTTP.Handler that redirects HTTP requests to projects hosted on sourcehut.
// Example usage:
//
//    import (
//        "net/http"
//        "ben.gmbh/sourcehut-vanity/vanityserver"
//    )
//    func main() {
//        http.ListenAndServe(port, vanityserver.VanityServer{Username: "blowry"})
//    }
//	import (
//		"net/http"
//		"ben.gmbh/sourcehut-vanity/vanityserver"
//	)
//	func main() {
//		http.ListenAndServe(port, vanityserver.VanityServer{Username: "blowry"})
//	}
//
// SPDX-License-Identifier: GPL-3.0-or-later
package vanityserver

import (
    "fmt"
    "strings"
    "net/http"
    "html/template"
	"fmt"
	"html/template"
	"net/http"
	"strings"
)

type VanityServer struct {
    Username string
	Username string
}

type templatedata struct {
    User string
    ImportHost string
    ProjectName string
    Redir string
	User        string
	ImportHost  string
	ProjectName string
	Redir       string
}

const page = `<!DOCTYPE html>


@@ 44,23 44,23 @@ Nothing to see here; <a href="{{.Redir}}">move along</a>.
</html>`

func (server VanityServer) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
    tmpl := template.Must(template.New("page").Parse(page))
    fmt.Println("Hit: " + r.URL.Path)
    path := strings.Split(r.URL.Path, "/")
    projectname := path[1]
    baseurl := "https://git.sr.ht/~" + server.Username + "/" + projectname
    var destination string
    r.ParseForm()
	tmpl := template.Must(template.New("page").Parse(page))
	fmt.Println("Hit: " + r.URL.Path)
	path := strings.Split(r.URL.Path, "/")
	projectname := path[1]
	baseurl := "https://git.sr.ht/~" + server.Username + "/" + projectname
	var destination string
	r.ParseForm()

    if (len(path) > 2 && path[2] != "") {
        destination = baseurl + "/tree/master/" + strings.Replace(r.URL.Path, "/" + path[1] + "/", "", 1)
    } else {
        destination = baseurl
    }
    if (r.Form.Get("go-get") == "1") { // go-get(1)
        tmpl.Execute(rw, templatedata{User: server.Username, ImportHost: r.Host + "/" + projectname, ProjectName: projectname, Redir: destination})
    } else { // Standard browser
        http.Redirect(rw, r, destination, 302)
    }
    return
	if len(path) > 2 && path[2] != "" {
		destination = baseurl + "/tree/master/" + strings.Replace(r.URL.Path, "/"+path[1]+"/", "", 1)
	} else {
		destination = baseurl
	}
	if r.Form.Get("go-get") == "1" { // go-get(1)
		tmpl.Execute(rw, templatedata{User: server.Username, ImportHost: r.Host + "/" + projectname, ProjectName: projectname, Redir: destination})
	} else { // Standard browser
		http.Redirect(rw, r, destination, 302)
	}
	return
}