-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
131 lines (112 loc) · 2.87 KB
/
main.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"encoding/json"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"log"
"net/http"
"time"
)
var db *gorm.DB
type User struct {
gorm.Model
Username string
Password string
EMail string
}
type UserJson struct {
Username string `json:"username"`
Password string `json:"password"`
EMail string `json:"email"`
}
func main() {
log.Println("Start Software")
//Migrate and Connect
db = migrateConnect()
// Start REST API
buildRestAPI()
}
func buildRestAPI() {
router := mux.NewRouter()
log.Println("Serving at localhost:80...")
// User Authentification Routes
router.HandleFunc("/api/user", getUser).Methods("GET")
router.HandleFunc("/api/user", createUser).Methods("POST")
http.Handle("/", router)
log.Fatal(http.ListenAndServe(":80", nil))
}
func migrateConnect() *gorm.DB {
// Wait until database is succesfully up
time.Sleep(5 * time.Second)
// Connect to Database
var globalDB *gorm.DB
var err error
//for connected {
globalDB, err = gorm.Open("mysql", "root:g240iowejgadvsijfgwrgafd@tcp(db:3306)/golangstarter?charset=utf8&parseTime=true&loc=Local")
if err != nil {
log.Println("failed to connect database ... retry")
log.Println(err)
} else {
log.Println("Successfully connect to database")
// Run Migrations
errors := globalDB.AutoMigrate(&User{}).Error
if errors != nil {
log.Println("Got errors")
log.Println(errors)
}
}
return globalDB
}
func getUser(w http.ResponseWriter, r *http.Request) {
// Build Response
w.Header().Add("Content-Type", "application/json")
// Get all users from Database
var user []User
errDB := db.Find(&user).Error
if errDB != nil {
err := json.NewEncoder(w).Encode("Database Error")
if err != nil {
log.Println(err)
}
} else {
// Send Response
err := json.NewEncoder(w).Encode(user)
if err != nil {
log.Println(err.Error())
}
}
}
func createUser(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
// Parse User Authentification
var jsonuser UserJson
err := json.NewDecoder(r.Body).Decode(&jsonuser)
if err != nil {
log.Println("ERROR DECODING JSON")
log.Println(err.Error())
}
var user User
user.EMail = jsonuser.EMail
user.Password = jsonuser.Password
user.Username = jsonuser.Username
if db.Where(&User{EMail: user.EMail}).First(&user).RecordNotFound() {
// Create the USer
err := db.Create(&user).Error
if err != nil {
log.Println(err.Error())
log.Println("Failed to create user in database")
err := json.NewEncoder(w).Encode("Failed to create user in database")
if err != nil {
log.Println(err.Error())
}
}
} else {
log.Println("Failed to create user, e-mail already exists")
err := json.NewEncoder(w).Encode("Failed to create user, e-mail already exists")
if err != nil {
log.Println(err.Error())
}
}
}