This sample code mark the denisenkom import with squirky line and in the terminal it only display Starting server at port 8080. I’m able to view the index.html and post the form.
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
// import mssql database driver/library
_ "github.com/denisenkom/go-mssqldb"
)
// requets send by user to server
// response server return to user
// r pointer to request
func upload_json(rw http.ResponseWriter, r *http.Request) {
fmt.Fprintf(rw, "Post request successful 14")
// validae url to upload function
if r.URL.Path != "/upload_json" {
http.Error(rw, "403 not found", http.StatusNotFound)
return
}
// validate form data if valid
if err := r.ParseForm(); err != nil {
fmt.Fprint(rw, "upload_json() err: %w", err)
return
}
fmt.Fprintf(rw, "Post request successful")
// get and set form.input[data] value
data := r.FormValue("data")
fmt.Fprintf(rw, "Data = %s", data)
}
var Db *sql.DB
func main() {
var (
server = "localhost"
port = 1433
user = "golang"
pwd = "1234abcD"
database = "Northwind"
)
var err error
// route to handle the default page such as index.html
fs := http.FileServer(http.Dir("./static"))
http.Handle("/", fs)
// route to handle the upload function
http.HandleFunc("/upload_json", upload_json)
// Server status notification
fmt.Printf("Starting server at port 8080\n")
log.Fatal(http.ListenAndServe(":8080", nil))
// create the connection pool
fmt.Printf("Starting mssql connection pool\n")
// create the connection string for ms sql
connString := fmt.Sprintf("server=%s; user id=%s; password=%s; port=%d; database=%s; ",
server, user, pwd, port, database)
Db, err = sql.Open("mssql", connString)
if err != nil {
fmt.Printf("Error creating connection pool :" + err.Error())
}
fmt.Printf("Connected to MSSQL")
}