Home
Crypto
Create
sms-server.go <pre> package main import ( "fmt" "github.com/gorilla/mux" "github.com/gorilla/sessions" // "github.com/fzzy/radix/redis" // "github.com/fatih/structs" "net" "net/http" "html/template" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" "regexp" "strings" "crypto/rand" "time" "strconv" // "encoding/json" // "reflect" ) var ( // MongoDB mongoConfig = "mongodb://localhost:27017/admin" MongoSession, err = mgo.Dial(mongoConfig) MDB = MongoSession.DB("admin") BlogCol = MDB.C("blog") UserCol = MDB.C("user") // Session store = sessions.NewCookieStore([]byte("something-very-secret")) // Blog skipHome = 0 limit = 10 rowsNum = 10 // Tag Factory tagPattern = "#([a-z]|[A-Z]|[_])+" // Path templatePath = "D:\\PROJECTS\\GO-APP\\webkoe\\templates\\" publicPath = "D:\\PROJECTS\\GO-APP\\webkoe\\public\\" ) func cekKodeJabatan(r *http.Request) string { session,_ := store.Get(r, "session-name") kode_jabatan := fmt.Sprintf("%s", session.Values["kode_jabatan"]) return kode_jabatan } func SmsHomeHandler(w http.ResponseWriter, r *http.Request) { if cekKodeJabatan(r) == "" { http.Redirect(w, r, "/login", 301) fmt.Println(cekKodeJabatan(r)) fmt.Println("*** @home -> No Data ") }else{ templates := template.Must(template.New("").ParseFiles(templatePath + "sms-base.html", templatePath + "sms-home.html")) err := templates.ExecuteTemplate(w, "base", cekKodeJabatan(r)) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } } func SmsLoginFormHandler(w http.ResponseWriter, r *http.Request) { if cekKodeJabatan(r) == "" { templates := template.Must(template.New("").ParseFiles(templatePath + "sms-base.html", templatePath + "sms-loginform.html")) err := templates.ExecuteTemplate(w, "base", nil) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } }else{ http.Redirect(w, r, "/", 301) } } func SmsPostLoginHandler(w http.ResponseWriter, r *http.Request) { if cekKodeJabatan(r) == "" { username := r.FormValue("username") password := r.FormValue("password") var results map[string]interface{} err = UserCol.Find(bson.M{"username": username, "password" : password}).One(&results) if err != nil { http.Redirect(w, r, "/#error", 301) }else{ kode_jabatan, _ := results["kode_jabatan"] session, _ := store.Get(r, "session-name") session.Values["kode_jabatan"] = kode_jabatan session.Save(r, w) http.Redirect(w, r, "/", 301) } }else{ http.Redirect(w, r, "/", 301) } } func SmsLogoutHandler(w http.ResponseWriter, r *http.Request) { session, _ := store.Get(r, "session-name") session.Values["kode_jabatan"] = "" session.Save(r, w) http.Redirect(w, r, "/", 301) } func safehtml(text string) template.HTML { text = text + " " text = strings.Replace(text, "<", "<", -1) text = strings.Replace(text, ">", ">", -1) text = strings.Replace(text, "<pre", "<pre", -1) text = strings.Replace(text, "</pre>", "</pre>", -1) text = strings.Replace(text, "<br", "<br", -1) text = strings.Replace(text, "<b>", "<b>", -1) text = strings.Replace(text, "</b>", "</b>", -1) text = strings.Replace(text, "<u>", "<u>", -1) text = strings.Replace(text, "</u>", "</u>", -1) text = strings.Replace(text, "<i>", "<i>", -1) text = strings.Replace(text, "</i>", "</i>", -1) text = strings.Replace(text, "=", "=", -1) text = strings.Replace(text, "<a href=", "<a href=", -1) text = strings.Replace(text, "</a>", "</a>", -1) text = strings.Replace(text, "<img src=", "<img src=", -1) // danger ! text = strings.Replace(text, ">", ">", -1) regexPagar, _ := regexp.Compile(tagPattern) tags := regexPagar.FindAllString(text, -1) for _ , value := range tags { tagAscii := strings.Replace(value, "#", "#", 1) tagOnly := strings.Replace(value, "#", "", 1) text = strings.Replace(text, value, "<a href='/tag/" + tagOnly + "'>" + tagAscii + "</a>", 1) } return template.HTML(text) } func timeFormatter(text string) template.HTML { text = strings.Replace(text, " ", "-", -1) text = strings.Replace(text, ":", "-", -1) split := strings.Split(text, "-") tahun, _ := strconv.Atoi(split[0]) bulan, _ := strconv.Atoi(split[1]) tanggal, _ := strconv.Atoi(split[2]) jam, _ := strconv.Atoi(split[3]) menit, _ := strconv.Atoi(split[4]) detik, _ := strconv.Atoi(split[5]) var _bulan time.Month if bulan == 1 { _bulan = time.January }else if bulan == 2 { _bulan = time.February }else if bulan == 3{ _bulan = time.March }else if bulan == 4{ _bulan = time.April }else if bulan == 5{ _bulan = time.May }else if bulan == 6{ _bulan = time.June }else if bulan == 7{ _bulan = time.July }else if bulan == 8{ _bulan = time.August }else if bulan == 9{ _bulan = time.September }else if bulan == 10{ _bulan = time.October }else if bulan == 11{ _bulan = time.November }else if bulan == 12{ _bulan = time.December } // const layout = "Mon Jan 2, 2006 at 3:04pm (MST)" (MST) --> WITA const layout = "Mon, 2 Jan 2006 3:04 pm" t := time.Date(tahun, _bulan, tanggal, jam, menit, detik, 0, time.Local) return template.HTML(t.Format(layout)) } func randStr(strSize int, randType string) string { var dictionary string if randType == "alphanum" { dictionary = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" } if randType == "alpha" { dictionary = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" } if randType == "number" { dictionary = "0123456789" } var bytes = make([]byte, strSize) rand.Read(bytes) for k, v := range bytes { bytes[k] = dictionary[v%byte(len(dictionary))] } return string(bytes) } func GetIP(r *http.Request) string { if ipProxy := r.Header.Get("X-FORWARDED-FOR"); len(ipProxy) > 0 { return ipProxy } ip, _, _ := net.SplitHostPort(r.RemoteAddr) return ip } func main() { mx := mux.NewRouter() // *** home *** mx.HandleFunc("/", SmsHomeHandler) // *** login *** mx.HandleFunc("/login", SmsLoginFormHandler).Methods("GET") mx.HandleFunc("/login", SmsPostLoginHandler).Methods("POST") // *** logout *** mx.HandleFunc("/logout", SmsLogoutHandler).Methods("GET") // *** static server *** mx.PathPrefix("/").Handler(http.FileServer(http.Dir( publicPath ))) http.ListenAndServe(":80", mx) } </pre> #go #script