Last active
December 9, 2020 10:19
-
-
Save martinomburajr/674f815aa9450ce9e3cd05edc7cf567e to your computer and use it in GitHub Desktop.
medium-building-a-go-web-app-from-scratch-to-deploying-on-google-cloud-welcome-app-main.go
This file contains hidden or 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 ( | |
| "net/http" | |
| "fmt" | |
| "time" | |
| "html/template" | |
| ) | |
| //Create a struct that holds information to be displayed in our HTML file | |
| type Welcome struct { | |
| Name string | |
| Time string | |
| } | |
| //Go application entrypoint | |
| func main() { | |
| //Instantiate a Welcome struct object and pass in some random information. | |
| //We shall get the name of the user as a query parameter from the URL | |
| welcome := Welcome{"Anonymous", time.Now().Format(time.Stamp)} | |
| //We tell Go exactly where we can find our html file. We ask Go to parse the html file (Notice | |
| // the relative path). We wrap it in a call to template.Must() which handles any errors | |
| // and halts if there are fatal errors | |
| templates := template.Must(template.ParseFiles("templates/welcome-template.html")) | |
| //Our HTML comes with CSS that go needs to provide when we run the app. Here we tell go to create | |
| // a handle that looks in the static directory, go then uses the "/static/" as a url that our | |
| //html can refer to when looking for our css and other files. | |
| http.Handle("/myPath/", //final url can be anything | |
| http.StripPrefix("/static/", | |
| http.FileServer(http.Dir("static")))) //Go looks in the relative static directory first, then matches it to a | |
| //url of our choice as shown in http.Handle("/static/"). | |
| //This url is what we need when referencing our css files | |
| //once the server begins. Our html code would therefore be <link rel="stylesheet" href="/static/stylesheet/..."> | |
| //It is important to note the final url can be whatever we like, so long as we are consistent. | |
| //This method takes in the URL path "/" and a function that takes in a response writer, and a http request. | |
| http.HandleFunc("/" , func(w http.ResponseWriter, r *http.Request) { | |
| //Takes the name from the URL query e.g ?name=Martin, will set welcome.Name = Martin. | |
| if name := r.FormValue("name"); name != "" { | |
| welcome.Name = name; | |
| } | |
| //If errors show an internal server error message | |
| //I also pass the welcome struct to the welcome-template.html file. | |
| if err := templates.ExecuteTemplate(w, "welcome-template.html", welcome); err != nil { | |
| http.Error(w, err.Error(), http.StatusInternalServerError) | |
| } | |
| }) | |
| //Start the web server, set the port to listen to 8080. Without a path it assumes localhost | |
| //Print any errors from starting the webserver using fmt | |
| fmt.Println(http.ListenAndServe(":8080", nil)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment