-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
1359 lines (1329 loc) · 50.5 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// /* */ //
// /* main.go */ //
package main
import (
"fmt"
"log"
"net/http"
"html/template"
"time"
"github.com/gorilla/mux"
"github.com/gorilla/handlers"
"github.com/upper/db/v4"
"github.com/upper/db/v4/adapter/cockroachdb"
"math"
"os/exec"
"os"
"strconv"
"sort"
"math/rand"
_ "github.com/0pcom/magnets/statik"
//"github.com/0pcom/magnets/statik"
"github.com/rakyll/statik/fs"
//"encoding/json"
flags "github.com/spf13/pflag"
)
//var baseURL = "https://magnetosphere.net"
var port = 8040
//var createpartno string
var (
runapp bool
createtables bool
deleteproducts bool
deleteequipments bool
droptable bool
droptable1 bool
testprod bool
testequip bool
createpartno string
create100p bool
create100e bool
create5000p bool
create5000e bool
importcsv bool
importcsv1 bool
printinv bool
printinv1 bool
vprintinv bool
exportcsv bool
exportcsv1 bool
helpmenu bool
)
func main(){
//TO DO: implement more string arguments for import export and port
flags.BoolVarP(&droptable, "droptables", "d", false, "Drop products table")
flags.BoolVarP(&droptable1, "droptables1", "b", false, "Drop equipments table")
flags.BoolVarP(&deleteproducts, "deleteall", "D", false, "Delete the products in the products database")
flags.BoolVarP(&deleteequipments, "deleteall1", "E", false, "Delete the equipment in the equipments database")
flags.BoolVarP(&createtables, "createtables", "c", false, "Create the tables if they do not exist")
flags.BoolVarP(&testprod, "testprod", "t", false, "create test product")
flags.BoolVarP(&testequip, "testequip", "u", false, "create test equipment")
flags.StringVarP(&createpartno, "createpartno", "C", "", "Create a part by providing the part number")
flags.BoolVarP(&create5000p, "create5000p", "Y", false, "Create 5000 products with sequential part numbers")
flags.BoolVarP(&create5000e, "create5000e", "Z", false, "Create 5000 equipments with sequential part numbers")
flags.BoolVarP(&create100p, "create100p", "y", false, "Create 100 products with sequential part numbers")
flags.BoolVarP(&create100e, "create100e", "z", false, "Create 100 equipments with sequential part numbers")
flags.BoolVarP(&exportcsv, "exportcsv", "e", false, "Export products to productsexport01.csv")
flags.BoolVarP(&exportcsv1, "exportcsv1", "f", false, "Export equipments to equipmentsexport01.csv")
flags.BoolVarP(&printinv, "printinv", "p", false, "Print the products table to the terminal")
flags.BoolVarP(&printinv1, "printinv1", "q", false, "Print the equipments table to the terminal")
//flags.BoolVarP(&printinv2, "printinv2", "q", false, "Print the inventory - BTCPayserver format")
flags.BoolVarP(&vprintinv, "vprintinv", "v", false, "More verbose printinventory")
flags.BoolVarP(&importcsv, "importcsv", "i", false, "Import products csv from http://127.0.0.1:8079/export01.csv")
flags.BoolVarP(&importcsv1, "importcsv1", "j", false, "Import equipments csv from http://127.0.0.1:8079/export01.csv")
flags.BoolVarP(&runapp, "run", "r", false, "run the web app")
flags.BoolVarP(&helpmenu, "help", "h", false, "show this help menu")
flags.Parse()
var cmdargs int = 0
// /* help menu */ //
if helpmenu {
helpmenu1()
os.Exit(0)
}
// /* database connection */ //
fmt.Printf("Initializing cockroachDB connection\n")
sess, err := cockroachdb.Open(settings) //establish the session
if err != nil {
log.Fatal("cockroachdb.Open: ", err)
}
defer sess.Close()
// /* drop tables */ //
if droptable {
dropProductsTable(sess)
cmdargs = 1
}
// /* drop tables */ //
if droptable1 {
dropEquipmentsTable(sess)
cmdargs = 1
}
// /* create tables */ //
if createtables {
createProductsTableIfNotExists(sess)
createEquipmentsTableIfNotExists(sess)
cmdargs = 1
}
// /* delete products */ //
if deleteproducts {
defineproducts(sess)
deleteAllProducts(sess)
cmdargs = 1
}
// /* delete products */ //
if deleteequipments {
defineequipments(sess)
deleteAllEquipments(sess)
cmdargs = 1
}
// /* Create Test Product */ //
if testprod {
defineproducts(sess)
createTestProd(sess)
cmdargs = 1
}
// /* Create Test Product */ //
if testequip {
defineequipments(sess)
createTestEquip(sess)
cmdargs = 1
}
// /* create part */ //
if createpartno != "" {
fmt.Println("createpart has value ", createpartno)
createProduct(sess, createpartno)
cmdargs = 1
}
// /* Create 100 Product */ //
if create5000p {
fmt.Println("creating 5000 products")
create5kP(sess)
cmdargs = 1
}
// /* Create 100 Equipment */ //
if create5000e {
fmt.Println("creating 5000 equipments")
create5kE(sess)
cmdargs = 1
}
if create100p {
fmt.Println("creating 100 products")
createHundredP(sess)
cmdargs = 1
}
// /* Create 100 Equipment */ //
if create100e {
fmt.Println("creating 100 equipments")
createHundredE(sess)
cmdargs = 1
}
// /* print products table inventory */ //
if printinv {
defineproducts(sess)
log.Printf("products:")
for i := range products {
//fmt.Printf("product #%d: %#v\n", i, products[i])
fmt.Printf("product[%d]:\n", products[i].Id)
fmt.Printf("\tpartno: "); fmt.Printf("%s\n", products[i].PartNo)
if products[i].Image1 != "" { fmt.Printf("\tImage1: "); fmt.Printf("%s\n", products[i].Image1) }
if products[i].Name != "" { fmt.Printf("\tName: "); fmt.Printf("%s\n", products[i].Name) }
fmt.Printf("\tQty: "); fmt.Printf("%d\n", products[i].Qty)
fmt.Printf("\tPrice: "); fmt.Printf("%.2f\n", products[i].Price)
fmt.Printf("\tEnable: "); fmt.Printf("%t\n", products[i].Enable)
}
cmdargs = 1
}
// /* print equipments table inventory*/ //
if printinv1 {
defineequipments(sess)
log.Printf("equipments:")
for i := range equipments {
//fmt.Printf("product #%d: %#v\n", i, products[i])
fmt.Printf("equipment[%d]:\n", equipments[i].Id)
fmt.Printf("\tpartno: "); fmt.Printf("%s\n", equipments[i].PartNo)
if equipments[i].Image1 != "" { fmt.Printf("\tImage1: "); fmt.Printf("%s\n", equipments[i].Image1) }
if equipments[i].Name != "" { fmt.Printf("\tName: "); fmt.Printf("%s\n", equipments[i].Name) }
fmt.Printf("\tQty: "); fmt.Printf("%d\n", equipments[i].Qty)
fmt.Printf("\tPrice: "); fmt.Printf("%.2f\n", equipments[i].Price)
fmt.Printf("\tEnable: "); fmt.Printf("%t\n", equipments[i].Enable)
}
cmdargs = 1
}
// /* print inventory so it can be pasted in the btcpayserver raw editor! */ //
/*
if printinv2 {
defineproducts(sess)
log.Printf("products:")
for i := range products {
if products[i].Enable {
fmt.Printf("%s:\n", products[i].PartNo)
fmt.Printf(" price: "); fmt.Printf("%.2f\n", products[i].Price)
fmt.Printf(" title: "); fmt.Printf("%s\n", products[i].Name)
fmt.Printf(" description: "); fmt.Printf("%s\n", products[i].Description1)
fmt.Printf(" image: "); fmt.Printf("%s/img/%s\n", siteURL, products[i].Image1)
}
}
cmdargs = 1
}
*/
// /* more verbosely print inventory */ //
if vprintinv {
defineproducts(sess)
log.Printf("products:")
for i := range products {
fmt.Printf("product #%d: %#v\n", i, products[i])
cmdargs = 1
}
}
// /* export products table to csv RELIES ON MAKEFILE - COCKROACHDB COMMANDS */ //
if exportcsv {
fmt.Println("Exporting csv to productsexport01.csv")
exportProductsCSV()
cmdargs = 1
}
// /* export equipments table csv RELIES ON MAKEFILE - COCKROACHDB COMMANDS */ //
if exportcsv1 {
fmt.Println("Exporting csv to productsexport01.csv")
exportEquipmentsCSV()
cmdargs = 1
}
// /* import csv to products table */ //
if importcsv {
fmt.Println("Import a csv into products table from http://127.0.0.1:8079/productsexport01.csv")
importProductsCSV(sess)
cmdargs = 1
}
// /* import csv to equipments table */ //
if importcsv1 {
fmt.Println("Import a csv into equipments table from http://127.0.0.1:8079/equipmentsexport01.csv")
importEquipmentsCSV(sess)
cmdargs = 1
}
// /* run the web app */ //
if runapp {
statikFS, err := fs.New()
if err != nil {
log.Fatal(err)
}
defineproducts(sess)
defineequipments(sess)
r := mux.NewRouter() //.StrictSlash(true)
r.NotFoundHandler = page404()
r.PathPrefix("/img/").Handler(http.StripPrefix("/img/", http.FileServer(http.Dir("./img")))) //images
//products table - main site original endpoints
r.Handle("/list", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(listPage))).Methods("GET") //pagination
r.Handle("/list/", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(listPage))).Methods("GET") //pagination
r.Handle("/list/{id:[0-9]+}", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(listPage))).Methods("GET") //pagination
r.Handle("/post/{slug}", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(findProduct))).Methods("GET") //individual product page
r.Handle("/cat/{slug}", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(categoryPage))).Methods("GET") //category
r.Handle("/cat/{slug}/{id:[0-9]+}", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(categoryPage))).Methods("GET") //category pagination
// equipment table endpoints
r.Handle("/equipment", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(equipmentPage))).Methods("GET") //equipment
r.Handle("/equipment/p/{id:[0-9]+}", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(equipmentPage))).Methods("GET") //equipment pagination
r.Handle("/equipment/post/{slug}", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(findEquipment))).Methods("GET") //individual product page
r.Handle("/equipment/cat/{slug}", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(categoryEquipmentPage))).Methods("GET") //category
r.Handle("/equipment/cat/{slug}/{id:[0-9]+}", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(categoryEquipmentPage))).Methods("GET") //category pagination
//single pages
r.Handle("/about", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(aboutPage))).Methods("GET") //about page
r.Handle("/blog", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(blogPage))).Methods("GET") //about page
r.Handle("/blog/", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(blogPage))).Methods("GET") //about page
r.Handle("/blog/{slug}/", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(blogPage))).Methods("GET") //about page
r.Handle("/friend", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(friendPage))).Methods("GET") //friends page
r.Handle("/friend/", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(friendPage))).Methods("GET") //friends page
r.Handle("/policy", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(policyPage))).Methods("GET") //shipping page
r.Handle("/policy/", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(policyPage))).Methods("GET") //shipping page
//r.Handle("/time", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(timeFunc))).Methods("GET") //shipping page
//
r.Handle("/{slug}", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(frontPage))).Methods("GET") //site root
r.Handle("/", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(frontPage))).Methods("GET") //site root
r.Handle("/p/{id:[0-9]+}", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(pagePage))).Methods("GET") //pagination
r.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(statikFS))) //statik sources
r.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// this handler behaved as I would expect the r.NotFoundHandler to behave..
w.WriteHeader(501)
w.Write([]byte(`{"status":501,"message":"501: Not implemented."}`))
})
Serve = r
fmt.Printf("listening on http://127.0.0.1:%d using gorilla router\n", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), Serve))
cmdargs = 1
}
if cmdargs == 0 { helpmenu1() }
}
// custom 404 not found page
func page404() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
wd, err := os.Getwd()
if err != nil { log.Fatal(err) }
tpl1 := template.Must(template.New("").Funcs(fm).ParseFiles(wd + "/public/404.html"))
if err := tpl1.ExecuteTemplate(w, "404.html", nil); err != nil { fmt.Printf("error: %s", err) }
})
}
// custom 404 not found page
func page404Page (w http.ResponseWriter, r *http.Request) {
wd, err := os.Getwd()
if err != nil { log.Fatal(err) }
tpl1 := template.Must(template.ParseFiles(wd + "/public/404.html"))
if err := tpl1.ExecuteTemplate(w, "404.html", nil); err != nil { fmt.Printf("error: %s", err) }
}
func helpmenu1() {
fmt.Printf("Usage: magnets -dDctCyepirh\n")
fmt.Printf("\tSuggested Demo: magnets -ctpr\n")
flags.PrintDefaults()
}
// /* defines products and categories from sess / fetch from database */ //
func defineproducts(sess db.Session) {
//define products
productsCol := Products(sess)
products = []Product{}
err = productsCol.Find().All(&products) // Find().All() maps all the records from the products collection.
if err != nil {
log.Fatal("productsCol.Find: ", err)
}
//count products
for i := 0; i < len(products); i++ {
if products[i].Enable == true {
lenproducts = lenproducts + 1
}
}
fmt.Printf("%d Products\n", lenproducts)
//define categories
var cat1 []string
for i:= range products{
if products[i].Category != "" {
cat1 = append(cat1, products[i].Category)
}
}
distProdCats(cat1)
//countProdCats(cat1)
}
// /* defines products and categories from sess / fetch from database */ //
func defineequipments(sess db.Session) {
//define equipments
equipmentsCol := Equipments(sess)
equipments = []Equipment{}
err = equipmentsCol.Find().All(&equipments) // Find().All() maps all the records from the products collection.
if err != nil {
log.Fatal("equipmentsCol.Find: ", err)
}
//define categories
var cat1 []string
for i:= range equipments{
if equipments[i].Category != "" {
cat1 = append(cat1, equipments[i].Category)
}
}
distEquipCats(cat1)
}
var pcats []Category
func distProdCats(cat1 []string){
for i:= range cat1{
if pcats == nil || len(pcats)==0{ pcats = append(pcats, Category{Name: cat1[i], Count: 1}) } else {
founded:=false
for j:= range pcats{
if pcats[j].Name == cat1[i] {
founded=true
pcats[j].Count += 1
}
}
if !founded{
pcats = append(pcats, Category{Name: cat1[i], Count: 1})
}
}
}
sort.Sort(alphab(pcats))
}
type alphab []Category
func (cat alphab) Len() int { return len(cat) }
func (cat alphab) Less(i, j int) bool { return cat[i].Name < cat[j].Name }
func (cat alphab) Swap(i, j int) { cat[i], cat[j] = cat[j], cat[i] }
var ecats []Category
func distEquipCats(cat1 []string){
for i:= range cat1{
if ecats == nil || len(ecats)==0{ ecats = append(ecats, Category{Name: cat1[i], Count: 1}) } else {
founded:=false
for j:= range ecats{
if ecats[j].Name == cat1[i] {
founded=true
ecats[j].Count += 1
}
}
if !founded{
ecats = append(ecats, Category{Name: cat1[i], Count: 1})
}
}
}
sort.Sort(alphab(ecats))
}
//package gorilla // Routing based on the gorilla/mux router
var Serve http.Handler
var ppartno Product
var epartno Equipment
var category string
var products []Product
var lenproducts int
var equipments []Equipment
var lenequipments int
//these are functions called from the template or webpage
var fm = template.FuncMap{
"fdateMDY": monthDayYear,
"snipcartapikey": snipcartApiKey,
"multiply": multiply,
"correct": correct,
"convertozgrams": convertozgrams,
"convertincm": convertincm,
"lenprods": lenprods,
"productListPage": productListPage,
"listPage1": listPage1,
"productIndexPage": productIndexPage,
"indexPage1": indexPage1,
"equipmentListPage": equipmentListPage,
"nextPage": nextPage,
"prevPage": prevPage,
"listCategories": listCategories,
"productsCategoryListPage": productsCategoryListPage,
"equipmentsCategoryListPage": equipmentsCategoryListPage,
"findProduct1": findProduct1,
"findEquipment1": findEquipment1,
//"pagination": pagination,
}//below are the functions
// /* timepage */ //
//func monthDayYear(t time.Time) string {
func monthDayYear() string {
return time.Now().Format("Monday January 2, 2006 15:04:05")
}
func multiply(a float64, b float64) float64 { //generic multiply
return a * b
}
func correct(a float64) string { //correct the number of decimals
return fmt.Sprintf("%.2f", a)
}
func convertozgrams(a float64) float64 { //convert ounces to grams for snipcart
return math.Round((a*28.35)*100)/100
}
func convertincm(a float64) float64 { //convert inches to centimeters for snipcart
return math.Round((a*2.54)*100)/100
}
//func timeFunc(w http.ResponseWriter, r *http.Request) { //a page with just the time
// w.Header().Set("Content-Type", "text/html")
// tp1 := template.Must(template.New("").Funcs(fm).ParseFiles("time.html"))
// if err := tp1.ExecuteTemplate(w, "time.html",nil); err != nil { fmt.Printf("error: %s", err) }
//}
// /* return list of categories */ //
func listCategories(table string) []Category {
var toreturn []Category
if table == "products" {
toreturn = pcats
}
if table == "equipments" {
toreturn = ecats
}
return toreturn
}
func nextPage(a int) int {//the "current context" of the template ({{.}}) is now an index
return a + 1
}
func prevPage(a int) int {//check for nonzero argument is done on the template side
return a - 1
}
func nextProd(a int) int {//the "current context" of the template ({{.}}) is now an index
return a + 1
}
func prevProd(a int) int {//check for nonzero argument is done on the template side
return a - 1
}
var snipcartapikey string = os.Getenv("SNIPCARTAPIKEY")
func snipcartApiKey() string {
if snipcartapikey == "" {
log.Fatal("error no api key found")
}
return snipcartapikey
}
//these structs are passed to the template by the handler Funcs
//like an index, providing context to the template
//the template then calls the mapped functions, passing the values that were passed to it
//returned from there is the data which is rendered on the page
type Page struct {
Table string //specifies the table; products or equipments
Category string //category
PageNumber int //pagination
}
type SubPage struct {
Table string
PartNumber string
}
// /* individual product page ENDPOINT: /post/{slug} */ //
func findProduct(w http.ResponseWriter, r *http.Request) { //, product string
slug := mux.Vars(r)["slug"]
a := false
for i := range products {
if products[i].PartNo == slug {
a = true
break // Found!
}
}
if !a {
fmt.Fprint(w, "No product found for part number:\n", slug)
} else {
productp := SubPage{"products", slug}
wd, err := os.Getwd()
if err != nil { log.Fatal(err) }
tpl1 := template.Must(template.New("").Funcs(fm).ParseFiles(wd + "/public/post/product/index.html"))
//tpl1, err := template.Must(template.New("").Funcs(fm).ParseFiles(wd + "/public/post/product/index.html"))
//if err != nil {
// fmt.Printf("error: %s", err)
// return
//}
if err := tpl1.ExecuteTemplate(w, "index.html", productp); err != nil { fmt.Printf("error: %s", err) }
}
}
// /* function called from template for above endpoint */ //
func findProduct1(part string) []Product { //, product string
var ppartno []Product
for i := range products {
if products[i].PartNo == part {
ppartno = append(ppartno, products[i])
break // Found!
}
}
return ppartno
}
// /* individual equipment page ENDPOINT: /equipment/post/{slug} */ //
func findEquipment(w http.ResponseWriter, r *http.Request) { //, product string
slug := mux.Vars(r)["slug"]
a := false
for i := range equipments {
if equipments[i].PartNo == slug {
a = true
break // Found!
}
}
if !a {
fmt.Fprint(w, "No equipment found for part number:\n", slug)
} else {
equipmentp := SubPage{"equipments", slug}
wd, err := os.Getwd()
if err != nil { log.Fatal(err) }
tpl1 := template.Must(template.New("").Funcs(fm).ParseFiles(wd + "/public/post/product/index.html"))
if err := tpl1.ExecuteTemplate(w, "index.html", equipmentp); err != nil { fmt.Printf("error: %s", err) }
}
}
// /* function called from template for above endpoint */ //
func findEquipment1(part string) []Equipment { //, product string
var epartno []Equipment
for i := range equipments {
if equipments[i].PartNo == part {
epartno = append(epartno, equipments[i])
break // Found!
}
}
return epartno
}
// /* individual category page ENDPOINT: /cat/{slug}/{id:[0-9]+} */ //
func categoryPage(w http.ResponseWriter, r *http.Request) {
slug := mux.Vars(r)["slug"]
vars := mux.Vars(r)
id, _ := strconv.Atoi(vars["id"])
a := false
for k := range pcats {
if pcats[k].Name == slug {
a = true
}
}
if !a {
fmt.Fprint(w, "No product category matching\n", slug)
} else {
categoryp := Page{"products", slug, id}
wd, err := os.Getwd()
if err != nil { log.Fatal(err) }
tpl1 := template.Must(template.New("").Funcs(fm).ParseFiles(wd + "/public/index.html"))
if err := tpl1.ExecuteTemplate(w, "index.html", categoryp); err != nil { fmt.Printf("error: %s", err) }
}
}
// /* individual equipment category page ENDPOINT: /equipment/cat/{slug}/{id:[0-9]+} */ //
func categoryEquipmentPage(w http.ResponseWriter, r *http.Request) {
slug := mux.Vars(r)["slug"]
vars := mux.Vars(r)
id, _ := strconv.Atoi(vars["id"])
a := false
for k := range ecats {
if ecats[k].Name == slug {
a = true
}
}
if !a {
fmt.Fprint(w, "No equipment category matching\n", slug)
} else {
categoryp := Page{"equipments", slug, id}
wd, err := os.Getwd()
if err != nil { log.Fatal(err) }
tpl1 := template.Must(template.New("").Funcs(fm).ParseFiles(wd + "/public/index.html"))
if err := tpl1.ExecuteTemplate(w, "index.html", categoryp); err != nil { fmt.Printf("error: %s", err) }
}
}
//}
// /* Front Page - main page ENDPOINT: magnetosphere.net/
func frontPage(w http.ResponseWriter, r *http.Request) {
slug := mux.Vars(r)["slug"]
wd, err := os.Getwd()
if err != nil { log.Fatal(err) }
if slug == "" {
tp1 := template.Must(template.New("").Funcs(fm).ParseFiles(wd + "/public/index.html"))
if err = tp1.ExecuteTemplate(w, "index.html", Page{"products", "", 0}); err != nil { fmt.Printf("error: %s", err) }
} else {
tp1 := template.Must(template.New("").Funcs(fm).ParseFiles(wd + "/public/404.html"))
if err = tp1.ExecuteTemplate(w, "404.html", nil); err != nil { fmt.Printf("error: %s", err) }
}
}
// /* page Page - /p/ page ENDPOINT: magnetosphere.net/p/{id:[0-9]+} */ //
func pagePage(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, _ := strconv.Atoi(vars["id"])
wd, err := os.Getwd()
if err != nil { log.Fatal(err) }
fp := Page{"products", "", id} //no category specified here
tp1 := template.Must(template.New("").Funcs(fm).ParseFiles(wd + "/public/index.html"))
if err := tp1.ExecuteTemplate(w, "index.html", fp); err != nil { fmt.Printf("error: %s", err) }
}
// /* List Page - text-only listings main page ENDPOINT: magnetosphere.net/list OR: magnetosphere.net/list/{id:[0-9]+} */ //
func listPage(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, _ := strconv.Atoi(vars["id"])
wd, err := os.Getwd()
if err != nil { log.Fatal(err) }
fp := Page{"products", "", id} //no category specified here
tp1 := template.Must(template.New("").Funcs(fm).ParseFiles(wd + "/public/list/index.html"))
if err := tp1.ExecuteTemplate(w, "index.html", fp); err != nil { fmt.Printf("error: %s", err) }
}
// /* Equipment Page - main equipment page ENDPOINT magnetosphere.net/equipment OR magnetosphere.net/equipment/p/{id:[0-9]+} */ //
func equipmentPage(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, _ := strconv.Atoi(vars["id"])
wd, err := os.Getwd()
if err != nil { log.Fatal(err) }
fp := Page{"equipments", "", id} //no category specified here
tp1 := template.Must(template.New("").Funcs(fm).ParseFiles(wd + "/public/index.html"))
if err := tp1.ExecuteTemplate(w, "index.html", fp); err != nil { fmt.Printf("error: %s", err) }
}
// /* function called from the template to get products by page */ //
func productListPage(pagenumber int) []Product {
var products1 []Product
fmt.Println(pagenumber)
lenprod := len(products)
if pagenumber == 0 {
for i := 0; i < lenprod; i++ {
if products[i].Enable == true {
products1 = append(products1, products[i])
}
//pull products from the whole database for the first page!
randnu := rand.New(rand.NewSource(time.Now().Unix()))
products2 := make([]Product, len(products1))
perm := randnu.Perm(len(products1))
for i, randIndex := range perm {
products2[i] = products1[randIndex]
}
}
} else {
//subsequent pages
for i := ((pagenumber - 1) * 10); len(products1) < 10; i++ {
if products[i].Enable == false {break}
products1 = append(products1, products[i])
}
}
//} //randomize the order in which products appear on the page
randnu := rand.New(rand.NewSource(time.Now().Unix()))
products2 := make([]Product, len(products1))
perm := randnu.Perm(len(products1))
for i, randIndex := range perm {
products2[i] = products1[randIndex]
}
// //randomize the categories products are selected from
randnu1 := rand.New(rand.NewSource(time.Now().Unix()))
cats2 := make([]Category, len(pcats))
perm1 := randnu1.Perm(len(pcats))
for i, randIndex := range perm1 {
cats2[i] = pcats[randIndex]
}
cats2 = cats2[:10]
//limit to 10 results!
//k := len(cats2)
//l := len(products2)
products3 := make([]Product, 10)
//m := len(products3)
for i:= range products2 {
if products3 == nil || len(products3)==0{ products3 = append(products3, products2[i]) } else {
founded:=false
for j:= range products3{
if products3[j].Category == products2[i].Category {
founded=true
}
}
if !founded{
products3 = append(products3, products2[i])
}
}
}
//sort.Sort(alphab(pcats))
// for i := 0; i < 10; i++ {
// products3[i] = products2[i]
// }
return products3
}
// /* function called from the template to get products by page */ //
func productIndexPage(pagenumber int) int {
var products1 []Product
for i := (pagenumber * 10); len(products1) < 10; i++ {
if products[i].Enable == false {break}
products1 = append(products1, products[i])
}
fmt.Println(len(products1))
return len(products1)
}
// /* function called from the template to get products by page */ //
func lenprods() int {
return lenproducts
}
// /* function called from the template to get equipments by page */ //
func equipmentListPage(pagenumber int) []Equipment {
//upper := (pagenumber + 1) * 10
//lower := pagenumber * 10
var equipments1 []Equipment
fmt.Println(pagenumber)
//var a int
// a = len(equipments)
//b := 0
//if a < 10 { b = a } else { b = 10 }
//if a < upper { upper = a }
//if pagenumber == 0 {
for i := 0; len(equipments1) < 10; i++ {
if equipments[i].Enable == false {break}
equipments1 = append(equipments1, equipments[i])
}
//for i := 0; i < b; i++ {
// if equipments[i].Enable == false {break}
// equipments1 = append(equipments1, equipments[i])
// }
//} else {
// for i := lower; i < upper; i++ {
// if equipments[i].Enable == false {break}
// equipments1 = append(equipments1, equipments[i])
// }
// }
//} //randomize the order in which products appear on the page
//var products2
randnu := rand.New(rand.NewSource(time.Now().Unix()))
equipments2 := make([]Equipment, len(equipments1))
perm := randnu.Perm(len(equipments1))
for i, randIndex := range perm {
equipments2[i] = equipments1[randIndex]
}
return equipments2
}
// /* function called from the template to get products by category & page */ //
func productsCategoryListPage(cat string, pagenumber int) []Product {
var categoryProducts []Product
var categoryProducts1 []Product
for i := range products { if products[i].Category == cat { categoryProducts = append(categoryProducts, products[i]) } }
upper := (pagenumber + 1) * 10
lower := pagenumber * 10
fmt.Println(pagenumber)
a := len(categoryProducts)
b := 0
if a < 10 { b = a } else { b = 10 }
if a < upper { upper = a }
if pagenumber == 0 {
for i := 0; i < b; i++ {
if categoryProducts[i].Enable == false {break}
categoryProducts1 = append(categoryProducts1, categoryProducts[i])
}
} else {
for i := lower; i < upper; i++ {
if categoryProducts[i].Enable == false {break}
categoryProducts1 = append(categoryProducts1, categoryProducts[i])
}
} //randomize the order in which products appear on the page
randnu := rand.New(rand.NewSource(time.Now().Unix()))
categoryProducts2 := make([]Product, len(categoryProducts1))
perm := randnu.Perm(len(categoryProducts1))
for i, randIndex := range perm { categoryProducts2[i] = categoryProducts1[randIndex] }
return categoryProducts2
}
// /* function called from the template to get products by category & page */ //
func equipmentsCategoryListPage(cat string, pagenumber int) []Equipment {
var categoryEquipments []Equipment
var categoryEquipments1 []Equipment
for i := range equipments { if equipments[i].Category == cat { categoryEquipments = append(categoryEquipments, equipments[i]) } }
upper := (pagenumber + 1) * 10
lower := pagenumber * 10
fmt.Println(pagenumber)
a := len(categoryEquipments)
b := 0
if a < 10 { b = a } else { b = 10 }
if a < upper { upper = a }
if pagenumber == 0 {
for i := 0; i < b; i++ {
if categoryEquipments[i].Enable == false {break}
categoryEquipments1 = append(categoryEquipments1, categoryEquipments[i])
}
} else {
for i := lower; i < upper; i++ {
if categoryEquipments[i].Enable == false {break}
categoryEquipments1 = append(categoryEquipments1, categoryEquipments[i])
} //randomize the order in which products appear on the page
}
randnu := rand.New(rand.NewSource(time.Now().Unix()))
categoryEquipments2 := make([]Equipment, len(categoryEquipments1))
perm := randnu.Perm(len(categoryEquipments1))
for i, randIndex := range perm { categoryEquipments2[i] = categoryEquipments1[randIndex] }
return categoryEquipments2
}
// /* function called from the template to get products for the text-only listings page */ //
func listPage1(pagenumber int) []Product {
//if pagenumber == 0 {
return products
// }
}
// /* function called from the template to get index number for products by page */ //
func indexPage1(pagenumber int) int {
var products1 []Product
for i := 0; i < len(products1); i++ {
if products[i].Enable == false {break}
products1 = append(products1, products[i])
}
// fmt.Println(len(products1))
return len(products1)
}
// /* Blog Pages - main page ENDPOINT: magnetosphere.net/ OR: magnetosphere.net/p/{id:[0-9]+} */ //
func blogPage(w http.ResponseWriter, r *http.Request) {
slug := mux.Vars(r)["slug"]
wd, err := os.Getwd()
if err != nil { log.Fatal(err) }
if slug == "" {
tp1 := template.Must(template.New("").Funcs(fm).ParseFiles(wd + "/public/blog/index.html"))
if err = tp1.ExecuteTemplate(w, "index.html", nil); err != nil { fmt.Printf("error: %s", err) }
} else {
tp1 := template.Must(template.New("").Funcs(fm).ParseFiles(wd + "/public/blog/" + slug + "/index.html"))
if err = tp1.ExecuteTemplate(w, "index.html", nil); err != nil { fmt.Printf("error: %s", err) }
}
}
// /* Single Pages */ //
// /* About Page */ //
func aboutPage(w http.ResponseWriter, r *http.Request) {
wd, err := os.Getwd()
if err != nil { log.Fatal(err) }
tp1 := template.Must(template.New("").Funcs(fm).ParseFiles(wd + "/public/about/index.html"))
if err := tp1.ExecuteTemplate(w, "index.html", nil); err != nil { fmt.Printf("error: %s", err) }
}
// /* friends Page */ //
func friendPage(w http.ResponseWriter, r *http.Request) {
wd, err := os.Getwd()
if err != nil { log.Fatal(err) }
tp1 := template.Must(template.New("").Funcs(fm).ParseFiles(wd + "/public/friend/index.html"))
if err := tp1.ExecuteTemplate(w, "index.html", nil); err != nil { fmt.Printf("error: %s", err) }
}
// /* Shipping / orders policy Page */ //
func policyPage(w http.ResponseWriter, r *http.Request) {
wd, err := os.Getwd()
if err != nil { log.Fatal(err) }
tp1 := template.Must(template.New("").Funcs(fm).ParseFiles(wd + "/public/policy/index.html"))
if err := tp1.ExecuteTemplate(w, "index.html", nil); err != nil { fmt.Printf("error: %s", err) }
}
// /* */ //
func NewRouter() *mux.Router {
router := mux.NewRouter().StrictSlash(true)
staticDir := "/static/" // Choose the folder to serve
router. // Create the route
PathPrefix(staticDir).
Handler(http.StripPrefix(staticDir, http.FileServer(http.Dir("."+staticDir))))
return router
}
// /* database stuff */ //
var err error
var settings = cockroachdb.ConnectionURL{
Host: "localhost",
Database: "product",
User: "madmin",
Options: map[string]string{ // Secure node.
"sslrootcert": "certs/ca.crt",
"sslkey": "certs/client.madmin.key",
"sslcert": "certs/client.madmin.crt",
},
}
// /* from upper/db tutorial -OR- cockroachdb documentation for upper/db */ //
func Products(sess db.Session) db.Store { // Products is a handy way to represent a collection.
return sess.Collection("products")
}
func Equipments(sess db.Session) db.Store { // Equipments is a handy way to represent a collection.
return sess.Collection("equipments")
}
type Product struct { // Product is used to represent a single record in the "products" table.
Id int64 `db:"id,omitempty" json:"id,omitempty"`
Image1 string `db:"image1,omitempty" json:"image1,omitempty"`
Image2 string `db:"image2,omitempty" json:"image2,omitempty"`
Image3 string `db:"image3,omitempty" json:"image3,omitempty"`
Thumb string `db:"thumb,omitempty" json:"thumb,omitempty"`
PartNo string `db:"partno" json:"partno"`
Name string `db:"name" json:"name"`
Enable bool `db:"enable,omitempty" json:"enable,omitempty"`
Price float64 `db:"price" json:"price"`
Qty int64 `db:"quantity" json:"quantity"`
MinOrder int64 `db:"minorder,omitempty" json:"minorder,omitempty"`
MaxOrder int64 `db:"maxorder,omitempty" json:"maxorder,omitempty"`
Shippable bool `db:"shippable,omitempty" json:"shippable,omitempty"`
UnlimitQty bool `db:"unlimitqty,omitempty" json:"unlimitqty,omitempty"`
DefaultQty int64 `db:"defaultquantity,omitempty" json:"defaultquantity,omitempty"`
StepQty int64 `db:"stepquantity,omitempty" json:"stepquantity,omitempty"`
MfgPartNo string `db:"mfgpartno,omitempty" json:"mfgpartno,omitempty"`
MfgName string `db:"mfgname,omitempty" json:"mfgname,omitempty"`
Category string `db:"category,omitempty" json:"category,omitempty"`
SubCategory string `db:"subcategory,omitempty" json:"subcategory,omitempty"`
Location string `db:"location,omitempty" json:"location,omitempty"`
Msrp float64 `db:"msrp,omitempty" json:"msrp,omitempty"`
Cost float64 `db:"cost,omitempty" json:"cost,omitempty"`
Type string `db:"type,omitempty" json:"type,omitempty"`
PackageType string `db:"packagetype,omitempty" json:"packagetype,omitempty"`
Technology string `db:"technology,omitempty" json:"technology,omitempty"`
Materials string `db:"materials,omitempty" json:"materials,omitempty"`
Value float64 `db:"value,omitempty" json:"value,omitempty"`
ValUnit string `db:"valunit,omitempty" json:"valunit,omitempty"`
Resistance float64 `db:"resistance,omitempty" json:"resistance,omitempty"`
ResUnit string `db:"resunit,omitempty" json:"resunit,omitempty"`
Tolerance float64 `db:"tolerance,omitempty" json:"tolerance,omitempty"`
VoltsRating float64 `db:"voltsrating,omitempty" json:"voltsrating,omitempty"`
AmpsRating float64 `db:"ampsrating,omitempty" json:"ampsrating,omitempty"`
WattsRating float64 `db:"wattsrating,omitempty" json:"temprating,omitempty"`
TempRating float64 `db:"temprating,omitempty" json:"temprating,omitempty"`
TempUnit string `db:"tempunit,omitempty" json:"tempunit,omitempty"`
Description1 string `db:"description1,omitempty" json:"description1,omitempty"`
Description2 string `db:"description2,omitempty" json:"description2,omitempty"`
Color1 string `db:"color1,omitempty" json:"color1,omitempty"`
Color2 string `db:"color2,omitempty" json:"color2,omitempty"`
Sourceinfo string `db:"sourceinfo,omitempty" json:"sourceinfo,omitempty"`
Datasheet string `db:"datasheet,omitempty" json:"datasheet,omitempty"`
Docs string `db:"docs,omitempty" json:"docs,omitempty"`
Reference string `db:"reference,omitempty" json:"reference,omitempty"`
Attributes string `db:"attributes,omitempty" json:"attributes,omitempty"`
Year int64 `db:"year,omitempty" json:"year,omitempty"`
Condition string `db:"condition,omitempty" json:"condition,omitempty"`
Note string `db:"note,omitempty" json:"note,omitempty"`
Warning string `db:"warning,omitempty" json:"warning,omitempty"`
Length float64 `db:"length,omitempty,omitempty" json:"length,omitempty"`
CableLength float64 `db:"length,omitempty,omitempty" json:"length,omitempty"`
Width float64 `db:"width,omitempty" json:"width,omitempty"`
Height float64 `db:"height,omitempty" json:"height,omitempty"`
WeightLb float64 `db:"weightlb,omitempty" json:"weightlb,omitempty"`
WeightOz float64 `db:"weightoz,omitempty" json:"weightoz,omitempty"`
MetaTitle string `db:"metatitle,omitempty" json:"metatitle,omitempty"`
MetaDesc string `db:"metadesc,omitempty" json:"metadesc,omitempty"`
MetaKeywords string `db:"metakeywords,omitempty" json:"metakeywords,omitempty"`
}
type Category struct {
Name string
Count int
}
type Equipment struct { // Equipment is used to represent a single record in the "equipments" table.
Id int64 `db:"id,omitempty" json:"id,omitempty"`
Image1 string `db:"image1,omitempty" json:"image1,omitempty"`
Image2 string `db:"image2,omitempty" json:"image2,omitempty"`
Image3 string `db:"image3,omitempty" json:"image3,omitempty"`
Thumb string `db:"thumb,omitempty" json:"thumb,omitempty"`
PartNo string `db:"partno" json:"partno"`
Name string `db:"name" json:"name"`
Enable bool `db:"enable,omitempty" json:"enable,omitempty"`
Price float64 `db:"price" json:"price"`
Qty int64 `db:"quantity" json:"quantity"`
MinOrder int64 `db:"minorder,omitempty" json:"minorder,omitempty"`
MaxOrder int64 `db:"maxorder,omitempty" json:"maxorder,omitempty"`
Shippable bool `db:"shippable,omitempty" json:"shippable,omitempty"`