ambition

package module
v0.0.0-...-284915a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 11, 2016 License: Unlicense Imports: 17 Imported by: 0

README

ambition

Action tracking

Setup

Go and docker or postgres are required

1. Download

$ go get github.com/myambition/ambition

3. Setup Config

Edit config.json for database settings and server port.

Then move to $HOME/.config/ambition/config.json

2. Get static files with bower

$ cd static

$ bower install

3. Compile

$ go install ./ambition

4. Setup Postgresql using docker

$ ambition dockerDB

This creates a docker container running the postgresql image. It is port forwarded to localhost:5432 with username:ambition password:ambition dbname:ambition and ssl:disabled.

To stop the docker container, refer to the docker documentation. This is provided as a convenient command to launch a clean database. Note that the data in this database will only last as long as the container does. Refer to the postgres docker image on dockerhub for additional details.

5. Seeding the database

$ ambition create

$ ambition seed

(Optional install of docker) Setup Postgresql
$ sudo apt-get install postgresql

# set password to be ambition for it to be easy
$ sudo adduser ambition

$ sudo -i -u postgres

$ createdb ambition

# let them be superuser
$ createuser ambition

$ psql

$ # ALTER USER ambition PASSWORD ‘ambition';

$ exit

Basic structure

main.go
  • Checks for command line arguments for setup.
  • If no command line arguments, start web server on port 3000 loading routes from routes.go
routes.go
  • Sets routes with http methods, paths, and parameters to functions in hander.go
  • TODO: Add better description of all routes. Currently docs/structure.md has all information, it needs to be more formalized.
handler.go

Possibly change handler.go to formatter.go and use it to parse the request and send it the right way. Then change jsonHandler.go to hander.go and only interact with db.go there

Input

  • Parameters in url
  • Optional: Query parameters
  • Optional: Post body parameters Result
  • Calls jsonHandler.go functions to handle both input and output json
  • database selection and returning of json
  • database insertion

I just realized that handler.go, db.go, and jsonHandler.go are much more entwined than I thought. handler.go with jsonHandler.go; both calling db.go methods.

jsonHandler.go

The idea was just to handle json here, but it also works with the database. May change to handler.go see handler.go

db.go

Wraps postgres with go functions so that all SQL interaction happens here. SQL strings are only allowed in db.go

types.go

All types used in ambition. May want to add custom struct tags for sql here. Like sql value name and sql value type, for generic database methods in db.go.

commands.go

Functions for command line arguments go here

util.go

Custom utils snippits go here.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ActionById

func ActionById(w http.ResponseWriter, r *http.Request, ps httprouter.Params, user *User)

func Actions

func Actions(w http.ResponseWriter, r *http.Request, _ httprouter.Params, user *User)

func AddRoutes

func AddRoutes(router *httprouter.Router)

Add routes to http router TODO: Add route description parameters and useage

func AuthLogin

func AuthLogin(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func CallCommand

func CallCommand(command string) error

func CheckAuth

func CheckAuth(handle UserHandler) httprouter.Handle

CheckAuth

func CompareHashAndToken

func CompareHashAndToken(hash string, token string) bool

func CompareSaltAndPasswordToHash

func CompareSaltAndPasswordToHash(salt, hashedPassword, password []byte) bool

CompareSaltAndPasswordToHash takes a salt, a hashedPassword, and a submitted password and returns true if the submitted password was the original password saved

func CreateSaltAndHashedPassword

func CreateSaltAndHashedPassword(password []byte) ([]byte, []byte, error)

CreateSaltAndHashedPassword takes a password as a byte array and returns a hashed version of the password and a password salt that was generated and used in the hashing process

func GenerateRandomBytes

func GenerateRandomBytes(n int) ([]byte, error)

GenerateRandomBytes returns securely generated random bytes. It will return an error if the system's secure random number generator fails to function correctly, in which case the caller should not continue.

func GenerateRandomString

func GenerateRandomString(s int) (string, error)

GenerateRandomString returns a URL-safe, base64 encoded securely generated random string. It will return an error if the system's secure random number generator fails to function correctly, in which case the caller should not continue.

func HashToken

func HashToken(token string) string

func Index

func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params, user *User)

func LoginPage

func LoginPage(w http.ResponseWriter, r *http.Request, _ httprouter.Params)

func LoginUser

func LoginUser(username string, password string)

func LoginUserJson

func LoginUserJson(userJson []byte) (string, int, error)

LoginuserJson takes json with a username and a password key unmarshals this json and then creates a session if the pass authentication information is valid

func OccurrenceById

func OccurrenceById(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func Occurrences

func Occurrences(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

TODO: Add time as a query string parameter. Allow the user to specify how many they want also make a week the default amount

func PostAction

func PostAction(w http.ResponseWriter, r *http.Request, ps httprouter.Params, user *User)

func PostArrayOfActionsJson

func PostArrayOfActionsJson(actionJson []byte) error

func PostArrayOfOccurrencesJson

func PostArrayOfOccurrencesJson(occurrenceJson []byte) error

func PostArrayOfSetsJson

func PostArrayOfSetsJson(setJson []byte) error

func PostOccurrence

func PostOccurrence(w http.ResponseWriter, r *http.Request, ps httprouter.Params, user *User)

func PostOccurrenceByActionIdJson

func PostOccurrenceByActionIdJson(ActionId int, occurrenceJson []byte) error

func PostUser

func PostUser(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func PostUserJson

func PostUserJson(userJson []byte) error

Creates a User in the database from json.

func Run

func Run()

Types

type Action

type Action struct {
	Id         int    `json:"id"`
	ActionName string `json:"actionName"`
	SetId      int    `json:"setId"`
	UserId     int    `json:"userId"`
}

TODO: Add metadata / extradata / data field. It can have any structure.

Action specifies the structure.

func (Action) CreateOccurrence

func (a Action) CreateOccurrence(occurrence Occurrence) error

type Configuration

type Configuration struct {
	DBName       string
	DBUser       string
	DBPassword   string
	DBLocal      bool
	DBHost       string
	DBPort       int
	DBSSL        string
	AmbitionPort int
}

func ReadConfiguration

func ReadConfiguration(file string) Configuration

type DB

type DB struct {
	*sql.DB
}

Database type to extend with custom functions

func (DB) CreateActionTable

func (db DB) CreateActionTable() error

func (DB) CreateOccurrenceTable

func (db DB) CreateOccurrenceTable() error

func (DB) CreateSessionTable

func (db DB) CreateSessionTable() error

func (DB) CreateSetTable

func (db DB) CreateSetTable() error

func (DB) CreateUserTable

func (db DB) CreateUserTable() error

func (DB) DeleteActionById

func (db DB) DeleteActionById(actionId int) error

func (DB) DeleteOccurrenceById

func (db DB) DeleteOccurrenceById(occurrenceId int) error

func (DB) DeleteSessionByUserId

func (db DB) DeleteSessionByUserId(userId int) error

func (DB) DeleteSetById

func (db DB) DeleteSetById(setId int) error

func (DB) DropActionTable

func (db DB) DropActionTable() error

func (DB) DropOccurrenceTable

func (db DB) DropOccurrenceTable() error

func (DB) DropSessionTable

func (db DB) DropSessionTable() error

func (DB) DropSetTable

func (db DB) DropSetTable() error

func (DB) DropUserTable

func (db DB) DropUserTable() error

func (DB) GetActionById

func (db DB) GetActionById(id int) (*Action, error)

func (DB) GetActions

func (db DB) GetActions() ([]Action, error)

func (DB) GetActionsByUserId

func (db DB) GetActionsByUserId(id int) ([]Action, error)

func (DB) GetOccurrenceById

func (db DB) GetOccurrenceById(id int) (*Occurrence, error)

func (DB) GetOccurrencesOfAction

func (db DB) GetOccurrencesOfAction(id int) ([]Occurrence, error)

func (DB) GetSessionKeysByUserId

func (db DB) GetSessionKeysByUserId(userId int) ([]string, error)

func (DB) GetSetById

func (db DB) GetSetById(id int) (*Set, error)

func (DB) GetSets

func (db DB) GetSets() ([]Set, error)

----------------------------- Sets ----------------------------- //

func (DB) GetUserById

func (db DB) GetUserById(id int) (*User, error)

func (DB) GetUserByUserName

func (db DB) GetUserByUserName(userName string) (*User, error)

func (DB) InsertAction

func (db DB) InsertAction(action *Action) error

func (DB) InsertOccurrence

func (db DB) InsertOccurrence(occurrence *Occurrence) error

func (DB) InsertSession

func (db DB) InsertSession(userId int, hashedToken string) error

func (DB) InsertSet

func (db DB) InsertSet(set *Set) error

func (DB) InsertUser

func (db DB) InsertUser(user *User) error

type Occurrence

type Occurrence struct {
	Id       int       `json:"id"`
	ActionId int       `json:"actionId"`
	Time     time.Time `json:"time"`
}

TODO: Add metadata / extradata / data field. It can have any structure.

Occurrence should follow the sturcture specified in Action.

type Set

type Set struct {
	Id      int    `json:"id"`
	SetName string `json:"setName"`
}

type User

type User struct {
	Id             int    `json:"id"`
	UserName       string `json:"username"`
	Email          string `json:"email"`
	HashedPassword []byte
	PasswordSalt   []byte
}

func Login

func Login(username, password string) (u *User, token string, err error)

func (User) CreateAction

func (u User) CreateAction(action Action) error

func (User) GetAction

func (u User) GetAction(actionId int) (*Action, error)

func (User) GetActions

func (u User) GetActions() ([]Action, error)

type UserHandler

type UserHandler func(http.ResponseWriter, *http.Request, httprouter.Params, *User)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL