Changing int to int64

Starting to implement angularjs
Implemented /api/campaigns/:id GET
Changed template delims to {{% and %}}
pull/24/head
Jordan 2014-01-31 20:49:22 -06:00
parent c4c57639e2
commit 87fbd41184
12 changed files with 84 additions and 61 deletions

View File

@ -51,9 +51,9 @@ func Login(r *http.Request) (bool, error) {
// GetUserById returns the user that the given id corresponds to. If no user is found, an
// error is thrown.
func GetUserById(id int) (models.User, error) {
func GetUserById(id int64) (models.User, error) {
u := models.User{}
err := db.Conn.SelectOne(&u, "SELECT id, username, apikey FROM Users WHERE id=?", id)
err := db.Conn.SelectOne(&u, "SELECT id, username, api_key FROM Users WHERE id=?", id)
if err != nil {
return u, err
}
@ -64,7 +64,7 @@ func GetUserById(id int) (models.User, error) {
// error is thrown.
func GetUserByAPIKey(key []byte) (models.User, error) {
u := models.User{}
err := db.Conn.SelectOne(&u, "SELECT id, username, apikey FROM Users WHERE apikey=?", key)
err := db.Conn.SelectOne(&u, "SELECT id, username, api_key FROM Users WHERE apikey=?", key)
if err != nil {
return u, err
}

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
ctx "github.com/gorilla/context"
@ -78,9 +79,27 @@ func API_Campaigns(w http.ResponseWriter, r *http.Request) {
//API_Campaigns_Id returns details about the requested campaign. If the campaign is not
//valid, API_Campaigns_Id returns null.
func API_Campaigns_Id(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
vars := mux.Vars(r)
fmt.Fprintf(w, "{\"method\" : \""+r.Method+"\", \"id\" : "+vars["id"]+"}")
id, err := strconv.ParseInt(vars["id"], 0, 64)
if checkError(err, w, "Invalid Int") {
return
}
switch {
case r.Method == "GET":
c := models.Campaign{}
err := db.Conn.SelectOne(&c, "SELECT campaigns.id, name, created_date, completed_date, status, template FROM campaigns, users WHERE campaigns.uid=users.id AND campaigns.id =? AND users.api_key=?", id, ctx.Get(r, "api_key"))
if checkError(err, w, "No campaign found") {
return
}
fmt.Printf("%v\n", c)
cj, err := json.MarshalIndent(c, "", " ")
if checkError(err, w, "Error creating JSON response") {
return
}
writeJSON(w, cj)
case r.Method == "DELETE":
//c := models.Campaign{}
}
}
//API_Doc renders a template describing the API documentation.

View File

@ -39,6 +39,8 @@ import (
"github.com/jordan-wright/gophish/models"
)
var templateDelims = []string{"{{%", "%}}"}
func CreateRouter() *mux.Router {
router := mux.NewRouter()
// Base Front-end routes
@ -145,7 +147,13 @@ func Login(w http.ResponseWriter, r *http.Request) {
}
func getTemplate(w http.ResponseWriter, tmpl string) *template.Template {
return template.Must(template.New("template").ParseFiles("templates/base.html", "templates/nav.html", "templates/"+tmpl+".html", "templates/flashes.html"))
templates := template.New("template")
templates.Delims(templateDelims[0], templateDelims[1])
_, err := templates.ParseFiles("templates/base.html", "templates/nav.html", "templates/"+tmpl+".html", "templates/flashes.html")
if err != nil {
fmt.Println(err)
}
return template.Must(templates, err)
}
func checkError(e error, w http.ResponseWriter, m string) bool {

View File

@ -18,7 +18,7 @@ func GetContext(handler http.Handler) http.HandlerFunc {
// Put the session in the context so that
ctx.Set(r, "session", session)
if id, ok := session.Values["id"]; ok {
u, err := auth.GetUserById(id.(int))
u, err := auth.GetUserById(id.(int64))
if err != nil {
ctx.Set(r, "user", nil)
}

View File

@ -0,0 +1,7 @@
var gophishApp = angular.module('gophishApp', []);
gophishApp.controller('CampaignCtrl', function($scope, $http) {
$http.get('/api/campaigns?api_key=' + API_KEY).success(function(data) {
$scope.campaigns = data;
})
})

View File

@ -1,6 +1,6 @@
{{define "base"}}
{{% define "base" %}}
<!DOCTYPE html>
<html lang="en">
<html lang="en" ng-app="gophishApp">
<head>
<meta charset="utf-8">
@ -10,7 +10,7 @@
<meta name="author" content="">
<link rel="shortcut icon" href="../../docs-assets/ico/favicon.png">
<title>Gophish - {{.Title}}</title>
<title>Gophish - {{% .Title %}}</title>
<!-- Bootstrap core CSS -->
<link href="/css/bootstrap.css" rel="stylesheet">
@ -19,10 +19,13 @@
<link href="/css/main.css" rel="stylesheet">
<link href="/css/font-awesome.min.css" rel="stylesheet">
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700' rel='stylesheet' type='text/css'>
{{%if .User%}}
<script>var API_KEY = {{%.User.APIKey%}}</script>
{{%end%}}
</head>
<body>
{{template "content" .}}
{{% template "content" . %}}
<!-- Footer -->
<div class="container">
<hr>
@ -33,7 +36,9 @@
<!-- Placed at the end of the document so the pages load faster -->
<script src="/js/jquery.js"></script>
<script src="/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
<script src="/js/app/controllers.js"></script>
</body>
</html>
{{end}}
{{% end %}}

View File

@ -1,4 +1,4 @@
{{define "content"}} {{template "nav" .User}}
{{% define "content" %}} {{% template "nav" .User %}}
<div class="jumbotron">
<div class="container" style="text-align:center;">
<h1 class="sans header">
@ -19,7 +19,7 @@
</li>
</ul>
</div>
<div class="col-md-9">
<div class="col-md-9" ng-controller="CampaignCtrl">
<div class="row">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#newCampaignModal"><i class="fa fa-plus"></i> New Campaign</button>
</div>
@ -34,25 +34,10 @@
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Mark</td>
<td>Otto</td>
</tr>
<tr>
<td>3</td>
<td>Mark</td>
<td>Otto</td>
</tr>
<tr>
<td>2</td>
<td>Jacob</td>
<td>Thornton</td>
</tr>
<tr>
<td>3</td>
<td>Larry the Bird</td>
<td>@twitter</td>
<tr ng-repeat="campaign in campaigns">
<td>{{campaign.id}}</td>
<td>{{campaign.name}}</td>
<td>{{campaign.status}}</td>
</tr>
</tbody>
</table>
@ -84,4 +69,4 @@
</div>
</div>
</div>
{{end}}
{{% end %}}

View File

@ -1,15 +1,15 @@
{{define "flashes"}}
{{range .}}
<div style="text-align:center" class="alert alert-{{.Type}}">
{{%define "flashes"%}}
{{%range .%}}
<div style="text-align:center" class="alert alert-{{%.Type%}}">
<i class="fa
{{if eq .Type "danger"}}
{{%if eq .Type "danger"%}}
fa-exclamation-circle
{{else if eq .Type "warning"}}
{{%else if eq .Type "warning"%}}
fa-exclamation-triangle
{{else if eq .Type "success"}}
{{%else if eq .Type "success"%}}
fa-check-circle
{{end}}"></i>
{{.Message}}
{{%end%}}"></i>
{{%.Message%}}
</div>
{{end}}
{{end}}
{{%end%}}
{{%end%}}

View File

@ -1,9 +1,9 @@
{{define "content"}}
{{%define "content"%}}
<div class="container">
<form class="form-signin" action="/login" method="POST">
<img id="logo" src="/images/logo.png" />
<h2 class="form-signin-heading">Please sign in</h2>
{{template "flashes" .Flashes}}
{{%template "flashes" .Flashes%}}
<input type="text" name="username" class="form-control" placeholder="Username" required autofocus>
<input type="password" name="password" class="form-control" placeholder="Password" required>
<label class="checkbox">
@ -12,4 +12,4 @@
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
{{end}}
{{%end%}}

View File

@ -1,4 +1,4 @@
{{define "nav"}}
{{%define "nav"%}}
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
@ -13,29 +13,28 @@
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li>
{{if .}}
{{%if .%}}
<div class="btn-group" id="navbar-dropdown">
<button type="button" class="btn btn-primary"><i class="fa fa-user"></i> {{.Username}}</button>
<button type="button" class="btn btn-primary"><i class="fa fa-user"></i> {{%.Username%}}</button>
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
<span class="caret" style="border-top-color:#FFFFFF"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" role="menu">
<li><a href="/settings">Settings</a>
<ul class="dropdown-menu" role="menu"> <li><a href="/settings">Settings</a>
</li>
<li class="divider"></li>
<li><a href="/logout">Logout</a>
</li>
</ul>
</div>
{{else}}
{{%else%}}
<a href="/login">
<button type="button" class="btn btn-primary">Login</button>
</a>
{{end}}
{{%end%}}
</li>
</ul>
</div>
</div>
</div>
{{end}}
{{%end%}}

View File

@ -1,4 +1,4 @@
{{define "content"}} {{template "nav" .User}}
{{%define "content"%}} {{%template "nav" .User%}}
<div class="jumbotron">
<div class="container" style="text-align:center;">
<h1 class="sans header">
@ -27,7 +27,7 @@
</p>
</div>
<div class="col-md-6">
<input type="text" value="{{.User.Username}}" class="form-control" />
<input type="text" value="{{%.User.Username%}}" class="form-control" />
</div>
</div>
<br/>
@ -37,11 +37,11 @@
</p>
</div>
<div class="col-md-6">
<input type="text" value="{{.User.APIKey}}" class="form-control" readonly/>
<input type="text" value="{{%.User.APIKey%}}" class="form-control" readonly/>
</div>
</div>
<br />
<button class="btn btn-primary">Save</button>
</div>
</div>
{{end}}
{{%end%}}

View File

@ -1,4 +1,4 @@
{{define "content"}} {{template "nav"}}
{{%define "content"%}} {{%template "nav"%}}
<div class="jumbotron">
<div class="container" style="text-align:center;">
<h1 class="sans header">
@ -26,5 +26,5 @@
<p>Test.</p>
</div>
</div>
{{end}}
{{%end%}}