Advertisement
Guest User

Go server

a guest
May 9th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import(
  4.   "io"
  5.   "net/http"
  6.   "io/ioutil"
  7.   "strings"
  8.   "os"
  9.   "fmt"
  10. )
  11.  
  12. func ServeFile(w http.ResponseWriter, req *http.Request) {
  13.   w.Header().Set("Strict-Transport-Security","max-age=315360000")
  14.   address := req.Host
  15.   parts := strings.Split(address,".")
  16.   subdomain := parts[0]
  17.   if subdomain == "www" && address !="www.domain.tld" {
  18.     parts[0] = ""
  19.     //io.WriteString(w,strings.Join(parts,"."))
  20.     http.Redirect(w,req,"https://" + strings.TrimLeft(strings.Join(parts,".") + req.RequestURI,"."),http.StatusMovedPermanently)
  21.   } else {
  22.     if address == "domain.tld" {
  23.       subdomain = "www"
  24.     }
  25.     path := "html/" + subdomain + req.RequestURI
  26.     stat, err := os.Stat(path)
  27.     if err==nil {
  28.       if stat.IsDir() {
  29.         if !strings.HasSuffix(path,"/") {
  30.           path = path + "/"
  31.         }
  32.         stat,err := os.Stat(path + "index.gparse")
  33.         if err==nil {
  34.           path = path + "index.gparse"
  35.         } else {
  36.           path = path + "index.html"
  37.         }
  38.       }
  39.       if strings.HasSuffix(path,".gparse") {
  40.         //need that code here
  41.       } else {
  42.         content, err := ioutil.ReadFile(path)
  43.         if err != nil {
  44.           fmt.Println(err)
  45.         } else {
  46.           //io.WriteString(w,req.Host)
  47.           io.WriteString(w,string(content))
  48.         }
  49.       }
  50.     } else {
  51.       w.WriteHeader(http.StatusNotFound)
  52.       io.WriteString(w,"404 Not found! You are at: " + req.Host)
  53.     }
  54.   }
  55. }
  56.  
  57. func RedToHttps(w http.ResponseWriter, req *http.Request) {
  58.   http.Redirect(w,req,"https://" + req.Host + req.RequestURI, http.StatusMovedPermanently)
  59. }
  60.  
  61. func main() {
  62.   http.HandleFunc("/",ServeFile)
  63.   go http.ListenAndServeTLS(":443","cert.pem","privkey.pem",nil)
  64.   http.ListenAndServe(":80",http.HandlerFunc(RedToHttps))
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement