Add -baseurl command line argument
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Peter Stuifzand 2020-05-18 21:58:44 +02:00
parent 419571daf6
commit 40d1fc3879

55
main.go
View File

@ -24,11 +24,10 @@ func init() {
var ( var (
mp PagesRepository mp PagesRepository
)
const ( port = flag.Int("port", 8080, "listen port")
ClientID = "https://wiki.p83.nl/" baseurl = flag.String("baseurl", "", "baseurl")
RedirectURI = "https://wiki.p83.nl/auth/callback" redirectURI string = ""
) )
type Backref struct { type Backref struct {
@ -164,7 +163,7 @@ func (*authHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
authURL := sess.AuthorizationEndpoint authURL := sess.AuthorizationEndpoint
verified, response, err := indieauth.VerifyAuthCode(ClientID, code, RedirectURI, authURL) verified, response, err := indieauth.VerifyAuthCode(*baseurl, code, redirectURI, authURL)
if err != nil { if err != nil {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
@ -220,11 +219,11 @@ func (*authHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
sess.AuthorizationEndpoint = authURL.String() sess.AuthorizationEndpoint = authURL.String()
sess.Me = urlString sess.Me = urlString
sess.LoggedIn = false sess.LoggedIn = false
sess.RedirectURI = RedirectURI sess.RedirectURI = redirectURI
sess.NextURI = "/" sess.NextURI = "/"
sess.State = state sess.State = state
newURL := indieauth.CreateAuthenticationURL(*authURL, urlString, ClientID, RedirectURI, state) newURL := indieauth.CreateAuthenticationURL(*authURL, urlString, *baseurl, redirectURI, state)
http.Redirect(w, r, newURL, 302) http.Redirect(w, r, newURL, 302)
return return
@ -298,8 +297,7 @@ func (h *historyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
func getPageBase() pageBaseInfo { func getPageBase() pageBaseInfo {
clientID := ClientID clientID := *baseurl
redirectURI := RedirectURI
pageBase := pageBaseInfo{ pageBase := pageBaseInfo{
BaseURL: clientID, BaseURL: clientID,
@ -409,12 +407,12 @@ func (h *editHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
pageBase := getPageBase() pageBase := getPageBase()
data := editPage{ data := editPage{
pageBaseInfo: pageBase, pageBaseInfo: pageBase,
Session: sess, Session: sess,
Title: cleanTitle(page), Title: cleanTitle(page),
Content: pageText, Content: pageText,
Editor: editor, Editor: editor,
Name: page, Name: page,
Backrefs: mpPage.Refs, Backrefs: mpPage.Refs,
} }
t, err := template.ParseFiles("templates/layout.html", "templates/edit.html") t, err := template.ParseFiles("templates/layout.html", "templates/edit.html")
@ -512,11 +510,11 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
pageBase := getPageBase() pageBase := getPageBase()
data := indexPage{ data := indexPage{
pageBaseInfo: pageBase, pageBaseInfo: pageBase,
Session: sess, Session: sess,
Title: cleanTitle(page), Title: cleanTitle(page),
Content: template.HTML(pageText), Content: template.HTML(pageText),
Name: page, Name: page,
Backrefs: mpPage.Refs, Backrefs: mpPage.Refs,
} }
t, err := template.ParseFiles("templates/layout.html", "templates/view.html") t, err := template.ParseFiles("templates/layout.html", "templates/view.html")
@ -584,10 +582,10 @@ func (h *recentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
pageBase := getPageBase() pageBase := getPageBase()
err = t.Execute(w, recentPage{ err = t.Execute(w, recentPage{
pageBaseInfo: pageBase, pageBaseInfo: pageBase,
Session: sess, Session: sess,
Title: "Recent changes", Title: "Recent changes",
Name: "Recent changes", Name: "Recent changes",
Recent: changes, Recent: changes,
}) })
if err != nil { if err != nil {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
@ -612,10 +610,11 @@ type LinkResponse struct {
} }
func main() { func main() {
var port int
flag.IntVar(&port, "port", 8080, "http port")
flag.Parse() flag.Parse()
*baseurl = strings.TrimRight(*baseurl, "/") + "/"
redirectURI = fmt.Sprintf("%sauth/callback", *baseurl)
mp = NewFilePages("data") mp = NewFilePages("data")
http.Handle("/auth/", &authHandler{}) http.Handle("/auth/", &authHandler{})
@ -684,6 +683,6 @@ func main() {
http.Handle("/recent/", &recentHandler{}) http.Handle("/recent/", &recentHandler{})
http.Handle("/", &indexHandler{}) http.Handle("/", &indexHandler{})
fmt.Printf("Running on port %d\n", port) fmt.Printf("Running on port %d\n", *port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil)) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
} }