Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import(
- "io"
- "net/http"
- "io/ioutil"
- "strings"
- "os"
- "fmt"
- )
- func ServeFile(w http.ResponseWriter, req *http.Request) {
- w.Header().Set("Strict-Transport-Security","max-age=315360000")
- address := req.Host
- parts := strings.Split(address,".")
- subdomain := parts[0]
- if subdomain == "www" && address !="www.domain.tld" {
- parts[0] = ""
- //io.WriteString(w,strings.Join(parts,"."))
- http.Redirect(w,req,"https://" + strings.TrimLeft(strings.Join(parts,".") + req.RequestURI,"."),http.StatusMovedPermanently)
- } else {
- if address == "domain.tld" {
- subdomain = "www"
- }
- path := "html/" + subdomain + req.RequestURI
- stat, err := os.Stat(path)
- if err==nil {
- if stat.IsDir() {
- if !strings.HasSuffix(path,"/") {
- path = path + "/"
- }
- stat,err := os.Stat(path + "index.gparse")
- if err==nil {
- path = path + "index.gparse"
- } else {
- path = path + "index.html"
- }
- }
- if strings.HasSuffix(path,".gparse") {
- //need that code here
- } else {
- content, err := ioutil.ReadFile(path)
- if err != nil {
- fmt.Println(err)
- } else {
- //io.WriteString(w,req.Host)
- io.WriteString(w,string(content))
- }
- }
- } else {
- w.WriteHeader(http.StatusNotFound)
- io.WriteString(w,"404 Not found! You are at: " + req.Host)
- }
- }
- }
- func RedToHttps(w http.ResponseWriter, req *http.Request) {
- http.Redirect(w,req,"https://" + req.Host + req.RequestURI, http.StatusMovedPermanently)
- }
- func main() {
- http.HandleFunc("/",ServeFile)
- go http.ListenAndServeTLS(":443","cert.pem","privkey.pem",nil)
- http.ListenAndServe(":80",http.HandlerFunc(RedToHttps))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement