github.com/komosa/bug@v0.3.1/bug-serve/main.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	//	"fmt"
     6  	"github.com/driusan/GoWebapp/HTMLPageRenderer"
     7  	"github.com/driusan/GoWebapp/URLHandler"
     8  	"github.com/driusan/bug/bugs"
     9  	"io/ioutil"
    10  	"net/http"
    11  )
    12  
    13  type MainPageHandler struct {
    14  	URLHandler.DefaultHandler
    15  }
    16  
    17  type BugPageHandler struct {
    18  	URLHandler.DefaultHandler
    19  }
    20  
    21  type SettingsHandler struct {
    22  	URLHandler.DefaultHandler
    23  }
    24  type BugListRenderer struct {
    25  	HTMLPageRenderer.ReactPage
    26  }
    27  
    28  type BugRenderer struct {
    29  	HTMLPageRenderer.ReactPage
    30  	Bug bugs.Bug
    31  }
    32  
    33  func (s SettingsHandler) Get(r *http.Request, p map[string]interface{}) (string, error) {
    34  	settings := struct {
    35  		Title     string
    36  		Directory string
    37  	}{bugs.GetRootDir().GetShortName().ToTitle(), string(bugs.GetRootDir())}
    38  	retVal, _ := json.Marshal(settings)
    39  	return string(retVal), nil
    40  }
    41  func (m MainPageHandler) Get(r *http.Request, p map[string]interface{}) (string, error) {
    42  	page := BugListRenderer{}
    43  	page.Title = "Open Issues"
    44  	page.JSFiles = []string{
    45  		// Bootstrap
    46  		//"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js",
    47  		// React
    48  		"https://cdnjs.cloudflare.com/ajax/libs/react/0.14.3/react.js",
    49  		"https://cdnjs.cloudflare.com/ajax/libs/react/0.14.3/react-dom.js",
    50  		"/js/BugApp.js",
    51  		"/js/BugList.js",
    52  		"/js/BugPage.js",
    53  	}
    54  	page.CSSFiles = []string{
    55  		// Bootstrap
    56  		"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css",
    57  		"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css",
    58  	}
    59  	page.RootElement = "BugApp"
    60  
    61  	return HTMLPageRenderer.Render(page), nil
    62  }
    63  
    64  func getBugList() (string, error) {
    65  	issues, _ := ioutil.ReadDir(string(bugs.GetRootDir()) + "/issues")
    66  
    67  	var issuesSlice []string
    68  
    69  	for _, issue := range issues {
    70  		issuesSlice = append(issuesSlice, issue.Name())
    71  	}
    72  
    73  	retVal, _ := json.Marshal(issuesSlice)
    74  	return string(retVal), nil
    75  }
    76  func (m BugPageHandler) Get(r *http.Request, extras map[string]interface{}) (string, error) {
    77  	if r.URL.Path == "/issues" || r.URL.Path == "/issues/" {
    78  		return getBugList()
    79  	}
    80  	bugDir := string(bugs.GetRootDir()) + r.URL.Path
    81  	b := bugs.Bug{}
    82  	b.LoadBug(bugs.Directory(bugDir))
    83  
    84  	switch r.URL.Query().Get("format") {
    85  	case "json":
    86  		bJSON, _ := json.Marshal(b)
    87  		return string(bJSON), nil
    88  	default:
    89  		page := BugRenderer{Bug: b}
    90  		page.RootElement = "RBugPage"
    91  		page.Title = b.Title("")
    92  		page.JSFiles = []string{
    93  			// Bootstrap JS
    94  			//"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js",
    95  			// React JS
    96  			"https://cdnjs.cloudflare.com/ajax/libs/react/0.14.3/react.js",
    97  			"https://cdnjs.cloudflare.com/ajax/libs/react/0.14.3/react-dom.js",
    98  			"/js/BugPage.js",
    99  		}
   100  		page.CSSFiles = []string{
   101  			"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css",
   102  			"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css"}
   103  		return HTMLPageRenderer.Render(page), nil
   104  	}
   105  
   106  }
   107  
   108  func main() {
   109  	URLHandler.RegisterHandler(MainPageHandler{}, "/")
   110  	URLHandler.RegisterHandler(SettingsHandler{}, "/settings")
   111  	URLHandler.RegisterHandler(BugPageHandler{}, "/issues/")
   112  	URLHandler.RegisterStaticHandler("/js/", "./js")
   113  	http.ListenAndServe(":8080", nil)
   114  }
   115  
   116  //go:generate FileConstGenerator main js/BugApp.js BugListConst.go BugListJS
   117  //go:generate FileConstGenerator main js/BugApp.js BugListConst.go BugListJS