现在......我想复制"提交"来自Gitlab的我自己的书面课程(我是学生)。
我写了这个:
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"html/template"
"log"
"net/http"
"os"
)
type Project struct {
Idint64`json:"project_id"`
Titel string `json:"title"`
Name string `json:"name"`
Data Data `json:"data"`
Commits Commits`json:"commits"`
}
type Data struct {
Id int64 `json:"user_id"`
Username string `json:"user_name"`
}
type Commits struct {
Id string `json:"id"`
Message string `json:"message"`
}
func SetupRouter() http.Handler {
r := mux.NewRouter()
r.HandleFunc("/", Home).Methods("GET")
r.HandleFunc("/projects", Projects).Methods("GET")
return r
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
r := SetupRouter()
http.ListenAndServe(":" + port, r)
log.Fatalln(http.ListenAndServe(":"+port, r))
}
func Home(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Content-Type", "application/json")
fmt.Fprint(rw, "{\"hello:\"\"world\"}")
}
func Projects(rw http.ResponseWriter, req *http.Request) {
resp, err := http.Get("https://gitlab.nolteundlauth.de/api/v3/projects/44/events?private_token=*XXXXXXXXXXXXXXX*")
if err != nil {
fmt.Println("Error: " + err.Error())
return
}
defer resp.Body.Close()
var projects []Project
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&projects)
if err != nil {
fmt.Println("Error parsing json")
}
tmpl, _ := template.ParseFiles("projects.tpl")
tmpl.Execute(rw, projects)
}
我的模板文件:
<html>
<body>
<h1>Commits</h1>
{{range .}}
{{.Id}} {{.Name}} ({{.Data.Id}}) ({{.Data.Username}}) ({{.Commits.Id}}) ({{.Commits.Message}}) <br>
{{end}}
</body>
</html>
Gitlab提交的API是:
"commits": [{
"id": "c5feabde2d8cd023215af4d2ceeb7a64839fc428",
"message": "Add simple search to projects in public area",
"timestamp": "2013-05-13T18:18:08+00:00",
"url": "https://dev.gitlab.org/gitlab/gitlabhq/commit/c5feabde2d8cd023215af4d2ceeb7a64839fc428",
"author": {
"name": "Dmitriy Zaporozhets",
"email": "dmitriy.zaporozhets@gmail.com"
}
}],
当我启动服务器时,除了提交之外,我从structvariables获取每个值。为什么?