Skip to content
This repository was archived by the owner on Feb 16, 2024. It is now read-only.

Commit 81a16f1

Browse files
committed
Initial Commit
1 parent 467fda6 commit 81a16f1

File tree

105 files changed

+25202
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+25202
-0
lines changed

README

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Pasar Sako Mandiri Backend System
2+
Created and Maintained By Albert Widiatmoko.
3+
4+
Featuring:
5+
- Ardiola
6+
- Freycool
7+
- Gabeaventh
8+
9+
Project Timelines:
10+
Alpha 1 = 7 Feb 2016
11+
Beta 1 = 1 Mar 2016
12+
13+
Alpha 1
14+
- Alpha 1 will be our baseline for this project, all features will be done in this release
15+
16+
Beta 1
17+
- Beta 1 will bring some consistency across the code, variable, profiling, etc.
18+
19+
Beta 2
20+
- Code cleanups and performance boost
21+
22+
Production 1
23+
- Stable release 1. Version: 1.0
24+
25+
Why using gin for this project?
26+
- Gin was simple yet fast, it's not the idiomatic Go, but still good enough for this project. I'll rewrite this project to pure "net/http" and "httprouter" later.
27+
28+
Current Features:
29+
- Router authentication via auth middleware
30+
- IP Ban to prevent brute force
31+
- JWT Token authentication
32+
- Session authentication
33+
- User registration (level included)
34+
- Parking device registration
35+
- Parking device group registration (camera, microcontroller, etc)
36+
- Parking vehicle registration
37+
- Parking price per-vehicle registration
38+
- Ticketing
39+
- IPCamera
40+
41+
Project Development Guidelines:
42+
43+
To Create a Web Page using template:
44+
1. Create template file in templates folder, using *.tmpl extension. Ex: index.tmpl
45+
2. Create controllers to load the page.
46+
Ex: func IndexPage(c *gin.Context) {
47+
session := session.Instance(c)
48+
c.HTML(http.StatusFound, "index.tmpl", gin.H{"title":"Index", "token":session.Get("token")})
49+
}
50+
3. Create router in router.go.
51+
Ex: r.GET("/index", middleware.DisAllowAnon(), controllers.IndexPage)
52+
*You can use middlewares to activate automatic authentication.
53+
54+
Don't use time.Now() to input time to Database, this could bring inconsistentcy between your Go system and your Database system. Use time.Now().string() instead.

TODO

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
Progress:
2+
- Authentication system and library in final state (production ready)
3+
- Framework in final state (production ready)
4+
- Code review is undergo
5+
- Code reviewed
6+
- Rewrite project (finished)
7+
- Prevent brute force (complete, using ipban)
8+
- Better token authentication (in progress, need a review and new method)
9+
- Prevent redundant ticket number (finished)
10+
- Performance boost (in review)
11+
- Password fully encrypted using bcrypt
12+
- Router restructure (in progress)
13+
14+
Jan 29 2016
15+
- Will soon re-baseline the code and focused in the UI
16+
- Framework considered as proper and good for production, improvement will always be there tough (in framework branch).
17+
18+
Jan 31 2016
19+
- Parking API will be finalized and reviewed
20+
- Framework security review
21+
- Code bootstrapping start
22+
23+
Feb 02 2016
24+
- Simplified gate device to group device
25+
- Parking API final review
26+
27+
Feb 04 2016
28+
- Time consistency update between Go and MySQL
29+
30+
Feb 05 2016
31+
- Code cleanups and router naming update
32+
- Memory leak test
33+
34+
Feb 06 2016
35+
- Finalize Raspberry API
36+
- Memory leak test
37+
- Print out test from raspberry
38+
- Parking transactions page
39+
40+
Feb 07 2016
41+
- Code documentation
42+
- Sincia Time!
43+
44+
Feb 12 2016
45+
- Camera Test
46+
- Convert DB MustExec to Exec
47+
48+
Feb 26 2016
49+
- Finalize Camera API
50+
- Gouroutines to delete picture
51+
52+
Mar 1 2016
53+
- Test all API for production
54+
- Add Log module for server log
55+
- Add channel to capture picture from camera
56+
57+
Mar 2 2016
58+
- Add better middleware chaining into router
59+
- Security audit
60+
61+
Mar 3 2016
62+
- Router code cleanups and improvement
63+
- Add a better way to handle picture by adding picture controller
64+
65+
Mar 7 2016
66+
- Feature freeze
67+
- Production ready phase 1

api/api.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package api
2+
import (
3+
"net/http"
4+
"net/url"
5+
"fmt"
6+
"bytes"
7+
"strconv"
8+
"io/ioutil"
9+
)
10+
11+
func CameraTakePicture(cameraIP string) {
12+
13+
}
14+
15+
func ContactClientCheckOut() {
16+
data := url.Values{}
17+
18+
client := &http.Client{}
19+
r, _ := http.NewRequest("GET", "http://192.168.0.177:8080", bytes.NewBufferString(data.Encode()))
20+
r.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
21+
22+
resp, err := client.Do(r)
23+
24+
if err != nil {
25+
fmt.Println(err)
26+
}
27+
28+
fmt.Println(resp.Status)
29+
fmt.Println(resp.Body)
30+
defer resp.Body.Close()
31+
32+
body, err := ioutil.ReadAll(resp.Body)
33+
fmt.Println(body)
34+
}
35+

api/ipcamera.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package api
2+
3+
import (
4+
"net/http"
5+
"io/ioutil"
6+
"log"
7+
)
8+
9+
type IpCamera struct {
10+
Protocol string
11+
Host string
12+
Param string
13+
Username string
14+
Password string
15+
Picture chan []byte
16+
}
17+
18+
func IpCamGetPicture(ipcam IpCamera) ([]byte, error) {
19+
var pictureByte []byte
20+
completeUrl := ipcam.Protocol+"://"+ipcam.Host+"/"+ipcam.Param
21+
22+
client := http.Client{}
23+
req, err := http.NewRequest("GET", completeUrl, nil)
24+
req.SetBasicAuth(ipcam.Username, ipcam.Password)
25+
if err != nil {
26+
return pictureByte, err
27+
}
28+
29+
res, err := client.Do(req)
30+
31+
if err != nil {
32+
return pictureByte, err
33+
}
34+
35+
defer res.Body.Close()
36+
37+
pictureByte, err = ioutil.ReadAll(res.Body)
38+
39+
if err != nil {
40+
return pictureByte, err
41+
}
42+
43+
return pictureByte, err
44+
}
45+
46+
func ipCamGetPicture(ipcam *IpCamera) ([]byte, error) {
47+
var pictureByte []byte
48+
completeUrl := ipcam.Protocol+"://"+ipcam.Host+"/"+ipcam.Param
49+
50+
client := http.Client{}
51+
req, err := http.NewRequest("GET", completeUrl, nil)
52+
req.SetBasicAuth(ipcam.Username, ipcam.Password)
53+
if err != nil {
54+
return pictureByte, err
55+
}
56+
57+
res, err := client.Do(req)
58+
59+
if err != nil {
60+
return pictureByte, err
61+
}
62+
63+
defer res.Body.Close()
64+
65+
pictureByte, err = ioutil.ReadAll(res.Body)
66+
67+
if err != nil {
68+
return pictureByte, err
69+
}
70+
71+
return pictureByte, err
72+
}
73+
74+
func (cam *IpCamera) GetPicture() {
75+
picture, err := ipCamGetPicture(cam)
76+
77+
if err != nil {
78+
log.Println(err)
79+
}
80+
81+
cam.Picture <- picture
82+
}

api/raspberrypi.go

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package api

arduino/arduino_server.ino

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
This a simple example of the aREST Library for Arduino (Uno/Mega/Due/Teensy)
3+
using the Ethernet library (for example to be used with the Ethernet shield).
4+
See the README file for more details.
5+
6+
Written in 2014 by Marco Schwartz under a GPL license.
7+
*/
8+
9+
// Libraries
10+
#include <SPI.h>
11+
#include <Ethernet.h>
12+
#include <aREST.h>
13+
#include <avr/wdt.h>
14+
15+
// Enter a MAC address for your controller below.
16+
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xFE, 0x40 };
17+
18+
// IP address in case DHCP fails
19+
IPAddress ip(192,168,0,100);
20+
21+
// Ethernet server
22+
EthernetServer server(80);
23+
// Ethernet Client
24+
EthernetClient newClient;
25+
26+
// Create aREST instance
27+
aREST rest = aREST();
28+
29+
// Variables to be exposed to the API
30+
int temperature;
31+
int humidity;
32+
33+
//char testServer[] = "localhost";
34+
IPAddress testServer(192,168,0,101);
35+
36+
void setup(void)
37+
{
38+
// Start Serial
39+
Serial.begin(9660);
40+
41+
// Init variables and expose them to REST API
42+
temperature = 24;
43+
humidity = 40;
44+
rest.variable("temperature",&temperature);
45+
rest.variable("humidity",&humidity);
46+
47+
// Function to be exposed
48+
rest.function("led",ledControl);
49+
rest.function("test",testControl);
50+
rest.function("testpost", testPost);
51+
52+
// Give name and ID to device
53+
rest.set_id("008");
54+
rest.set_name("dapper_drake");
55+
56+
// Start the Ethernet connection and the server
57+
if (Ethernet.begin(mac) == 0) {
58+
Serial.println("Failed to configure Ethernet using DHCP");
59+
// no point in carrying on, so do nothing forevermore:
60+
// try to congifure using IP address instead of DHCP:
61+
Ethernet.begin(mac, ip);
62+
}
63+
server.begin();
64+
Serial.print("server is at ");
65+
Serial.println(Ethernet.localIP());
66+
67+
// Start watchdog
68+
wdt_enable(WDTO_4S);
69+
}
70+
71+
void loop() {
72+
73+
// listen for incoming clients
74+
EthernetClient client = server.available();
75+
rest.handle(client);
76+
wdt_reset();
77+
78+
}
79+
80+
int testPost(String command) {
81+
Serial.println("Test Post");
82+
//delay(1000);
83+
if (newClient.connect(testServer, 8080)) {
84+
Serial.println("Connected");
85+
newClient.println("GET /opengate HTTP/1.1");
86+
newClient.println("Host: 192.168.0.101");
87+
newClient.println("Connection: close");
88+
newClient.println("");
89+
newClient.stop();
90+
} else {
91+
Serial.println("Not connected");
92+
newClient.stop();
93+
}
94+
}
95+
96+
int testControl(String command) {
97+
Serial.println("Open The GATE!");
98+
return 1;
99+
}
100+
101+
// Custom function accessible by the API
102+
int ledControl(String command) {
103+
104+
Serial.println(command);
105+
// Get state from command
106+
int state = command.toInt();
107+
108+
digitalWrite(6,state);
109+
return 1;
110+
}

0 commit comments

Comments
 (0)