prettify

baseline

Web Server in Go

package main

import (
       
"log"
       
"net/http"
       
"strings"
)
func handler
(w http.ResponseWriter, r *http.Request) {
       
var sPath string
       
if r.Method == "GET" {
                log
.Println(r.URL.Path)
               
if strings.HasPrefix(r.URL.Path, "/") {
                        sPath
= r.URL.Path[1:]
               
} else {
                        sPath
= r.URL.Path
               
}
                http
.ServeFile(w, r, sPath)
       
}
}
func main
() {
        log
.Printf("About to listen on localhost. Go to http://localhost")
        http
.HandleFunc("/", handler)
        err
:= http.ListenAndServe(":80", nil)
       
if err != nil {
                log
.Fatal("ListenAndServe: ", err)
       
}
}

No comments:

Post a Comment