Browse Source

first commit

master
Alfred 2 years ago
commit
e02dc029c3
9 changed files with 192 additions and 0 deletions
  1. 1
    0
      .gitignore
  2. 10
    0
      README.md
  3. 41
    0
      config/config.go
  4. 55
    0
      controllers/person.go
  5. 19
    0
      controllers/routes.go
  6. 28
    0
      db/db.go
  7. 7
    0
      db/models.go
  8. 7
    0
      defconfig.json
  9. 24
    0
      main.go

+ 1
- 0
.gitignore View File

@@ -0,0 +1 @@
1
+.idea/

+ 10
- 0
README.md View File

@@ -0,0 +1,10 @@
1
+# MACARRODES
2
+
3
+## Start 
4
+
5
+```
6
+$ go get github.com/gin-gonic/gin
7
+$ go get -u github.com/jinzhu/gorm
8
+$ go get -u github.com/go-sql-driver/mysql
9
+$ go get github.com/spf13/viper
10
+```

+ 41
- 0
config/config.go View File

@@ -0,0 +1,41 @@
1
+package config
2
+
3
+import (
4
+	"log"
5
+	"os"
6
+	"encoding/json"
7
+)
8
+
9
+type Configuration struct {
10
+	Name string
11
+	Release bool
12
+	Database struct {
13
+		ConnectionString string `json:"connection_string"`
14
+	} `json:"datarbase"`
15
+}
16
+
17
+const configEnvVar = "CONFIG"
18
+const defaultConfigFile = "defconfig.json"
19
+var config Configuration
20
+
21
+func init() {
22
+	confpath := os.Getenv(configEnvVar)
23
+	if confpath == "" {
24
+		confpath = defaultConfigFile
25
+	}
26
+
27
+	file, _ := os.Open(confpath)
28
+	decoder := json.NewDecoder(file)
29
+	err := decoder.Decode(&config)
30
+
31
+	if err != nil {
32
+		log.Fatal("Error reading config file: ", err)
33
+		panic(err)
34
+	}
35
+
36
+	log.Println("Loaded config: ", config.Name)
37
+}
38
+
39
+func GetConfig() *Configuration{
40
+	return &config
41
+}

+ 55
- 0
controllers/person.go View File

@@ -0,0 +1,55 @@
1
+package controllers
2
+
3
+import (
4
+	"github.com/gin-gonic/gin"
5
+	"fmt"
6
+	"../db"
7
+)
8
+
9
+func DeletePerson(c *gin.Context) {
10
+	id := c.Params.ByName("id")
11
+	var person db.Person
12
+	d := db.GetDB().Where("id = ?", id).Delete(&person)
13
+	fmt.Println(d)
14
+	c.JSON(200, gin.H{"id #" + id: "deleted"})
15
+}
16
+
17
+func UpdatePerson(c *gin.Context) {
18
+	var person db.Person
19
+	id := c.Params.ByName("id")
20
+	if err := db.GetDB().Where("id = ?", id).First(&person).Error; err != nil {
21
+		c.AbortWithStatus(404)
22
+		fmt.Println(err)
23
+	}
24
+	c.BindJSON(&person)
25
+	db.GetDB().Save(&person)
26
+	c.JSON(200, person)
27
+}
28
+
29
+func CreatePerson(c *gin.Context) {
30
+	var person db.Person
31
+	c.BindJSON(&person)
32
+	db.GetDB().Create(&person)
33
+	c.JSON(200, person)
34
+}
35
+
36
+func GetPeople(c *gin.Context) {
37
+	var people []db.Person
38
+	if err := db.GetDB().Find(&people).Error; err != nil {
39
+		c.AbortWithStatus(404)
40
+		fmt.Println(err)
41
+	} else {
42
+		c.JSON(200, people)
43
+	}
44
+}
45
+
46
+func GetPerson(c *gin.Context) {
47
+	id := c.Params.ByName("id")
48
+	var person db.Person
49
+	if err := db.GetDB().Where("id = ?", id).First(&person).Error; err != nil {
50
+		c.AbortWithStatus(404)
51
+		fmt.Println(err)
52
+	} else {
53
+		c.JSON(200, person)
54
+	}
55
+}

+ 19
- 0
controllers/routes.go View File

@@ -0,0 +1,19 @@
1
+package controllers
2
+
3
+import (
4
+	gin "github.com/gin-gonic/gin"
5
+)
6
+
7
+func SetRoutes(r *gin.Engine) {
8
+	r.GET("/", func(c *gin.Context) {
9
+		c.JSON(200, gin.H{
10
+			"message": "Hello World",
11
+		})
12
+	})
13
+
14
+	r.GET("/people/", GetPeople)
15
+	r.GET("/people/:id", GetPerson)
16
+	r.POST("/people", CreatePerson)
17
+	r.PUT("/people/:id", UpdatePerson)
18
+	r.DELETE("/people/:id", DeletePerson)
19
+}

+ 28
- 0
db/db.go View File

@@ -0,0 +1,28 @@
1
+package db
2
+
3
+import (
4
+	"github.com/jinzhu/gorm"
5
+	_ "github.com/jinzhu/gorm/dialects/mysql"
6
+	"log"
7
+	"../config"
8
+)
9
+
10
+var db *gorm.DB
11
+
12
+func Connect() {
13
+
14
+	var err error
15
+	db, err = gorm.Open("mysql", config.GetConfig().Database.ConnectionString)
16
+	if err != nil {
17
+		log.Fatal("Error when connecting to DB. ", err)
18
+	} else {
19
+		log.Println("Properly connected to DB")
20
+	}
21
+	defer db.Close()
22
+
23
+	db.AutoMigrate(&Person{})
24
+}
25
+
26
+func GetDB() *gorm.DB {
27
+	return db
28
+}

+ 7
- 0
db/models.go View File

@@ -0,0 +1,7 @@
1
+package db
2
+
3
+type Person struct {
4
+	ID uint `json:"id"`
5
+	FirstName string `json:"firstname"`
6
+	LastName string `json:"lastname"`
7
+}

+ 7
- 0
defconfig.json View File

@@ -0,0 +1,7 @@
1
+{
2
+  "name": "Development",
3
+  "release": false,
4
+  "datarbase": {
5
+    "connection_string": "root:pass@/test?charset=utf8&parseTime=True&loc=Local"
6
+  }
7
+}

+ 24
- 0
main.go View File

@@ -0,0 +1,24 @@
1
+package main
2
+
3
+import (
4
+	"./config"
5
+	"./db"
6
+	"./controllers"
7
+	"log"
8
+	"github.com/gin-gonic/gin"
9
+)
10
+
11
+func main() {
12
+	config := config.GetConfig()
13
+	if config.Release {
14
+		gin.SetMode(gin.ReleaseMode)
15
+	}
16
+
17
+	db.Connect()
18
+
19
+	r := gin.Default()
20
+	controllers.SetRoutes(r)
21
+	log.Println("Properly initialized")
22
+
23
+	r.Run(":8080")
24
+}

Loading…
Cancel
Save