Skip to content

Commit

Permalink
Merge pull request #2 from aerth/develop
Browse files Browse the repository at this point in the history
merge in subdomain stuff
  • Loading branch information
aerth committed Feb 15, 2016
2 parents e6688ae + 0c52c3f commit 4af6427
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
42 changes: 28 additions & 14 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ func HomeHandler(w http.ResponseWriter, r *http.Request) {
// I love lamp. This displays affection for r.URL.Path[1:]

func LoveHandler(w http.ResponseWriter, r *http.Request) {
if getSubdomain(r) == "" {
fmt.Fprintf(w, "I love %s!", r.URL.Path[1:])
subdomain := getSubdomain(r)
log.Printf("Debug: domain is %s!", subdomain)
if subdomain == "127" {
fmt.Fprintf(w, "I love %s!", r.URL.Path[1:])
log.Printf("I love %s says %s at %s", r.URL.Path[1:], r.UserAgent(), r.RemoteAddr)
}else{
fmt.Fprintf(w, "%s loves %s!", getSubdomain(r), r.URL.Path[1:])
log.Printf("I love %s says %s at %s", getSubdomain(r), r.UserAgent(), r.RemoteAddr)
}

} else if subdomain == "" {
fmt.Fprintf(w, "I love %s!", r.URL.Path[1:])
log.Printf("I love %s says %s at %s", r.URL.Path[1:], r.UserAgent(), r.RemoteAddr)
} else {
fmt.Fprintf(w, "%s loves %s!", subdomain, r.URL.Path[1:])
log.Printf("I love %s says %s at %s", subdomain, r.UserAgent(), r.RemoteAddr)
}

}

Expand Down Expand Up @@ -154,15 +158,25 @@ func ParseQuery(query url.Values) *Form {
return form
}


func getSubdomain(r *http.Request) string {
type Subdomains map[string]http.Handler
domainParts := strings.Split(r.Host, ".")
log.Println(len(domainParts))
//log.Println("debug URL:"+ r.URL.RequestURI())
//log.Println("debug URL:"+
log.Println(r.Host)
hostparts := strings.Split(r.Host, ":")
requesthost := hostparts[0]
log.Println(requesthost)

domainParts := strings.Split(requesthost, ".")
//log.Println("domain parts:"+len(domainParts))
//log.Printf("debug: %s and %s", string(r.Host), domainParts[0] + domainParts[1] + domainParts[2])
if len(domainParts) > 3 {
return domainParts[0]
}else{
return ""
log.Println(domainParts[0])
if len(domainParts) > 2 {
if domainParts[0] != "127" {
return domainParts[0]
} else {
return ""
}
}
return ""
}
23 changes: 18 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"github.com/gorilla/mux"
"log"
"math/rand"
"net"
"net/http"
"net/http/fcgi"
"os"
"time"
)
Expand All @@ -16,7 +18,6 @@ var (
mandrillKey string
casgoDestination string
casgoAPIKey string
// subdomain string
)

func main() {
Expand All @@ -41,6 +42,8 @@ func main() {
debug := flag.Bool("debug", false, "be verbose, dont switch to logfile")
insecure := flag.Bool("insecure", false, "accept insecure cookie transfer")
mailbox := flag.Bool("mailbox", false, "save messages to an local mbox file")
fastcgi := flag.Bool("fastcgi", false, "use fastcgi")
bind := flag.String("bind", "127.0.0.1", "default: 127.0.0.1")
flag.Parse()

mandrillApiUrl = "https://mandrillapp.com/api/1.0/"
Expand Down Expand Up @@ -79,7 +82,6 @@ func main() {

http.Handle("/", r)


// Switch to file log so we can ctrl+c and launch another instance :)

if *mailbox == true {
Expand All @@ -94,12 +96,23 @@ func main() {
log.Println("debug on: [not using casgo.log]")
}

log.Println("info: Listening on", *port)

if *insecure == true {
if *fastcgi == true {
log.Println("fastcgi [on]")
log.Println("secure [off]")
listener, err := net.Listen("tcp", *bind+":"+*port)
if err != nil {
log.Fatal("Could not bind: ", err)
}
log.Println("info: Listening on", *port)
fcgi.Serve(listener, nil)
//log.Fatal(fcgi.Serve( listener, csrf.Protect([]byte("LI80PNK1xcT01jmQBsEyxyrNCrbyyFPjPU8CKnxwmCruxNijgnyb3hXXD3p1RBc0+LIRQUUbTtis6hc6LD4I/A=="), csrf.HttpOnly(true), csrf.Secure(false))(r)))

} else if *insecure == true {
log.Println("info: Listening on", *port)
log.Println("secure [off]")
log.Fatal(http.ListenAndServe(":"+*port, csrf.Protect([]byte("LI80PNK1xcT01jmQBsEyxyrNCrbyyFPjPU8CKnxwmCruxNijgnyb3hXXD3p1RBc0+LIRQUUbTtis6hc6LD4I/A=="), csrf.HttpOnly(true), csrf.Secure(false))(r)))
} else {
log.Println("info: Listening on", *port)
// Change this CSRF auth token in production!
log.Println("secure [on]")
log.Fatal(http.ListenAndServe(":"+*port, csrf.Protect([]byte("LI80PNK1xcT01jmQBsEyxyrNCrbyyFPjPU8CKnxwmCruxNijgnyb3hXXD3p1RBc0+LIRQUUbTtis6hc6LD4I/A=="), csrf.HttpOnly(true), csrf.Secure(true))(r)))
Expand Down

0 comments on commit 4af6427

Please sign in to comment.