-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.go
45 lines (37 loc) · 1.11 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
adapter "github.com/gsvaldevieso/go-dream-architecture/adapter/database"
"github.com/gsvaldevieso/go-dream-architecture/domain/entity"
"github.com/gsvaldevieso/go-dream-architecture/infrastructure/database"
"github.com/gsvaldevieso/go-dream-architecture/repository"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}
func main() {
mysql, _ := database.NewMySQL()
dbAdapter := adapter.NewRelational(mysql.DB)
orderRepo := repository.NewOrderRepo(dbAdapter)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
err := orderRepo.Store(entity.Order{ID: "ID"})
if err != nil {
response{http.StatusInternalServerError}.send(w)
return
}
response{http.StatusCreated}.send(w)
})
//http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
type response struct {
Status int `json:"status"`
}
func (r response) send(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(r.Status)
json.NewEncoder(w).Encode(r)
}