Created
January 11, 2020 02:34
-
-
Save monkrus/d4f1569a458ccf9a8d798d3cb7311a80 to your computer and use it in GitHub Desktop.
Go HTTP services
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"html/template" | |
"net/http" | |
"time" | |
) | |
type Welcome struct { | |
Name string | |
Time string | |
} | |
func main() { | |
welcome := Welcome{"Anonymous", time.Now().Format(time.Stamp)} | |
templates := template.Must(template.ParseFiles("templates/welcome-template.html")) | |
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
if name := r.FormValue("name"); name != "" { | |
welcome.Name = name | |
} | |
if err := templates.ExecuteTemplate(w, "welcome-template.html", welcome); err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
} | |
}) | |
fmt.Println("Serving on PORT 8080") | |
fmt.Println(http.ListenAndServe(":8080", nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment