示例#1
0
func (self *RootController) Index(c *gin.Context) {
	username := self.CurrentUser(c)

	if username == "" {
		c.HTML(http.StatusOK, "index.tmpl", gin.H{
			"alert":      false,
			"error":      false,
			"logged_in":  false,
			"message":    "",
			"baseDomain": self.config.BaseDomain,
		})

		return
	}

	if !user.Exists(self.etcd, username) {
		c.HTML(http.StatusNotFound, "apps.tmpl", gin.H{
			"error":   true,
			"message": fmt.Sprintf("User %s does not exist.", username),
		})

		return
	}

	c.HTML(http.StatusOK, "index.tmpl", gin.H{
		"alert":      false,
		"avater_url": user.GetAvaterURL(self.etcd, username),
		"error":      false,
		"logged_in":  true,
		"message":    "",
		"username":   username,
		"baseDomain": self.config.BaseDomain,
	})
}
示例#2
0
func (self *AppController) Index(c *gin.Context) {
	username := self.CurrentUser(c)

	if username == "" {
		c.Redirect(http.StatusFound, "/")

		return
	}

	if !user.Exists(self.etcd, username) {
		c.HTML(http.StatusNotFound, "apps.tmpl", gin.H{
			"error":   true,
			"message": fmt.Sprintf("User %s does not exist.", username),
		})

		return
	}

	apps, err := app.List(self.etcd, username)

	if err != nil {
		fmt.Fprintf(os.Stderr, "%+v\n", err)

		c.HTML(http.StatusInternalServerError, "apps.tmpl", gin.H{
			"error":   true,
			"message": "Failed to list apps.",
		})

		return
	}

	c.HTML(http.StatusOK, "apps.tmpl", gin.H{
		"error":      false,
		"apps":       apps,
		"avater_url": user.GetAvaterURL(self.etcd, username),
		"logged_in":  true,
		"username":   username,
	})
}
示例#3
0
func (self *AppController) Get(c *gin.Context) {
	var latestURL string

	username := self.CurrentUser(c)

	if username == "" {
		c.Redirect(http.StatusFound, "/")

		return
	}

	if !user.Exists(self.etcd, username) {
		c.HTML(http.StatusNotFound, "apps.tmpl", gin.H{
			"error":   true,
			"message": fmt.Sprintf("User %s does not exist.", username),
		})

		return
	}

	appName := c.Param("appName")

	if !app.Exists(self.etcd, username, appName) {
		c.HTML(http.StatusNotFound, "apps.tmpl", gin.H{
			"error":   true,
			"message": fmt.Sprintf("Application %s does not exist.", appName),
		})

		return
	}

	urls, err := app.URLs(self.etcd, self.config.URIScheme, self.config.BaseDomain, username, appName)

	if err != nil {
		fmt.Fprintf(os.Stderr, "%+v\n", err)

		c.HTML(http.StatusInternalServerError, "app.tmpl", gin.H{
			"error":   true,
			"message": "Failed to list app URLs.",
		})

		return
	}

	envs, err := env.List(self.etcd, username, appName)

	if err != nil {
		fmt.Fprintf(os.Stderr, "%+v\n", err)

		c.HTML(http.StatusInternalServerError, "app.tmpl", gin.H{
			"error":   true,
			"message": "Failed to list environment variables.",
		})

		return
	}

	buildArgs, err := arg.List(self.etcd, username, appName)

	if err != nil {
		fmt.Fprintf(os.Stderr, "%+v\n", err)

		c.HTML(http.StatusInternalServerError, "app.tmpl", gin.H{
			"error":   true,
			"message": "Failed to list build args.",
		})

		return
	}

	hc, err := healthcheck.Get(self.etcd, username, appName)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%+v\n", err)

		c.HTML(http.StatusInternalServerError, "app.tmpl", gin.H{
			"error":   true,
			"message": "Failed to get healthcheck.",
		})

		return
	}

	if len(urls) > 0 {
		latestURL = app.LatestAppURLOfUser(self.config.URIScheme, self.config.BaseDomain, username, appName)
	}

	c.HTML(http.StatusOK, "app.tmpl", gin.H{
		"error":       false,
		"app":         appName,
		"avater_url":  user.GetAvaterURL(self.etcd, username),
		"buildArgs":   buildArgs,
		"envs":        envs,
		"healthcheck": hc,
		"latestURL":   latestURL,
		"logged_in":   true,
		"urls":        urls,
		"username":    username,
	})
}
示例#4
0
func (self *SessionController) Callback(c *gin.Context) {
	session := sessions.Default(c)

	if session.Get("state") == nil {
		c.String(http.StatusBadRequest, "State string is not stored in session.")
		return
	}

	storedState := session.Get("state").(string)
	state := c.Query("state")

	if state != storedState {
		c.String(http.StatusUnauthorized, "State string does not match.")
		return
	}

	session.Delete("state")

	code := c.Query("code")
	token, err := self.oauthConf.Exchange(oauth2.NoContext, code)

	if err != nil {
		fmt.Fprintf(os.Stderr, "%+v\n", err)

		c.String(http.StatusBadRequest, "Failed to generate OAuth access token.")
		return
	}

	if !token.Valid() {
		c.String(http.StatusBadRequest, "OAuth access token is invalid.")
		return
	}

	oauthClient := self.oauthConf.Client(oauth2.NoContext, &oauth2.Token{AccessToken: token.AccessToken})
	client := github.NewClient(oauthClient)

	u, _, err := client.Users.Get("")

	if err != nil {
		c.String(http.StatusBadRequest, "Failed to retrive GitHub user profile.")
	}

	keys, _, err := client.Users.ListKeys("", &github.ListOptions{})

	if err != nil {
		fmt.Fprintf(os.Stderr, "%+v\n", err)

		c.String(http.StatusBadRequest, "Failed to retrive SSH public keys from GitHub.")
		return
	}

	for _, key := range keys {
		if !self.config.SkipKeyUpload {
			_, err := user.UploadPublicKey(*u.Login, *key.Key)

			if err != nil {
				fmt.Fprintf(os.Stderr, "%+v\n", err)

				c.String(http.StatusBadRequest, "Failed to register SSH public key.")
				return
			}
		} else {
			fmt.Printf("User: %s, Key: %s\n", *u.Login, *key.Key)
		}
	}

	if !user.Exists(self.etcd, *u.Login) {
		if err := user.Create(self.etcd, u); err != nil {
			fmt.Fprintf(os.Stderr, "%+v\n", err)

			c.String(http.StatusBadRequest, "Failed to create user.")
			return
		}
	}

	if err := user.RegisterAccessToken(self.etcd, *u.Login, token.AccessToken); err != nil {
		fmt.Fprintf(os.Stderr, "%+v\n", err)

		c.String(http.StatusBadRequest, "Failed to register access token.")
		return
	}

	session.Set("token", token.AccessToken)
	session.Save()

	c.Redirect(http.StatusSeeOther, "/")
}