Blank page without errors when using post in go code - html

I'm new to Go programming and I am writing a function to post into an alternate key as the primary key auto-increments using Go, Mysql, and template.HTML package.
After troubleshooting for the last few days I am loading a blank html page.
Any suggestions on best practice or ways I should troubleshoot in the future would be so appreciated.
Thank you so much for any help you are willing to offer!
I used linters and formatters to ensure that the code is correctly formatted, used print statements, and rebuilt the function and html content from scratch to narrow it down.
HTTP handler
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"text/template"
_ "github.com/go-sql-driver/mysql"
)
type watchInfo struct {
ID int
Brand string
}
var (
tpl *template.Template
// cnn, err = sql.Open("mysql", "root:root#tcp(db:3306)/appdb")
)
//function to connect to db
func dbConn() (db *sql.DB) {
dbDriver := "mysql"
dbUser := "root"
dbPass := "root"
dbName := "appdb"
db, err := sql.Open(dbDriver, dbUser+":"+dbPass+"#tcp"+"(db:3306)/"+dbName)
if err != nil {
fmt.Println("dbConn not work")
}
return db
}
func init() {
tpl = template.Must(template.ParseGlob("/go/templates/*"))
}
func main() {
http.HandleFunc("/", index)
http.HandleFunc("/upload", uploadWatchInfo)
http.ListenAndServe(":8080", nil)
}
//This function get watch id and brand using getWatch function and passes them to the gohtml template file
func index(w http.ResponseWriter, r *http.Request) {
db := dbConn()
defer db.Close()
scanWatches, err := db.Query("SELECT * FROM watches order by id")
if err != nil {
log.Fatal(err)
}
err = db.Ping()
if err != nil {
fmt.Println(err.Error())
}
watch := watchInfo{}
watchSlice := []watchInfo{}
for scanWatches.Next() {
var id int
var brand string
err = scanWatches.Scan(&id, &brand)
if err != nil {
fmt.Println("sW.Scan didn't work")
}
watch.ID = id
watch.Brand = brand
watchSlice = append(watchSlice, watch)
}
tpl.ExecuteTemplate(w, "pic.gohtml", watchSlice)
}
func uploadWatchInfo(w http.ResponseWriter, r *http.Request) {
db := dbConn()
defer db.Close()
if r.Method == "POST" {
brand := r.FormValue("brand")
insForm, err := db.Prepare("INSERT INTO watches(brand) VALUES(?)")
if err != nil {
panic(err.Error)
}
insForm.Exec(brand)
err = tpl.ExecuteTemplate(w, "upload.gohtml", watchInfo{Brand: brand})
if err != nil {
panic(err.Error)
}
}
}
//this is handling an error and can be called in other page functions
func HandleError(w http.ResponseWriter, err error) {
if err != nil {
fmt.Println("Index did not work. error in index")
}
}
HTML Template
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Upload page</title>
</head>
<body>
<h1>upload page</h1>
<form method="POST" action="insert">
<label> brand </label><input type="text" name="brand"/><br />
<input type="submit" value="Save brand"/>
</form>
Index
</body>
</html>
I Expect my handler to insert the string value into the database and print the confirmation in the terminal.

Related

How to set up web server to perform POST Request in Go?

I want to set up a web server to perform a POST request. How does the post request get executed with the code below since only HandleFunc and ListenAndServe are defined in main function?
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
)
func post(w http.ResponseWriter, r *http.Request) {
const myurl string = "http://localhost:8000/"
request := strings.NewReader(`
{
"Name":"Tom",
"Age":"20"
}
`)
response, err := http.Post(myurl, "application/json", request)
content, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
fmt.Println(string(content))
defer response.Body.Close()
}
func main() {
http.HandleFunc("/", post)
log.Fatal(http.ListenAndServe(":8000", nil))
}
Here is a basic example of how you could go about it. I am using the same program to run both, the server and the client. This is just for demonstration purposes. You can of course make them separate programs.
// use struct to represent the data
// to recieve and send
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
// run the example
func main() {
// start the server in a goroutine
go server()
// wait 1 second to give the server time to start
time.Sleep(time.Second)
// make a post request
if err := client(); err != nil {
fmt.Println(err)
}
}
// basic web server to receive a request and
// decode the body into a user struct
func server() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
user := &Person{}
err := json.NewDecoder(r.Body).Decode(user)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
fmt.Println("got user:", user)
w.WriteHeader(http.StatusCreated)
})
if err := http.ListenAndServe(":8080", nil); err != http.ErrServerClosed {
panic(err)
}
}
// a simple client that posts a user to the server
func client() error {
user := &Person{
Name: "John",
Age: 30,
}
b := new(bytes.Buffer)
err := json.NewEncoder(b).Encode(user)
if err != nil {
return err
}
resp, err := http.Post("http://localhost:8080/", "application/json", b)
if err != nil {
return err
}
defer resp.Body.Close()
fmt.Println(resp.Status)
return nil
}
Here is the working example: https://go.dev/play/p/34GT04jy_uA

Seperate database connection function from the query function

I have been trying to execute queries with http://www.github.com/go-sql-driver/mysql and have been following this tutorial: https://tutorialedge.net/golang/golang-mysql-tutorial/
Now this tutorial does everything in 1 custom function, but I've tried to split up opening the database connection and the queries itself in seperate functions. The way I've done this is like this:
package main
import (
"fmt"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
type SqlResult struct {
id int
}
var db *sql.DB
func main() {
dbConnection:= openDatabaseConnection();
getID(dbConnection);
}
func openDatabaseConnection() *sql.DB {
db,err:= sql.Open("mysql","username:password#tcp(127.0.0.1:3306)/test");
if err != nil {
panic(err.Error())
}
defer db.Close()
return db
}
func getID(db *sql.Db) {
results,err:= db.Query("SELECT id FROM test")
for results.Next() {
var result SqlResult
err=results.Scan(&result.id)
if err != nil {
panic(err.Error())
}
fmt.Print(result.id)
}
}
This doesn't print out anything but also doesn't give an error.
Now when I put my code in 1 function, like this:
package main
import (
"fmt"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
type SqlResult struct {
id int
}
var db *sql.DB
func main() {
openDatabaseConnection();
}
func openDatabaseConnection() *sql.DB {
db,err:= sql.Open("mysql","username:password#tcp(127.0.0.1:3306)/test");
if err != nil {
panic(err.Error())
}
results,err:= db.Query("SELECT id FROM test")
for results.Next() {
var result SqlResult
err=results.Scan(&result.id)
if err != nil {
panic(err.Error())
}
fmt.Print(result.id)
}
defer db.Close()
return db
}
This returns me all the id's, but I want to split the functionality up. It doesn't seem like opening the database connection everytime I need a new query is the right solution.
Does anyone know what I'm doing wrong in my first example and if so, what would be the right way of the way I want it to be?
"what would be the right way of the way I want it to be?"
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
var db *sql.DB
func main() {
db = openDatabaseConnection()
defer db.Close()
printIds(db)
}
func openDatabaseConnection() *sql.DB {
db, err := sql.Open("mysql", "username:password#tcp(127.0.0.1:3306)/test")
if err != nil {
panic(err)
} else if err := db.Ping(); err != nil {
panic(err)
}
return db
}
func printIds(db *sql.DB) {
rows, err := db.Query("SELECT id FROM test")
if err != nil {
panic(err)
}
defer rows.Close()
for rows.Next() {
var id int
if err := rows.Scan(&id); err != nil {
panic(err)
}
fmt.Println(id)
}
if err := rows.Err(); err != nil {
panic(err)
}
}

How to get values from a variable of type *sql.Rows in the view?

In posts table, there are attributes like title, content.
I can get data of type *sql.Rows and pass them to the view by
posts, err := db.Query("SELECT id, title FROM posts WHERE id = 1")
and
err = tpl.ExecuteTemplate(w, "index.gohtml", posts)
but I couldn't display the title value in the view. Here is my code.
index.go
package main
import (
"net/http"
"fmt"
"log"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
func index(w http.ResponseWriter, r *http.Request) {
db, err := sql.Open("mysql", "root:****#/database")
if err != nil {
panic(err.Error())
}
defer db.Close()
posts, err := db.Query("SELECT id, title FROM posts WHERE id = 1")
var id int
var title string
for posts.Next() {
err = posts.Scan(&id, &title)
if err != nil {
panic(err.Error())
}
}
fmt.Println(posts)
defer posts.Close()
err = tpl.ExecuteTemplate(w, "index.gohtml", posts)
if err != nil {
log.Println(err)
}
}
index.gohtml
<h1>Awesome Posts</h1>
{{$posts := .}}
<p>{{$posts}}</p>
There are a few mistakes in your code and I think you have misunderstood how to extract data using the sql package.
As Flimzy said in the comments, you should pass a proper context struct which contains your ID and Title values somewhere.
If you check the docs for sql.Rows you will see how to extract the data at each of the rows from your query...and in fact you already know how to get the rows and column values - by using the Next() and Scan() methods. But this should not be done by code in the HTML template, it should store the results in some variable which is passed to the template.
Quick Answer
A quick answer to your question would be to change how you pass the values into the template and amend the template. Seeing as you declare id and title variables you should pass them to the template:
err = tpl.ExecuteTemplate(w, "index.gohtml", map[string]interface{}{"ID": id,"Title": title})
if err != nil {
log.Println(err)
}
<h1>Awesome Posts</h1>
<p>{{.ID}} - {{.Title}}</p>
Having a Post model
A better solution would be to have a struct which holds all of the properties of the post, and use this to Scan into.
type Post struct{
ID int
Title string
}
...
var p Post
...
_ = rows.Scan(&p)
However, there is another problem with the way you are storing the results of the query. You are using db.Query to return a single row - This is an assumption as you have WHERE ID=1. If you expect only one post to be returned then use the QueryRow method: (N.B. you can chain on the Scan method for simplicity)
var p Post
// error handling removed for brevity
_ = db.QueryRow("SELECT id, title FROM posts WHERE id = 1").Scan(&p)
_ = tpl.ExecuteTemplate(w, "index.gohtml", p)
If, however, you are expecting to retrieve multiple posts (and you were just adding the where clause for simplicity), then you need to Scan into a Post struct, and append into a slice of Posts.
rows, _ := db.Query("SELECT id, title FROM posts")
defer rows.Close()
var posts []Post
for rows.Next() {
var p Post
_ = posts.Scan(&id, &p) // you should handle error here.
posts = append(posts, p)
}
if err = tpl.ExecuteTemplate(w, "index.gohtml", posts); err!=nil{
log.Println(err)
}
Other considerations
You should not be creating a connection to the DB within the HTTP handler. One way is to have a global variable which holds the connection. A struct with embedded connection can work and/or it is possible to abstract away the connection into a package as well.
/db/db.go
package db
import (
"database/sql"
// MYSQL driver
_ "github.com/go-sql-driver/mysql"
)
var db *sql.DB
// Open handles the opening of the DB
func Open(connstr string) (err error) {
db, err = sql.Open("mysql", connstr)
if err != nil {
return err
}
return nil
}
// Close handles the closing of the DB
func Close() error {
return db.Close()
}
/db/posts.go
package db
// Post model
type Post struct {
ID uint
Title string
Body string
}
const (
getPosts = `SELECT id, title, body FROM posts`
getAPost = `SELECT id, title, body FROM posts WHERE id=?`
)
// GetPosts will return all posts from the DB
func GetPosts() ([]Post, error) {
rows, err := db.Query(getPosts)
if err != nil {
return nil, err
}
var posts []Post
for rows.Next() {
var p Post
if err := rows.Scan(&p.ID, &p.Title, &p.Body); err != nil {
return nil, err
}
posts = append(posts, p)
}
return posts, nil
}
// GetPost will return single post identified by ID from the DB
func GetPost(id uint) (Post, error) {
var p Post
if err := db.QueryRow(getAPost, id).Scan(&p.ID, &p.Title, &p.Body); err != nil {
return p, err
}
return p, nil
}
main.go
import (
"chilledoj/sopost/db" // this is the absolute path to the db folder
"html/template"
"log"
"net/http"
"strconv"
"flag"
"github.com/gorilla/mux"
)
var dbconn string
func init() {
flag.StringVar(&dbconn, "dbconn", "", "MYSQL DB Connection string")
flag.Parse()
}
func main() {
if dbconn == "" {
log.Fatal("DB Connection string not set")
}
if err := db.Open(dbconn); err != nil {
log.Fatal(err)
}
defer db.Close()
r := mux.NewRouter()
r.HandleFunc("/", indexHandler())
r.HandleFunc("/posts", postsHandler())
r.HandleFunc("/posts/{id}", postHandler())
if err := http.ListenAndServe(":8080", r); err != nil {
log.Panic(err)
}
}
var indexHandler = func() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(`<h1>Welcome</h1>Posts`))
}
}
var postsHandler = func() http.HandlerFunc {
tmpl, err := template.New("posts").Parse(`<h1>Awesome Posts</h1>
<ul>{{range .}}
<li>{{.Title}}</li>
{{end}}</ul>
<hr/>
Home`)
if err != nil {
log.Panic(err)
}
return func(w http.ResponseWriter, r *http.Request) {
posts, err := db.GetPosts()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
err = tmpl.Execute(w, posts)
if err != nil {
log.Printf("There was a template Error.\n%v\n", err)
}
}
}
var postHandler = func() http.HandlerFunc {
tmpl, err := template.New("posts").Parse(`<h1>Awesome Posts</h1>
<h2>{{.Title}}</h2>
<p>{{.Body}}</p>
<hr/>
Home
Posts`)
if err != nil {
log.Panic(err)
}
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := strconv.ParseInt(vars["id"], 10, 32)
if err != nil {
http.NotFound(w, r)
return
}
post, err := db.GetPost(uint(id))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
tmpl.Execute(w, post)
}
}
Run the above using
go run main.go -dbconn [dbuser]:[dbpass]#/[dbname]?parseTime=true
Another way is to use dependency injection and have a function take in a db connection but return an http.HandlerFunc. e.g.
var indexHandler = function (db *sql.DB) http.HandlerFunc{
return function(w http.ResponseWriter, r *http.Request){
// now have access to db
}
}
http.HandleFunc("/posts", indexHandler())

How to send variable and its value to html

I have written a small piece of code in go
func loginHandler(w http.ResponseWriter, r *http.Request) {
log.Println("loginHandler")
log.Println("request url is", r.RequestURI)
log.Println("request method", r.Method)
requestbody, _ := ioutil.ReadAll(r.Body)
log.Println("request body is", string(requestbody))
if r.Method == "POST" {
us, err := globalSessions.SessionStart(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
us.Set("LoggedInUserID", "000000")
w.Header().Set("Location", "/auth")
w.WriteHeader(http.StatusFound)
return
}
outputHTML(w, r, "static/login.html")
}
func outputHTML(w http.ResponseWriter, req *http.Request, filename string) {
log.Println("outputHTML")
requestbody, _ := ioutil.ReadAll(req.Body)
log.Println("request body is", string(requestbody))
log.Println("request body is", requestbody)
file, err := os.Open(filename)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
defer file.Close()
fi, _ := file.Stat()
http.ServeContent(w, req, file.Name(), fi.ModTime(), file)
}
in this code i am redirecting to login.html . now i want to send a variable let it be some string called testvariable and its value to login.html.
To be able to display values in your html you can use Go's html/template package.
First you'll need to specify where in the html page you want your values to appear, using the html/template package you can do that with template actions.
"Actions"--data evaluations or control structures--are delimited by
"{{" and "}}"
Next you'll need to drop the http.ServeContent function as that does not know how to render templates, instead you can use Execute to display the login page together with your values.
Here's an example:
login.html:
<html>
<body>
<h1>{{.MyVar}}</h1>
</body>
</html>
outputHTML:
func outputHTML(w http.ResponseWriter, filename string, data interface{}) {
t, err := template.ParseFiles(filename)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
if err := t.Execute(w, data); err != nil {
http.Error(w, err.Error(), 500)
return
}
}
And your loginHandler:
func loginHandler(w http.ResponseWriter, r *http.Request) {
// do whatever you need to do
myvar := map[string]interface{}{"MyVar": "Foo Bar Baz"}
outputHTML(w, "static/login.html", myvar)
}
Read more on templates here: html/template and for information about how to program the templates themselves, see the documentation for text/template

I need help on this Golang web application using mysql as database

I am new to Golang and had been following some tutorials and I want to put into practice what I have learned to create a website
This is the main.go file
package main
import (
"html/template"
"net/http"
"log"
"database/sql"
_"github.com/go-sql-driver/mysql"
)
//Fetch all templates
var templates, templatesErr = template.ParseGlob("templates/*")
func main() {
PORT := ":9000"
log.Println("Listening to port", PORT)
http.HandleFunc("/", root)
http.HandleFunc("/facilities", allFacilities)
http.ListenAndServe(PORT, nil)
}
func root(w http.ResponseWriter, r *http.Request) {
rootData := make(map[string]string)
rootData["page_title"] = "iSpace Open Data"
rootData["body"] = ""
templates.ExecuteTemplate(w, "index.html", rootData)
}
type facility struct{
FacilityName string
Type string
}
func allFacilities(w http.ResponseWriter, r *http.Request){
db, err := sql.Open("mysql", "root:08swanzy#tcp(127.0.0.1:3306)/iod")
if err !=nil{
log.Fatal(err)
}
defer db.Close()
rows, err := db.Query("Select FacilityName, Type from health_facilities ")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
fac := facility{}
facilities := []facility{}
for rows.Next(){
var FacilityName, Type string
rows.Scan(&FacilityName, &Type)
fac.FacilityName= FacilityName
fac.Type= Type
facilities = append(facilities, fac)
}
templates.ExecuteTemplate(w, "facilities.html", facilities)
}
This uses html files in templates folder for the view. But I keep on getting runtime error saying it has pointer dereference. I need help please.
Tried your code and got the same error. It happened on this line:
templates.ExecuteTemplate(w, "index.html", rootData)
The problem is that your templates are not loaded correctly. I moved template parsing to the main function and it works. Here the relevant code snippet:
//Fetch all templates
var (
templates *template.Template
)
func main() {
var err error
templates, err = template.ParseGlob("templates/*")
if err != nil {
panic(err)
}
PORT := ":9000"
log.Println("Listening to port", PORT)
http.HandleFunc("/", root)
http.HandleFunc("/facilities", allFacilities)
http.ListenAndServe(PORT, nil)
}