Skip to content

Instantly share code, notes, and snippets.

@monkrus
Created January 11, 2020 02:34
Show Gist options
  • Save monkrus/d4f1569a458ccf9a8d798d3cb7311a80 to your computer and use it in GitHub Desktop.
Save monkrus/d4f1569a458ccf9a8d798d3cb7311a80 to your computer and use it in GitHub Desktop.
Go HTTP services
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