Skip to content

Commit

Permalink
Merge pull request 0c34#2 from muhfaris/fix-package
Browse files Browse the repository at this point in the history
Fix package
  • Loading branch information
0c34 authored Nov 13, 2017
2 parents aa4c9b0 + 3ce934a commit 0849226
Show file tree
Hide file tree
Showing 16 changed files with 327 additions and 333 deletions.
49 changes: 24 additions & 25 deletions setting/setting.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package setting

import(

import (
"fmt"
"strings"
"runtime"
"net/http"
"runtime"
"strings"

"govwa/util"
"govwa/user/session"
"govwa/util/database"
"govwa/util/middleware"
"github.com/0c34/govwa/user/session"
"github.com/0c34/govwa/util"
"github.com/0c34/govwa/util/database"
"github.com/0c34/govwa/util/middleware"

"github.com/julienschmidt/httprouter"
)
Expand All @@ -30,7 +29,7 @@ func (self Setting) SetRouter(r *httprouter.Router) {

}

func settingViewHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params){
func settingViewHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
data := make(map[string]interface{})

data["title"] = "Setting & system info"
Expand All @@ -39,38 +38,38 @@ func settingViewHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Par
data["currentuser"] = getCurrentUserName(r)
data["level"] = getCurrentLevel(r)

util.SafeRender(w,r,"template.setting", data)
util.SafeRender(w, r, "template.setting", data)

}

func setLevelAction(w http.ResponseWriter, r *http.Request, _ httprouter.Params){
if r.Method == "POST" && r.FormValue("level") != ""{
func setLevelAction(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
if r.Method == "POST" && r.FormValue("level") != "" {
level := r.FormValue("level")
util.SetCookieLevel(w, r, level)
res := struct{
res := struct {
Res string
}{
Res : "Success",
Res: "Success",
}
util.RenderAsJson(w, res)
}
}

func getDBMSVersion()string{
const sql=`
func getDBMSVersion() string {
const sql = `
select @@version
`
var version string
db,_ := database.Connect()
db, _ := database.Connect()

_ = db.QueryRow(sql).Scan(&version)
dbversion := strings.Split(version, "-")
return dbversion[0]
}

func getOSVersion()string{
switch os := runtime.GOOS; os{
func getOSVersion() string {

switch os := runtime.GOOS; os {
case "darwin":
return "OS X"
case "linux":
Expand All @@ -81,13 +80,13 @@ func getOSVersion()string{
return ""
}

func getCurrentUserName(r *http.Request)string{
func getCurrentUserName(r *http.Request) string {
s := session.New()
uname := s.GetSession(r, "uname")
return uname
}

func getCurrentLevel(r *http.Request)string{
level := util.GetCookie(r,"Level")
func getCurrentLevel(r *http.Request) string {
level := util.GetCookie(r, "Level")
return level
}
}
36 changes: 18 additions & 18 deletions setup/function.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package setup

import(
"log"
import (
"database/sql"
"log"

"govwa/util/database"
"github.com/0c34/govwa/util/database"
)

const(
DropUsersTable = `DROP TABLE IF EXISTS Users`;
const (
DropUsersTable = `DROP TABLE IF EXISTS Users`

CreateUsersTable = `CREATE TABLE Users (
id int(10) NOT NULL AUTO_INCREMENT,
uname varchar(100) NOT NULL,
pass varchar(100) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1`

InsertUsers = `INSERT INTO Users VALUES (1,'admin','9f3b6fa4703a5ba96fda0dee48ec76fc'),(2,'user1','ff1d5c0015a535b01a5d03a373bf06f6')`

DropProfilesTable = `DROP TABLE IF EXISTS Profile`
Expand All @@ -31,46 +31,46 @@ const(
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1`

InsertProfile = `INSERT INTO Profile VALUES (1,1,'Andro','Jakarta','08882112345'),(2,2,'Rocky','Bandung','08882112345')`

)

var DB *sql.DB
var err error

func init(){
func init() {
DB, err = database.Connect()
if err != nil{
if err != nil {
log.Println(err.Error())
}
}
func createUsersTable()error{
func createUsersTable() error {

_, err = DB.Exec(DropUsersTable)
if err != nil{
if err != nil {
return err
}
_, err = DB.Exec(CreateUsersTable)
if err != nil{
if err != nil {
return err
}
_, err = DB.Exec(InsertUsers)
if err != nil{
if err != nil {
return err
}
return nil
}

func createProfileTable()error{
func createProfileTable() error {
_, err = DB.Exec(DropProfilesTable)
if err != nil{
if err != nil {
return err
}
_, err = DB.Exec(CreateProfilesTable)
if err != nil{
if err != nil {
return err
}
_, err = DB.Exec(InsertProfile)
if err != nil{
if err != nil {
return err
}
return nil
}
}
74 changes: 36 additions & 38 deletions setup/setup.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package setup

import(
import (
"fmt"
"net/http"

"govwa/util"
"govwa/user/session"
"govwa/util/config"
"govwa/util/database"
"govwa/util/middleware"
"github.com/0c34/govwa/user/session"
"github.com/0c34/govwa/util"
"github.com/0c34/govwa/util/config"
"github.com/0c34/govwa/util/database"
"github.com/0c34/govwa/util/middleware"

"github.com/julienschmidt/httprouter"
)
Expand All @@ -28,76 +28,74 @@ func (self Setup) SetRouter(r *httprouter.Router) {

}

type JsonResp struct{
Status string `json:"status"`
type JsonResp struct {
Status string `json:"status"`
Message string `json:"message"`
}

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

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

/* clear login session when setup accessing setup page */
s := session.New()
s.DeleteSession(w, r)
cookies := []string{"Level", "Uid"}
util.DeleteCookie(w,cookies)
util.DeleteCookie(w, cookies)

var info string

data := make(map[string]interface{})
ok, err := database.CheckDatabase()
if !ok || err != nil{

if !ok || err != nil {
info = fmt.Sprintf(`<div id="info" class="alert alert-danger">%s
<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a></div>`,err.Error())
<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a></div>`, err.Error())
data["error"] = util.ToHTML(info)
}else{
} else {

info = fmt.Sprintf(`<div id="info" class="alert alert-success">Connection Success Click Reset to reset Database<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a></div>`)
data["error"] = util.ToHTML(info)
}

data["title"] = "Setup/Reset"
data["weburl"] = config.Fullurl
util.SafeRender(w,r,"template.setup", data)
util.SafeRender(w, r, "template.setup", data)
}

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

if r.Method == "POST" && r.FormValue("act") == "cr" {

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

if r.Method == "POST" && r.FormValue("act") == "cr"{

res := []JsonResp{}
loginUrl := util.ToHTML(fmt.Sprintf(`<a href=%slogin>Login</a>`,config.Fullurl))
loginUrl := util.ToHTML(fmt.Sprintf(`<a href=%slogin>Login</a>`, config.Fullurl))

err = createUsersTable() //create users table
if err != nil{
if err != nil {
res = append(res, JsonResp{
Status : "0",
Message : err.Error(),
Status: "0",
Message: err.Error(),
})
}else{
} else {
res = append(res, JsonResp{
Status : "1",
Message : "Create Users Table Success Please "+string(loginUrl),
Status: "1",
Message: "Create Users Table Success Please " + string(loginUrl),
})
}

err = createProfileTable() //create profilet table
if err != nil{

if err != nil {
res = append(res, JsonResp{
Status : "0",
Message : err.Error(),
Status: "0",
Message: err.Error(),
})
}else{
} else {
res = append(res, JsonResp{
Status : "1",
Message : "Create Profile Table Success Please "+string(loginUrl),
Status: "1",
Message: "Create Profile Table Success Please " + string(loginUrl),
})
}

util.RenderAsJson(w, res)

}
}
}
24 changes: 11 additions & 13 deletions user/session/session.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package session

import (


"log"
"fmt"
"log"
"net/http"
"govwa/util/config"

"github.com/0c34/govwa/util/config"
"github.com/gorilla/sessions"
)

Expand All @@ -28,7 +27,7 @@ func (self *Self) SetSession(w http.ResponseWriter, r *http.Request, data map[st
session.Options = &sessions.Options{
Path: "/",
MaxAge: 3600,
HttpOnly: false, //set to false for xss :)
HttpOnly: false, //set to false for xss :)
}

session.Values["govwa_session"] = true
Expand All @@ -40,13 +39,13 @@ func (self *Self) SetSession(w http.ResponseWriter, r *http.Request, data map[st
}
}
err = session.Save(r, w) //safe session and send it to client as cookie
if err != nil {
log.Println(err.Error())
}

if err != nil {
log.Println(err.Error())
}
}

func (self *Self) GetSession(r *http.Request, key string) string {
func (self *Self) GetSession(r *http.Request, key string) string {
session, err := store.Get(r, "govwa")

if err != nil {
Expand All @@ -63,10 +62,10 @@ func (self *Self) DeleteSession(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Println(err.Error())
}

session.Options = &sessions.Options{
MaxAge: -1,
HttpOnly: false, //set to false for xss :)
HttpOnly: false, //set to false for xss :)
}

session.Values["govwa_session"] = false
Expand All @@ -89,4 +88,3 @@ func (self *Self) IsLoggedIn(r *http.Request) bool {
}
return true
}

Loading

0 comments on commit 0849226

Please sign in to comment.