handler.go (1699B)
1 /* 2 Copyright 2021 Mark Wilkerson 3 4 This program is free software: you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation, either version 3 of the License. 7 8 This program is distributed in the hope that it will be useful, 9 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License 14 along with this program. If not, see <https://www.gnu.org/licenses/> 15 */ 16 package main 17 18 import ( 19 "fmt" 20 "log" 21 "net/http" 22 "strings" 23 ) 24 25 const logfmt = "%s %s %s %d %s" 26 27 func handler(opts options) http.HandlerFunc { 28 29 return func(w http.ResponseWriter, r *http.Request) { 30 31 // This should only ever be an http GET 32 // Anything else is shenanigans 33 if r.Method != http.MethodGet { 34 w.WriteHeader(http.StatusMethodNotAllowed) 35 log.Printf(logfmt, r.RemoteAddr, r.Method, r.URL, http.StatusMethodNotAllowed, r.UserAgent()) 36 return 37 } 38 39 qp := r.URL.Query() 40 41 if qp.Get("go-get") == "1" { 42 log.Printf(logfmt, r.RemoteAddr, r.Method, r.URL, http.StatusOK, r.UserAgent()) 43 44 host := strings.Split(r.Host, ":")[0] 45 repo := r.URL.Path 46 47 fmt.Fprintf(w, `<meta name="go-import" content="%s%s git %s%s.git">`, host, repo, opts.Dest, repo) 48 49 if opts.Debug { 50 log.Printf(`Sent: <meta name="go-import" content="%s%s git %s%s.git">`, host, repo, opts.Dest, repo) 51 } 52 53 } else { 54 55 log.Printf(logfmt, r.RemoteAddr, r.Method, r.URL, http.StatusNotFound, r.UserAgent()) 56 w.WriteHeader(http.StatusNotFound) 57 58 } 59 } 60 }