mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-05-30 04:20:53 +00:00
Conver to regular Go vendor + dep tool
This commit is contained in:
+7
@@ -0,0 +1,7 @@
|
||||
# Guide to run Gin under App Engine LOCAL Development Server
|
||||
|
||||
1. Download, install and setup Go in your computer. (That includes setting your `$GOPATH`.)
|
||||
2. Download SDK for your platform from here: `https://developers.google.com/appengine/downloads?hl=es#Google_App_Engine_SDK_for_Go`
|
||||
3. Download Gin source code using: `$ go get github.com/gin-gonic/gin`
|
||||
4. Navigate to examples folder: `$ cd $GOPATH/src/github.com/gin-gonic/gin/examples/`
|
||||
5. Run it: `$ goapp serve app-engine/`
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
application: hello
|
||||
version: 1
|
||||
runtime: go
|
||||
api_version: go1
|
||||
|
||||
handlers:
|
||||
- url: /.*
|
||||
script: _go_app
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package hello
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// This function's name is a must. App Engine uses it to drive the requests properly.
|
||||
func init() {
|
||||
// Starts a new Gin instance with no middle-ware
|
||||
r := gin.New()
|
||||
|
||||
// Define your handlers
|
||||
r.GET("/", func(c *gin.Context){
|
||||
c.String(200, "Hello World!")
|
||||
})
|
||||
r.GET("/ping", func(c *gin.Context){
|
||||
c.String(200, "pong")
|
||||
})
|
||||
|
||||
// Handle all requests using net/http
|
||||
http.Handle("/", r)
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var DB = make(map[string]string)
|
||||
|
||||
func main() {
|
||||
r := gin.Default()
|
||||
|
||||
// Ping test
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
c.String(200, "pong")
|
||||
})
|
||||
|
||||
// Get user value
|
||||
r.GET("/user/:name", func(c *gin.Context) {
|
||||
user := c.Params.ByName("name")
|
||||
value, ok := DB[user]
|
||||
if ok {
|
||||
c.JSON(200, gin.H{"user": user, "value": value})
|
||||
} else {
|
||||
c.JSON(200, gin.H{"user": user, "status": "no value"})
|
||||
}
|
||||
})
|
||||
|
||||
// Authorized group (uses gin.BasicAuth() middleware)
|
||||
// Same than:
|
||||
// authorized := r.Group("/")
|
||||
// authorized.Use(gin.BasicAuth(gin.Credentials{
|
||||
// "foo": "bar",
|
||||
// "manu": "123",
|
||||
//}))
|
||||
authorized := r.Group("/", gin.BasicAuth(gin.Accounts{
|
||||
"foo": "bar", // user:foo password:bar
|
||||
"manu": "123", // user:manu password:123
|
||||
}))
|
||||
|
||||
authorized.POST("admin", func(c *gin.Context) {
|
||||
user := c.MustGet(gin.AuthUserKey).(string)
|
||||
|
||||
// Parse JSON
|
||||
var json struct {
|
||||
Value string `json:"value" binding:"required"`
|
||||
}
|
||||
|
||||
if c.Bind(&json) {
|
||||
DB[user] = json.Value
|
||||
c.JSON(200, gin.H{"status": "ok"})
|
||||
}
|
||||
})
|
||||
|
||||
// Listen and Server in 0.0.0.0:8080
|
||||
r.Run(":8080")
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/flosch/pongo2"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type pongoRender struct {
|
||||
cache map[string]*pongo2.Template
|
||||
}
|
||||
|
||||
func newPongoRender() *pongoRender {
|
||||
return &pongoRender{map[string]*pongo2.Template{}}
|
||||
}
|
||||
|
||||
func writeHeader(w http.ResponseWriter, code int, contentType string) {
|
||||
if code >= 0 {
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
w.WriteHeader(code)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *pongoRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
||||
file := data[0].(string)
|
||||
ctx := data[1].(pongo2.Context)
|
||||
var t *pongo2.Template
|
||||
|
||||
if tmpl, ok := p.cache[file]; ok {
|
||||
t = tmpl
|
||||
} else {
|
||||
tmpl, err := pongo2.FromFile(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.cache[file] = tmpl
|
||||
t = tmpl
|
||||
}
|
||||
writeHeader(w, code, "text/html")
|
||||
return t.ExecuteWriter(ctx, w)
|
||||
}
|
||||
|
||||
func main() {
|
||||
r := gin.Default()
|
||||
r.HTMLRender = newPongoRender()
|
||||
|
||||
r.GET("/index", func(c *gin.Context) {
|
||||
name := c.Request.FormValue("name")
|
||||
ctx := pongo2.Context{
|
||||
"title": "Gin meets pongo2 !",
|
||||
"name": name,
|
||||
}
|
||||
c.HTML(200, "index.html", ctx)
|
||||
})
|
||||
|
||||
// Listen and server on 0.0.0.0:8080
|
||||
r.Run(":8080")
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ja">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>{{ title }}</title>
|
||||
<meta name="keywords" content="">
|
||||
<meta name="description" content="">
|
||||
</head>
|
||||
<body>
|
||||
Hello {{ name }} !
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user