Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
chenmin committed Jun 10, 2020
1 parent 56312cd commit d3f70d4
Show file tree
Hide file tree
Showing 9 changed files with 445 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .vscode/sftp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "My Server",
"host": "172.16.90.230",
"protocol": "sftp",
"port": 22,
"username": "root",
"password": "@cm10086",
"remotePath": "/data/go/src/helm-api",
"syncMode": "update",
"ignore": [
"**/.vscode/**",
"**/.git/**"
],
"uploadOnSave": true
}
7 changes: 7 additions & 0 deletions conf/app.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[database]
Type = mysql
User = root
Password = test123
Host = 127.0.0.1:3306
Name = nvwa
TablePrefix = kube_
26 changes: 26 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import "github.com/gin-gonic/gin"

import "helm-api/views/charts"
import "helm-api/setting"
import "helm-api/models"

func init() {
setting.Setup()
models.Setup()
}

func main() {
r := gin.Default()
r.POST("/charts/:cluster/:namespace/:name", charts.Update)
r.GET("/charts/:cluster/:namespace/", charts.List)
r.DELETE("/charts/:cluster/:namespace/:name", charts.Delete)
r.GET("/charts/:cluster/:namespace/:name", charts.Retrieve)
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run(":10000")
}
23 changes: 23 additions & 0 deletions models/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package models

import (
// "fmt"

"github.com/jinzhu/gorm"
)

type Cluster struct {
ID int `json:"id"`
Name string `json:"name"`
Config string `json:"config"`
}

// GetArticle Get a single article based on ID
func GetCluster(id int) (*Cluster, error) {
var cluster Cluster
err := db.Where("id = ?", id).First(&cluster).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
return &cluster, nil
}
39 changes: 39 additions & 0 deletions models/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package models

import (
"fmt"

"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"

"helm-api/setting"
)

var db *gorm.DB

func Setup () {
var err error
db, err = gorm.Open(setting.DatabaseSetting.Type, fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local",
setting.DatabaseSetting.User,
setting.DatabaseSetting.Password,
setting.DatabaseSetting.Host,
setting.DatabaseSetting.Name))

if err != nil {
fmt.Println(err)
return
}else {
fmt.Println("connection succedssed")
}
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
return setting.DatabaseSetting.TablePrefix + defaultTableName
}

db.SingularTable(true)
db.DB().SetMaxIdleConns(10)
db.DB().SetMaxOpenConns(100)
}

func CloseDB() {
defer db.Close()
}
37 changes: 37 additions & 0 deletions setting/setting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package setting

import (
"log"

"github.com/go-ini/ini"
)

type Database struct {
Type string
User string
Password string
Host string
Name string
TablePrefix string
}

var DatabaseSetting = &Database{}

var cfg *ini.File

func Setup() {
var err error
cfg, err = ini.Load("conf/app.ini")
if err != nil {
log.Fatalf("setting.Setup, fail to parse 'conf/app.ini': %v", err)
}
mapTo("database", DatabaseSetting)
}

// mapTo map section
func mapTo(section string, v interface{}) {
err := cfg.Section(section).MapTo(v)
if err != nil {
log.Fatalf("Cfg.MapTo %s err: %v", section, err)
}
}
13 changes: 13 additions & 0 deletions util/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package util

import (
"fmt"
"io/ioutil"
)

func WriteFile(name, content string) {
data := []byte(content)
if ioutil.WriteFile(name,data,0644) == nil {
fmt.Println("写入文件成功: ", name)
}
}
30 changes: 30 additions & 0 deletions util/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package util

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/golang/glog"
)

type respBody struct {
Code int `json:"code"` // 0 or 1, 0 is ok, 1 is error
Data interface{} `json:"data,omitempty"`
Error string `json:"error,omitempty"`
}

func RespErr(c *gin.Context, err error) {
glog.Warningln(err)

c.JSON(http.StatusOK, &respBody{
Code: 1,
Error: err.Error(),
})
}

func RespOK(c *gin.Context, data interface{}) {
c.JSON(http.StatusOK, &respBody{
Code: 0,
Data: data,
})
}
Loading

0 comments on commit d3f70d4

Please sign in to comment.