Use sql connection in another func - mysql

How init mysql connection in main function and place it another function? Or init connection in another place and use it anywhere?
Something like this below(this example wrong!)
package main
import (
"fmt"
"net/http"
_ "github.com/go-sql-driver/mysql"
"database/sql"
)
func indexHandler(w http.ResponseWriter, r *http.Request) {
stmt, err := db.Prepare("INSERT userinfo SET username=?,departname=?,created=?")
res, err := stmt.Exec("test", "test", "test")
}
func main() {
db, err := sql.Open("mysql", "connection")
if err != nil {
panic(err)
}
http.HandleFunc("/", indexHandler)
http.ListenAndServe(":3000", nil)
}
Thanks anyway!

Make it global:
var db *sql.DB
func indexHandler(w http.ResponseWriter, r *http.Request) {
stmt, err := db.Prepare(...
...
}
func main() {
var err error
db, err = sql.Open("mysql", "connection")
...
or create handler as closure:
func createIndexHandler(db *sql.DB) (func (http.ResponseWriter, *http.Request)) {
return func(w http.ResponseWriter, r *http.Request) {
stmt, err := db.Prepare(...
...
}
}
func main() {
db, err := sql.Open("mysql", "connection")
...
http.HandleFunc("/", createIndexHandler(db))

Related

json: cannot unmarshal object into Go value of type []*main.Config

I'm new to golang and json, we are using gorilla mux library and I'd like to do a post request in postman. In config struct entries needs to be a map like that and in post server I need to have an array of *Config in postServer struct. I have 3 go files.
Service.go file is this:
package main
import (
"errors"
"github.com/gorilla/mux"
"mime"
"net/http"
)
type Config struct {
Id string `json:"id"`
entries map[string]string `json:"entries"`
}
type postServer struct {
data map[string][]*Config
}
func (ts *postServer) createPostHandler(w http.ResponseWriter, req *http.Request) {
contentType := req.Header.Get("Content-Type")
mediatype, _, err := mime.ParseMediaType(contentType)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if mediatype != "application/json" {
err := errors.New("Expect application/json Content-Type")
http.Error(w, err.Error(), http.StatusUnsupportedMediaType)
return
}
rt, err := decodeBody(req.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
id := createId()
ts.data[id] = rt
renderJSON(w, rt)
}
func (ts *postServer) getAllHandler(w http.ResponseWriter, req *http.Request) {
allTasks := []*Config{}
for _, v := range ts.data {
allTasks = append(allTasks, v...)
}
renderJSON(w, allTasks)
}
func (ts *postServer) getPostHandler(w http.ResponseWriter, req *http.Request) {
id := mux.Vars(req)["id"]
task, ok := ts.data[id]
if !ok {
err := errors.New("key not found")
http.Error(w, err.Error(), http.StatusNotFound)
return
}
renderJSON(w, task)
}
func (ts *postServer) delPostHandler(w http.ResponseWriter, req *http.Request) {
id := mux.Vars(req)["id"]
if v, ok := ts.data[id]; ok {
delete(ts.data, id)
renderJSON(w, v)
} else {
err := errors.New("key not found")
http.Error(w, err.Error(), http.StatusNotFound)
}
}
I wanted to test createPostHandler.
Then I have helper.go file where I decoded json into go and rendered into json:
package main
import (
"encoding/json"
"github.com/google/uuid"
"io"
"net/http"
)
func decodeBody(r io.Reader) ([]*Config, error) {
dec := json.NewDecoder(r)
dec.DisallowUnknownFields()
var rt []*Config
if err := dec.Decode(&rt); err != nil {
return nil, err
}
return rt, nil
}
func renderJSON(w http.ResponseWriter, v interface{}) {
js, err := json.Marshal(v)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
func createId() string {
return uuid.New().String()
}
and the last one go file is main.go where I have this:
package main
import (
"context"
"github.com/gorilla/mux"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
router := mux.NewRouter()
router.StrictSlash(true)
server := postServer{
data: map[string][]*Config{},
}
router.HandleFunc("/config/", server.createPostHandler).Methods("POST")
router.HandleFunc("/configs/", server.getAllHandler).Methods("GET")
router.HandleFunc("/config/{id}/", server.getPostHandler).Methods("GET")
router.HandleFunc("/config/{id}/", server.delPostHandler).Methods("DELETE")
// start server
srv := &http.Server{Addr: "0.0.0.0:8000", Handler: router}
go func() {
log.Println("server starting")
if err := srv.ListenAndServe(); err != nil {
if err != http.ErrServerClosed {
log.Fatal(err)
}
}
}()
<-quit
log.Println("service shutting down ...")
// gracefully stop server
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal(err)
}
log.Println("server stopped")
}
And JSON whad I did send is this:
{
"entries":["hello", "world"]
}
And error what I'm getting in postman is this:
json: cannot unmarshal object into Go value of type []*main.Config
I don't know what is a problem, maybe I'm sending wrong json or I just did something wrong in decodeBody, I needed to add [] in decodeBody in var rt []*Config because it wouldn't work otherwise.
Can someone help me to fix this please?
This is an example of how you can define a struct Config that you can parse your sample JSON into.
EDIT: field entries changed to map.
You can play with it on Playground.
package main
import (
"encoding/json"
"fmt"
)
type Config struct {
Id string `json:"id"`
Entries map[string]string `json:"entries"`
}
func main() {
str := `[{"id":"42", "entries":{"hello": "world"}}]`
var tmp []Config
err := json.Unmarshal([]byte(str), &tmp)
if err != nil {
fmt.Printf("error: %v", err)
}
var rt []*Config
for _, c := range tmp {
rt = append(rt, &c)
}
for _, c := range rt {
for k, v := range c.Entries {
fmt.Printf("id=%s key=%s value=%s\n", c.Id, k, v)
}
}
}

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

How to stop the repetition of code when parsing files

I am repeating the same code of html files in every function. I just changed the name of one file in every function and that is ui/html/home.html in indexHandler() and ui/html/signup.html in signup()
Other file names are the same and the code of ParseFiles() is also the same in each function. How to prevent the code from repeating every time.
func indexHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
files := []string{"ui/html/home.html", "ui/html/footer.html", "ui/html/base.html",}
tmpl, err := template.ParseFiles(files...)
if err != nil {
panic(err)
}
tmpl.Execute(w, nil)
}
func signup(w http.ResponseWriter, r *http.Request) {
files := []string{"ui/html/signup.html", "ui/html/footer.html", "ui/html/base.html",}
tmpl, err := template.ParseFiles(files...)
if err != nil {
panic(err)
}
tmpl.Execute(w, nil)
}
func main() {
http.HandleFunc("/", indexHandler)
http.HandleFunc("/signup", signup)
log.Println("Starting the server at :4000")
http.ListenAndServe(":4000", nil)
}
You can make it a package variable.
https://play.golang.org/p/haDmat_kzkg
package main
import (
"fmt"
)
var (
files = []string{"ui/html/home.html", "ui/html/footer.html", "ui/html/base.html"}
)
func indexHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
tmpl, err := template.ParseFiles(files...)
if err != nil {
panic(err)
}
tmpl.Execute(w, nil)
}
func signup(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFiles(files...)
if err != nil {
panic(err)
}
tmpl.Execute(w, nil)
}
func main() {
http.HandleFunc("/", indexHandler)
http.HandleFunc("/signup", signup)
log.Println("Starting the server at :4000")
http.ListenAndServe(":4000", nil)
}
If you need to add a template for a specific handler, create a new slice out of the global variable and append to it.
tmpl, err := template.ParseFiles(append(files,[]string{"..."})...)
I think you have to read a bit about effective go in general, https://golang.org/doc/effective_go#initialization
or the go tour https://tour.golang.org/welcome/1
A tip to apply is to compile the template during initialization, rather during the request processing. This will speed up your program.
package main
//...
var (
indexTpml = template.Must(template.ParseFiles(files...))
)
https://play.golang.org/p/TgjZYCul9M6
Using some helper functions, one can write,
package main
import (
"html/template"
"log"
"net/http"
)
func makeTpl(base string) *template.Template {
files := []string{base, "ui/html/footer.html", "ui/html/base.html"}
return template.Must(template.ParseFiles(files...))
}
var (
indexTpml = makeTpl("ui/html/home.html")
signupTpml = makeTpl("ui/html/signup.html")
)
func indexHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
indexTpml.Execute(w, nil)
}
func signup(w http.ResponseWriter, r *http.Request) {
signupTpml.Execute(w, nil)
}
func main() {
http.HandleFunc("/", indexHandler)
http.HandleFunc("/signup", signup)
log.Println("Starting the server at :4000")
http.ListenAndServe(":4000", nil)
}

How can I display the JSON response from the API in Go?

I'm invoking a small API that gives back a JSON response with a Todo object. How can I display that in my Go web server? Right now, all that's displaying is 'Todo', which I hard-coded. How can that be replaced by the API JSON response?
Here is the code:
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)
// Todo Structure
type Todo struct {
userID int
id int
title string
completed bool
}
func main() {
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "<h1>Todo</h1>")
})
response, err := http.Get("http://jsonplaceholder.typicode.com/todos/1")
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(responseData))
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}
You have to write a function delegated to retrieve the data from the external website. Than use the http.HandleFunc in order to expose the method at your endpoint, here an example:
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)
// Todo Structure
type Todo struct {
userID int
id int
title string
completed bool
}
func main() {
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, callHTTP())
})
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}
func callHTTP() string {
var (
response *http.Response
err error
responseData []byte
)
if response, err = http.Get("http://jsonplaceholder.typicode.com/todos/1"); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
if responseData, err = ioutil.ReadAll(response.Body); err != nil {
log.Fatal(err)
}
ret := string(responseData)
fmt.Println(ret)
return ret
}
Note:
Avoid to use the default http client in production. You can try some HTTP framework like GIN. I prefer fasthttp, here you can view some code as example: https://github.com/alessiosavi/StreamingServer

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)
}