-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
chenmin
committed
Jun 10, 2020
1 parent
56312cd
commit d3f70d4
Showing
9 changed files
with
445 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |
Oops, something went wrong.